Actions
Bug #11803
closed
NoMethodError when calling String#gsub
Description
Assigning the result of a call to String#gsub throws a NoMethodError. It seems to have to do with assigning the return value to a variable with the same name as the method returning the string gsub is called on. I've included short script which reproduces the issue.
Files
Updated by Anonymous almost 10 years ago
- File test_fail.rb test_fail.rb added
Updated by nobu (Nobuyoshi Nakada) almost 10 years ago
- Status changed from Open to Rejected
def to_s
return unless word
word = word.gsub(/a/, "")
You make a new local variable here, and call gsub
on it before assignment.
Updated by Hanmac (Hans Mackowiak) almost 10 years ago
first i dont think its a good idea in a to_s method to change the value of the object.
otherwise you need to use @instance variables then it works:
class TestFail
def initialize(word)
@word = word
end
def to_s
return unless @word
@word.gsub(/a/, "")
end
def word
@word
end
end
Actions
Like0
Like0Like0Like0