Member-only story

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

Understanding Rub’s bang/exclamation mark

Suraj Mishra
3 min readJan 24, 2023

Originally Published in https://asyncq.com/

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…

--

--

Suraj Mishra
Suraj Mishra

Written by Suraj Mishra

Staff Software Engineer @PayPal ( All opinions are my own and not of my employer )

No responses yet