Why getting error for this code fragment

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)

Are you trying to use unary if operations? If so you need to add ‘:’
like so (runtime==nil ? ‘nil’ : :runtime)

On Wed, Mar 31, 2010 at 1:27 AM, Amishera Amishera

On Mar 30, 2010, at 8:27 PM, Amishera Amishera wrote:

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/.

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 B. http://agileconsultingllc.com
[email protected]

Rob B. wrote:

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

Or even:

def to_str
“#{runtime||‘nil’}&&&#{language||‘nil’}&&&#{color||‘nil’}&&&#{sound||‘nil’}”
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs