2012年7月22日日曜日

開発環境

『Learning Ruby』(Michael Fitzgerald 著、O'Reilly Media、2007年、ISBN978-0-596-52986-4)の Chapter 9(Classes)Review Questions 3を解いてみる。

その他参考書籍

3.

関数名の前にクラス名を付け加えればいい。

コード(TextWrangler)

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

class C
  def initialize
    @str = "Hello, Ruby!"
  end
  def p
    puts @str
  end
  # クラスメソッド
  def C.p
    puts "Hello, World!"
  end
end

c = C.new
c.p
C.p

入出力結果(Terminal)

$ ./sample.rb
Hello, Ruby!
Hello, World!
$

0 コメント:

コメントを投稿