DEV Community

Discussion on: What does ** (two asterisks in a row) mean in Ruby?

Collapse
 
val_baca profile image
Valentin Baca • Edited

I used to do Project Euler in college with Ruby during semesters when I had fewer coding classes, and this brings back memories!

Don't forget about irb and ri:

$ irb
irb(main):001:0> help 'Integer#**'
# or
$ ri 'Integer#**'

Both give:

= Integer#**

(from ruby core)
------------------------------------------------------------------------------
  int ** numeric  ->  numeric_result

------------------------------------------------------------------------------

Raises int to the power of numeric, which may be negative or fractional. The
result may be an Integer, a Float, a Rational, or a complex number.

  2 ** 3        #=> 8
  2 ** -1       #=> (1/2)
  2 ** 0.5      #=> 1.4142135623730951
  (-1) ** 0.5   #=> (0.0+1.0i)

  123456789 ** 2     #=> 15241578750190521
  123456789 ** 1.2   #=> 5126464716.0993185
  123456789 ** -2    #=> (1/15241578750190521)

Useful since google can be a little hard to use with special characters, like "**"