Runnin' this site ain't free, help us out! Donate.

Programming With Ruby Episode 5, Numbers

by Tyler on April 24th, 2009
Programming With Ruby Episode 4, Main Ruby ConceptsProgramming With Ruby Episode 6, Strings


Covered in This Episode

  • Numbers
  • Expressions
  • Types of Numbers
  • Constants (New type of variable!)
  • Doing something a number of times

Transcript

Hello Everyone and Welcome to Programming With Ruby Episode 5,
Numbers. Its sitll presented by me, Tyler. And brought to you by
manwithcode.com

In this episode I will be talking about numbers in Ruby. I will go
over expressions, the different types of numbers, I will introduce a
new variable type called a constant, and will show you how to repeat
an action a certain number of times in Ruby.

Lets get started!

Pop open you Command prompt or Terminal and start Interactive Ruby. If
you have forgotten, just enter ‘irb’.

First item is expressions.

2 + 2 #=> 4
2 - 2 #=> 0
3 * 3 #=> 9
6 / 3 #=> 2
2 ** 5 #=> 5

expressions with variables work in the same way

x = 5
5 * x #=> 25
x ** 4 #=> 625

If you want to do math on a variable and set the value of the variable
to the result use:

x = 5
x *= 2 #=> x = 10
x -= 4 #=> x = 6
x /= 6 #=> x = 1

In Ruby there are 3 different types of numbers. Fixnum, which are
32-bit numbers. Floats, which are numbers with a value after the
decimal point. Bignums which are numbers larger than 32-bits.

5.class #=> Fixnum
0.421.class #=> Float
999999999999 #=> Bignum

In Ruby it is possible to separate up larger numbers to improve
readability in the code. Simply place in underscores

1_000_000 #=> 1000000

Lets say you are writing some code and you need to do something a
certain number of times. Use this code:

2.times do |x|
puts x
end
#=> 0
#=> 1

That wraps it up for this episode. If you have any questions or
comments, leave a comment in the comment box below. Or email me at
tyler@manwithcode.com

Don’t forget to donate. The button is to the right of this video.

Thanks for watching. Goodbye

Programming With Ruby Episode 4, Main Ruby ConceptsProgramming With Ruby Episode 6, Strings

Be Sociable, Share!

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS

6 Comments
  1. Silverhailo21 permalink

    Hey, thanks for the videos Tyler. I’ve been really enjoying them and I wanted to let you know that as an individual with very little actual programing experience, I’ve been following the videos without a problem. Occasionally I hop on a forum to explore a concept I don’t fully understand, but for the most part everything is explained very well. Thanks so much for making the videos, I’ll donate when I have some more money. ^_^

  2. Akash permalink

    Thanks so much for putting up these videos. It’s so difficult to find programming tutorials that get straight to the point without driving you crazy with all sorts of links and other diversions.

  3. http://mensengagementrings.ca permalink

    I do trust all of the concepts you have offered for your post. They are really convincing and will certainly work. Still, the posts are too brief for novices. May just you please lengthen them a bit from next time? Thanks for the post.

  4. Kirk Abela permalink

    I rarely leave a response, however I looked at a great deal of remarks here Programming With Ruby Episode 5, Numbers | Man With Code. I do have some questions for you if it’s okay. Could it be just me or does it look like like a few of these remarks look like they are left by brain dead individuals? :-P And, if you are posting at additional places, I would like to keep up with anything fresh you have to post. Would you list of the complete urls of all your shared sites like your Facebook page, twitter feed, or linkedin profile?

    • They’re not brain dead, they’re just not programmers yet. Please don’t insult them, if ya don’t mind.

      My twitter feed is listed on the website (though I don’t update it anymore), and I don’t have any other pages I’d like to share with the world, sorry :P