# Why getting error for this code fragment

**URL:** <https://www.ruby-forum.com/t/why-getting-error-for-this-code-fragment/186059>\
**Category:** Ruby\
**Created:** [March 31, 2010, 2:27am UTC](https://www.ruby-forum.com/t/why-getting-error-for-this-code-fragment/186059 "2010-03-31T02:27:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [March 31, 2010, 2:27am UTC](https://www.ruby-forum.com/t/why-getting-error-for-this-code-fragment/186059/1 "2010-03-31T02:27:00Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [March 31, 2010, 3:06am UTC](https://www.ruby-forum.com/t/why-getting-error-for-this-code-fragment/186059/2 "2010-03-31T03:06:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [March 31, 2010, 3:28am UTC](https://www.ruby-forum.com/t/why-getting-error-for-this-code-fragment/186059/3 "2010-03-31T03:28:23Z")

</div>

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/](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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [March 31, 2010, 12:23pm UTC](https://www.ruby-forum.com/t/why-getting-error-for-this-code-fragment/186059/4 "2010-03-31T12:23:14Z")

</div>

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
