Codebeerstartups

Long integer literals in ruby

Introduction

user

Mohit Jain

Rails Developer


ruby tips-and-tricks

Long integer literals in ruby

Posted by Mohit Jain on .
Featured

ruby tips-and-tricks

Long integer literals in ruby

Posted by Mohit Jain on .

I am here to share a small tip that I just come to know about Ruby. A simple question. How many zeros are there integer written below?

100000000000

Difficult to count right? In normal day to day practice, we generally use a comma to separate the digits, for example in US standard we use a comma every 3 digits. So number written above can be re-written as:

100,000,000,000

Now the same thing can be done with long integers in Ruby using an underscore, something like this:

100_000_000_000

which is actually same as writing 100000000000, but using underscore makes things easier to read;)

I hope you will find this technique somewhere useful ;)

user

Mohit Jain

http://codebeerstartups.com

Rails Developer