Try it like this:
5 class Movie_Info
6 attr_accessor :runtime, :language, :sound, :color
7 def to_str
38 puts (runtime==nil ? 'nil' : runtime)+'&&&'+(language==nil ? 'nil' : language)+'&&&'+(color==nil ? 'nil' : color)+'&&&'+(sound==nil ? 'nil' : sound)
39 return str
40 end
41 end
It might be that ?' is being parsed as a character literal or it might be that :runtime is being parsed as a symbol. It's hard to tell because it seems to be missing lines 8..37. But in any case, I think that you are really trying for something like these. All three of these should give you the same result. In your original, you never create a str variable. The puts will send output to $stdout, but not return anything except nil.
Also, the to_str is used when the object should act like a String. Compare that with to_s which typically returns a representation of the object as a string (often a more human-readable version that the default from Object#to_s).
class Movie_Info
attr_accessor :runtime, :language, :sound, :color
def to_str
str = self.runtime || 'nil'
str << '&&&' << (self.language || 'nil')
str << '&&&' << (self.color || 'nil')
str << '&&&' << (self.sound || 'nil')
str
end
end
class Movie_Info
attr_accessor :runtime, :language, :sound, :color
def to_str
[ :runtime, :language, :color, :sound ].map do |field|
self.send(field) || 'nil'
end.join('&&&')
end
end
class Movie_Info
attr_accessor :runtime, :language, :sound, :color
def to_str
[ :runtime, :language, :color, :sound ].map do |field|
instance_variable_get("@#{field}") || 'nil'
end.join('&&&')
end
end
-Rob
Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
···
On Mar 30, 2010, at 8:27 PM, Amishera Amishera wrote:
5 class Movie_Info
6 attr_accessor :runtime, :language, :sound, :color
7 def to_str
38 puts
(runtime==nil?'nil':runtime)+'&&&'+(language==nil?'nil':language)+'&&&'+(color==nil?'nil':color)+'&&&'+(sound==nil?'nil':sound)
39 return str
40 end
41 end
the statement at line 38 causes some error.
./util.rb:38: syntax error, unexpected tSTRING_BEG, expecting kDO or '{'
or '(' (SyntaxError)
puts
(runtime==nil?'nil':runtime)+'&&&'+(language==nil?'nil':language)+'&&&'+(color==nil?'nil':color)+'&&&'+(sound==nil?'nil':sound)
--
Posted via http://www.ruby-forum.com/\.