Bug #2736

StringConstant +"string literal" (unspaced) raises exception

Added by Joe Lapp almost 2 years ago. Updated 10 months ago.

[redmine4ruby-lang:272]
Status:Closed Start date:02/12/2010
Priority:Low Due date:
Assignee:- % Done:

0%

Category:-
Target version:-

Description

When concatenating to a string constant, if a space does not follow '+' operator, Ruby raises an exception complaining that the unary '+' operator on strings is not defined.

The following code:

Konst = 'A'
Konst +'B'

...raises the following exception in IRB:

NoMethodError: undefined method `+@' for "B":String
	from (irb):3
	from /usr/local/bin/irb:12:in `<main>'

Note that the following code -- adding a space after the '+' -- works as expected:

Konst = 'A'
Konst + 'B'
=> "AB"

The behavior appears to be a bug because the following does work as expected:

v = 'A'
v +'B'
=> "AB"

This also works:

'A' +'B'
=> "AB"

I discovered the problem when I changed working code by replacing an r-value variable with a constant.  Replacing a literal or an r-value variable with a constant shouldn't break anything if the types match.

I found he problem in Ruby 1.9.1p376, but also reproduced it in 1.9.1p243.  Friends are reporting that Ruby 1.8.7 (MRI) and JRuby 1.4 also have the problem.

History

Updated by amsalways amsalways about 1 year ago

I am also really having problem with the string constantant but if you search for a video tutorial about string constantant,then you will get a brief introduction of string concept in programming.Love stories

Updated by Yui NARUSE 11 months ago

  • Status changed from Open to Closed

wrong project, please post it to ruby's project.

Also available in: Atom PDF