Odkazy:
Bežnou neanonymní třídu definujeme takto.
class Person
…
endTřídu můžeme také definovat anonymně, voláním new. Třídy Class.
Person = Class.new do
…
endDalší zajímavé zápisy k prozkoumání a popsání.
class Person
class << self
def who2
#
true
end
end
end
class << Person
def who2
#
end
end
Můžeme přidat metodu nikoliv do třídy ale do samotného objektu, tedy instance třídy. K tomu můžeme použít dva různé zápisy:
class << foo
def bar
puts "hello world"
end
enddef foo.bar
puts "hello, world"
endZ hlediska Ruby jsou oba zápisy ekvivalentní.
Tři ekvivalentní zápisy definování metody třídy:
def String.hello
puts "hello"
end
class String
def self.hello
puts "hello"
end
end
class String
class << self
def hello
puts "hello"
end
end
end
