What is Ruby’s method with a bang (!)

  • Post last modified:January 16, 2023
  • Reading time:3 mins read

Understanding Rub’s bang/exclamation mark

Introduction

  • I have been doing Java development for 6+ years of my software developer career.
  • But recently I started learning Ruby and Ruby on Rails.
  • As a Java developer, there are lots of similarities and surprises when I jump on the ruby boat, one of them seeing the exclamation operator.
  • In this blog, we will understand what is the meaning of this exclamation mark in Ruby

Exclamation Mark

  • Methods with an exclamation mark are called bang methods in ruby.
  • The use of exclamation marks mainly tells the programmer that these methods are dangerous and used with caution.
  • Dangerous in the sense that this method will modify the input. Let’s see the example.

Using Array Example

#1: We have a numbers array that we would like to reverse.

numbers = [1,2,3,4,5]

numbers.reverse
puts numbers.join(",")
  • In the above example, the reverse operation didn’t modify the actual numbers array because when we print the numbers array it’s the same as the input array.
  • But if we want to get the reverse array as result then we have to store it in the variable like below and print.
numbers = [1,2,3,4,5]

reversed_number = numbers.reverse
puts reversed_number.join(",")
  • So basically in above example reverse method returns the result array without modifying actual input array or changing the state.
  • Now let’s use the reverse with a bang.
numbers = [1,2,3,4,5]
numbers.reverse!
puts numbers.join(",")
  • As we can see bang methods modify the input array itself, thats why ! means use with caution, since its changes the state of input data.
  • Whenever there is a change in state or mutability we have to be extra cautious since as a developer we can make mistakes.
  • Let’s check one more example
def squared(numbers:)
    numbers.map! { |num| num*num}
end

def multiply(numbers:)
    numbers.map! {|num| num*2}
end


nums = [1,2,3,4,5]
puts "input -> " + nums.join(",")


squared =  squared(numbers: nums)
puts "squared nums: ->" + squared.join(",")
multiply = multiply(numbers: nums)
puts "multiplied nums:->" + multiply.join(",")
  • In the above example, we are squaring and multiplying input numbers and our results are not as expected.
  • This is because we are using bang which is changing the input number when we perform square , hence multiply was performed on modified nums array. As a developer its easy to make such mistake, hence in ruby we use bang which basically tells that this is dangerous or distructive method.
  • The below example provides expected result where we are not using bang .
def squared(numbers:)
    numbers.map { |num| num*num}
end

def multiply(numbers:)
    numbers.map {|num| num*2}
end


nums = [1,2,3,4,5]
puts "input -> " + nums.join(",")


squared =  squared(numbers: nums)
puts "squared nums: ->" + squared.join(",")
multiply = multiply(numbers: nums)
puts "multiplied nums:->" + multiply.join(",")

Conclusion

  • In this article, we discuss what is the use of bang! in Ruby and how we as developers need to be cautious when we use it since it’s modify the input data state.

Ruby Bestseller Course

if you want to learn from scratch you can checkout this udemy bestseller course ( 32 hours content , 4.7 rating, 74k+ students already enrolled )

Leave a Reply