A
Abder-Rahman Ali
This is an example similar to the one here:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/56755
class Foo
def initialize
@var = "var"
end
def to_s
"This is a customized string"
end
end
var = Foo.new
puts var
There are some scenarios I'm doing here:
1- When I remove "def to_s" method, and for "puts var", I change it as
follows:
puts var.to_s
Result: #<Foo:0x273a8>
2- in "def to_s":
def to_s
@var
end
Result: var
3- Running the code as it is shown at the top.
Result: "This is a customized string"
4- Without "def to_s".
Result: #<Foo:0x273f8>
The most parts that are confusing me here are scenarios (1) and (4). Why
are the following results? And, what do those results mean by the way?
Thanks.
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/56755
class Foo
def initialize
@var = "var"
end
def to_s
"This is a customized string"
end
end
var = Foo.new
puts var
There are some scenarios I'm doing here:
1- When I remove "def to_s" method, and for "puts var", I change it as
follows:
puts var.to_s
Result: #<Foo:0x273a8>
2- in "def to_s":
def to_s
@var
end
Result: var
3- Running the code as it is shown at the top.
Result: "This is a customized string"
4- Without "def to_s".
Result: #<Foo:0x273f8>
The most parts that are confusing me here are scenarios (1) and (4). Why
are the following results? And, what do those results mean by the way?
Thanks.