Skip to content

Instantly share code, notes, and snippets.

@sriranggd
Created August 14, 2011 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sriranggd/1145457 to your computer and use it in GitHub Desktop.
Save sriranggd/1145457 to your computer and use it in GitHub Desktop.
Ruby -- issues with unary + used on strings
$ ./irb
>> def ret_str
>> "hello "
>> end
=> nil
>> @ins_var = "world"
=> "world"
>> ret_str +@ins_var
NoMethodError: undefined method `+@' for "world":String
from (irb):5
from ./irb:12:in `<main>'
>> ins_var
NameError: undefined local variable or method `ins_var' for main:Object
from (irb):6
from ./irb:12:in `<main>'
>> self.ins_var
NoMethodError: undefined method `ins_var' for main:Object
from (irb):7
from ./irb:12:in `<main>'
>> "hello " +@ins_var
=> "hello world"
>> str = "hello "
=> "hello "
>> str +@ins_var
=> "hello world"
>> ret_str+@ins_var
=> "hello world"
>> str = "world"
=> "world"
>> ret_str +str
NoMethodError: undefined method `+@' for "world":String
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment