M
Martin Elzen
Hi all. Am I the only one that finds it odd that if I define a to_s
function for a class, that if I do:
puts obj
I won't get errors, but if I try something like:
s = String.new
s += obj
I get a 'can not convert a <class type>' into String type error'...
It's no prob to write obj.to_s, it's just I expected the first version to
call to_s automatically...
Martin
PS for clarity, my complete error producing example is as follows:
class Animal
def initialize(name)
fail "name is nil" if name.nil?
@name = name
end
def to_s
"#{@name}"
end
end
c = Animal.new('Snoes')
puts c
puts "substituted in a string literal: #{c}"
s = String.new
s += c
#puts s
function for a class, that if I do:
puts obj
I won't get errors, but if I try something like:
s = String.new
s += obj
I get a 'can not convert a <class type>' into String type error'...
It's no prob to write obj.to_s, it's just I expected the first version to
call to_s automatically...
Martin
PS for clarity, my complete error producing example is as follows:
class Animal
def initialize(name)
fail "name is nil" if name.nil?
@name = name
end
def to_s
"#{@name}"
end
end
c = Animal.new('Snoes')
puts c
puts "substituted in a string literal: #{c}"
s = String.new
s += c
#puts s