Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accidental expression evaluation in ruby

Why this expression evaluating to 13 ?.

I accidentally evaluated this expression (1_2).next instead of (1+2).next which o/p 4 as result.

=> (1_2).next
=> 13

Please let me know how this is, as i m new to Ruby

like image 401
swapnesh Avatar asked May 19 '26 19:05

swapnesh


1 Answers

Ruby allows you to use _ to break up long numbers, for example

123456789 == 123_456_789

but the latter is a little easier to read, so your code is the same as 12.next

like image 181
Frederick Cheung Avatar answered May 22 '26 10:05

Frederick Cheung