[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Noob Concatenation Q?

jervis

5/4/2007 8:54:00 PM

Can someone briefly explain why ::
puts i.to_s +" :: "+arrayoutput[i]

yaml.rb:12: undefined method `+@' for " :: ":String (NoMethodError)
from F:/RUBY/Source/yaml.rb:11:in `times'
from F:/RUBY/Source/yaml.rb:11

but the addition of a space => puts i.to_s + " :: "+arrayoutput[i]
results in success.

some proir exp in java and cant remember requiring spaces in
concatentation. any replies welcomed!




--
--------------------------------- --- -- -
Posted with NewsLeecher v3.8 Beta 6
Web @ http://www.newsleecher.c...
------------------- ----- ---- -- -

3 Answers

Marcin Mielzynski

5/4/2007 9:09:00 PM

0

jervis@o2.ie wrote:
> Can someone briefly explain why ::
> puts i.to_s +" :: "+arrayoutput[i]
>
> yaml.rb:12: undefined method `+@' for " :: ":String (NoMethodError)
> from F:/RUBY/Source/yaml.rb:11:in `times'
> from F:/RUBY/Source/yaml.rb:11
>

because parens in Ruby are optional, so it ends up:

puts i.to_s(+" :: "+arrayoutput[i])


lopex

William James

5/4/2007 9:27:00 PM

0

On May 4, 3:54 pm, jer...@o2.ie (jer...@o2.ie) wrote:
> Can someone briefly explain why ::
> puts i.to_s +" :: "+arrayoutput[i]
>
> yaml.rb:12: undefined method `+@' for " :: ":String (NoMethodError)
> from F:/RUBY/Source/yaml.rb:11:in `times'
> from F:/RUBY/Source/yaml.rb:11
>
> but the addition of a space => puts i.to_s + " :: "+arrayoutput[i]
> results in success.
>
> some proir exp in java and cant remember requiring spaces in
> concatentation. any replies welcomed!
>
> --
> --------------------------------- --- -- -
> Posted with NewsLeecher v3.8 Beta 6
> Web @http://www.newsleecher.c...
> ------------------- ----- ---- -- -

I know I'm not answering your question, but
I think this is cleaner:

puts "#{ i } :: #{ array_output[i] }"

Rick DeNatale

5/5/2007 11:19:00 PM

0

On 5/4/07, Marcin Mielzynski <lopx@gazeta.pl> wrote:> jervis@o2.ie wrote:> > Can someone briefly explain why ::> > puts i.to_s +" :: "+arrayoutput[i]> >> > yaml.rb:12: undefined method `+@' for " :: ":String (NoMethodError)> > from F:/RUBY/Source/yaml.rb:11:in `times'> > from F:/RUBY/Source/yaml.rb:11> >>> because parens in Ruby are optional, so it ends up:>> puts i.to_s(+" :: "+arrayoutput[i])Correct and then the + in +"::"is being interpreted by the parser as a unary + which it implements bysending the message :+@ to the string "::".$ qri +@------------------------------------------------------------- Numeric#+@ +num => num------------------------------------------------------------------------ Unary Plus---Returns the receiver's value.-- Rick DeNataleMy blog on Rubyhttp://talklikeaduck.denh...