Actions
Bug #12851
closedstring.gsub!(/\W/, '').downcase! returns undefined method in some (listed) cases
Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin16]
Description
Hi! Found this strange issue with gsub! and downcase! methods used together for a string. Example code:
def palindrome? (str)
str.gsub!(/\W/, '').downcase!
str == str.reverse
end
puts palindrome?("Madam, I'm Adam!")
returns true. Ok! But...!
if the given string is single word:
def palindrome? (str)
str.gsub!(/\W/, '').downcase!
str == str.reverse
end
puts palindrome?("aBba")
it returns no method error.
hw1_string.rb:13:in palindrome?': undefined method
downcase!' for nil:NilClass (NoMethodError)
from hw1_string.rb:30:in `'
If the code is refactored like this:
def palindrome? (str)
str.gsub!(/\W/, '')
str.downcase!
str == str.reverse
end
puts palindrome?("abba")
puts palindrome?("Madam, I'm Adam!")
than it worked ok in both cases. Why?
I'm new to ruby, and, may be still misunderstood something, but seems that is really a bug.
Updated by ciscoved (Mikhail A) over 7 years ago
- Description updated (diff)
Updated by ciscoved (Mikhail A) over 7 years ago
- Description updated (diff)
Updated by jeremyevans0 (Jeremy Evans) over 7 years ago
- Status changed from Open to Rejected
This isn't a bug. String#gsub! returns nil if no changes are made. From the documentation for String#gsub!: "Performs the substitutions of String#gsub in place, returning str, or nil if no substitutions were performed."
Actions
Like0
Like0Like0Like0