[#77789] [Ruby trunk Feature#12012] Add Boolean method — prodis@...
Issue #12012 has been updated by Fernando Hamasaki de Amorim.
4 messages
2016/10/27
[ruby-core:77665] [Ruby trunk Bug#12851][Rejected] string.gsub!(/\W/, '').downcase! returns undefined method in some (listed) cases
From:
merch-redmine@...
Date:
2016-10-18 17:43:36 UTC
List:
ruby-core #77665
Issue #12851 has been updated by Jeremy Evans.
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."
----------------------------------------
Bug #12851: string.gsub!(/\W/, '').downcase! returns undefined method in some (listed) cases
https://bugs.ruby-lang.org/issues/12851#change-60938
* Author: Mikhail A
* Status: Rejected
* Priority: Normal
* Assignee:
* ruby -v: ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin16]
* Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
----------------------------------------
Hi! Found this strange issue with gsub! and downcase! methods used together for a string. Example code:
~~~ ruby
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:
~~~ ruby
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 `<main>'
If the code is refactored like this:
~~~ ruby
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.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>