Forum: Ruby Noob Concatenation Q?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2007-05-05 01:00
(Received via mailing list)
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.com/?usenet
------------------- ----- ---- -- -
Marcin M. (Guest)
on 2007-05-05 01:10
(Received via mailing list)
removed_email_address@domain.invalid 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 J. (Guest)
on 2007-05-05 01:30
(Received via mailing list)
On May 4, 3:54 pm, removed_email_address@domain.invalid 
(removed_email_address@domain.invalid) wrote:
> 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.com/?usenet
> ------------------- ----- ---- -- -

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

puts "#{ i } :: #{ array_output[i] }"
Rick D. (Guest)
on 2007-05-06 03:20
(Received via mailing list)
On 5/4/07, Marcin Miel¿yñski <removed_email_address@domain.invalid> wrote:
>
> puts i.to_s(+" :: "+arrayoutput[i])

Correct and then the + in
  +"::"

is being interpreted by the parser as a unary + which it implements by
sending the message :+@ to the string "::".

$ qri +@
------------------------------------------------------------- Numeric#+@
     +num    => num
------------------------------------------------------------------------
     Unary Plus---Returns the receiver's value.


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
This topic is locked and can not be replied to.