Difference Between Downcase and Downcase! in Ruby

Difference Between downcase and downcase! in Ruby

Methods with an exclamation mark at the end are often called bang-methods. A bang method doesn't necessarily modify its receiver as well as there is no guarantee that methods without a exclamation mark doesn't.

It's all very well explained in this blog post. To quote the post:

The ! in method names that end with !
means, “This method is dangerous”—or,
more precisely, this method is the
“dangerous” version of an otherwise
equivalent method, with the same name
minus the !. “Danger” is relative; the
! doesn’t mean anything at all unless
the method name it’s in corresponds to
a similar but bang-less method name.

and

The ! does not mean “This method
changes its receiver.” A lot of
“dangerous” methods do change their
receivers. But some don’t. I repeat: !
does not mean that the method changes
its receiver.

Ruby's string.downcase! behavior

This is because String#downcase! is a destructive method (hence the !) meaning it operates on the object in place. Since it changes the object itself, its return value isn't need. The Ruby designers decided that a good use of the return value would be to indicate if any changes were made.

Downcases the contents of str, returning nil if no changes were made.

Your string is already lowercase, so downcase! returns nil which you then replace the variable with.

.downcase! syntax shorthand

Edit: Having an exclamation point (aka "bang") at the end of a method name in ruby means "handle with care". From Matz himself:

The bang (!) does not mean "destructive" nor lack of it mean non
destructive either. The bang sign means "the bang version is more
dangerous than its non bang counterpart; handle with care". Since
Ruby has a lot of "destructive" methods, if bang signs follow your
opinion, every Ruby program would be full of bangs, thus ugly.

(For the full thread, see @sawa's link in the comments.)

For the method in question, downcase is making a copy of the given string, modifying that, and returning that copy as a result. Whereas downcase! modifies the string itself.

In the first case, you're modifying the variable stored in gets.chomp, in the second you're modifying user_input.

Note that if you call user_input.downcase on the last line (instead of user_input.downcase!) it won't actually change user_input, it just returns a copy of the string and makes the copy lowercase.

How to compare strings ignoring the case

You're looking for casecmp. It returns 0 if two strings are equal, case-insensitively.

str1.casecmp(str2) == 0

"Apple".casecmp("APPLE") == 0
#=> true

Alternatively, you can convert both strings to lower case (str.downcase) and compare for equality.

Ruby - Downcase on string values in a hash that also contains nil values

Check if the value is a valid string before downcasing

testHash.each { |k, v| v.downcase! if v.is_a?(String) }

Ruby doesn't recognize `downcase` or `split` as defined methods?

If returns a blank string because you don't return anything inside the collect! block. Return word and it will work :

def translate phrase
phrase = phrase.downcase.split(/ /)
phrase.collect! do |word|
word = word.split(//)
switched = false
while switched == false
word.each_index do |letter|
if word[letter] == ("a" || "e" || "i" || "o" || "u")
switched = true
word = (word[letter..-1] + word[0..(letter-1)]).join + "ay"
end
end
end
word
end
return phrase.join(" ")
end

puts translate("chocolate cream")
#=> atechocolay amcreay

It doesn't look like it's returning exactly what you expected, but it's still better than a blank string.

As for your weird error message in the console, it seems to be specific to the REPL (possibly because of the method name translate).

include downcase in if statement for ruby

File.read returns the content of the file as a String. So, to achieve what you want, you would need to convert both Strings you're working with to lowercase. You can achieve it by:

if row[0].to_s != "" && File.read(commit_file).downcase.include?(row[0].to_s.downcase)


Related Topics



Leave a reply



Submit