why getting error for this code fragment

A

Amishera Amishera

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)
 
J

jbw

Are you trying to use unary if operations? If so you need to add ':'
like so (runtime=3D=3Dnil ? 'nil' : :runtime)

=C2=A05 class Movie_Info
=C2=A06 =C2=A0 =C2=A0 =C2=A0 =C2=A0 attr_accessor :runtime, :language, :s= ound, :color
=C2=A07 =C2=A0 =C2=A0 =C2=A0 =C2=A0 def to_str
=C2=A038 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 puts
(runtime=3D=3Dnil?'nil':runtime)+'&&&'+(language=3D=3Dnil?'nil':language)= +'&&&'+(color=3D=3Dnil?'nil':color)+'&&&'+(sound=3D=3Dnil?'nil':sound)
=C2=A039 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return s= tr
=C2=A040 =C2=A0 =C2=A0 =C2=A0 =C2=A0 end
=C2=A041 end

the statement at line 38 causes some error.

./util.rb:38: syntax error, unexpected tSTRING_BEG, expecting kDO or '{'
or '(' (SyntaxError)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0puts
(runtime=3D=3Dnil?'nil':runtime)+'&&&'+(language=3D=3Dnil?'nil':language)= +'&&&'+(color=3D=3Dnil?'nil':color)+'&&&'+(sound=3D=3Dnil?'nil':sound)



--=20
jbw
 
R

Rob Biedenharn

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)


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
(e-mail address removed)
 
B

Brian Candler

Rob said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Syntax error 16
block.call 3
Syntax error with blocks 5
what do you think of this code? 15
strange syntax error in while loop 2
Why is this not implemented in Ruby 15
Problem getting user input 5
void value expression 2

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top