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’.
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.
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.
# Define the class Greeter
class Greeter
# Define the method hello
# This method greets the
# user
def hello(name)
puts "Hello " + name
end # End of the method hello
end # End of the class Greeter
Greeter.new.hello("Tyler")
Transcript:
Hello everybody and welcome to Programming With Ruby Episode 4, Main
Ruby Concepts.
Covered in this episode. We’ll be playing with interactive ruby (also
called irb). I will teach you about variables, basic methods, and
classes in Ruby. Lets get started!
Basics Description
Ruby is an object oriented programming language. object oriented
languages use objects. Ruby goes beyond most other object oriented
languages, because in Ruby, everything is an object. (I’ll show you
exactly what that means in a few minutes)
Object oriented programming sort of models real life. Look around you,
everything around you is an object. Your computer, your desk, books,
the moon, and people are all objects.
In programming, all objects have properties called variables. These
could be the color of the object, the weight, size, or any other kind
of property.
Objects also have methods (which are sometimes called functions). A
camera object would have a method to take pictures. A car would have a
method to drive. A printer would have a method to print.
As well as making code easier to understand, you will also continue to
appreciate other benefits of object oriented programming further on in
your programming career.
Example
I have written a very basic Ruby program. This is about what most
programs look like, just a lot simpler. Let’s break it down line by
line.
Everything followed by a hash mark (#), is ignored by Ruby. These are
called comments. In these comments you can describe what your code is
doing, make notes to yourself, and other such things.
The first line says “class Greeter”. This line tells Ruby that we are
now defining a class named “Greeter”. Please remember that all classes
in Ruby start with a capital letter!
The next line says “def hello(name)”. This line means we are defining
a function (def), named “hello”, that takes the parameter “name”.
The following line is a little trickier. ‘puts “Hello ” +
name’. “puts” means “put string”. our string is “Hello ” + name. Now
you’re probably wondering what the “+ name” is for. We’re aren’t doing
math on strings, but we are connecting the variable “name” to our other
string “Hello “.
The next two lines have the keyword “end”. The first end means we are
done defining the function “hello”. The second “end” means we are done
defining the class “Greeter”.
Below where the class “Greeter” ends we have the line ‘myname =
“Tyler”‘. This means are creating the variable myname and placing
“Tyler” inside of it.
Next is, the line “person = Greeter.new”. This means we are
instantiating a new greeter object named person.
Finally we have the line “person.hello(myname)”. This calls the
person object’s method “hello” and passes the variable “myname” as an
argument.
Now lets run the program. If you remember, to run a program you open
Terminal or Command Prompt, change directories into where you saved
your program and type “ruby programname.rb”. In my case I will type
“ruby episode4.rb”.
And there you have it, it says “Hello Tyler”
The code in this example will be available in the YouTube video
description, and below this video on manwithcode.com
Example 2
Back to the code. This code is actually a little longer
than it has to be. I did this so things would hopefully make more
sense. In reality we can change the last three lines to
‘Greeter.new.hello(“Tyler”)’
We can do this because in Ruby, everything is an expression. Here’s how
it breaks down. “Greeter.new” creates a greeter object. “.hello” calls
the method “hello” on that new object. And then we pass in the string
“Tyler” as an parameter.
Ruby has many other tricks like this to make your code shorter. Some
have disadvantages and others do not. One of the most important
disadvantages can be clarity or readability. You want your code to be
as easy to understand as possible.
irb
Interactive Ruby (or irb for short). irb makes it very easy to
quickly test out code, find out if/how something works, do basic math,
and write throwaway code you will only use once.
To open irb, open up your Command Prompt or Terminal and enter
irb. Alternatively, if you are on Windows, go into All Programs and
under Ruby will be a program name fxri which is an equivalent to irb.
In irb you can do basic math:
2 + 2 #=> 4
2**6 #=> 64
2 * 5 #=> 10
We can write any valid Ruby code. So we could write the greet method again.
def greet(name)
puts "Hello " + name
end
And then call it:
greet("Tyler")
Remember earlier I said everything in Ruby was an object? I’ll show
you what I mean. if you type in the name of an object and call the
class method it will tell you its class.
0.class #=> Fixnum
"Hello".class #=>; String
String.class #=> Class
I encourage you to play around with irb for a little while, try
writing your own methods, and have some fun with it.
End
This brings us to the end of this episode, I hope it helped
you. If you need any help, have questions or comments, leave a
comment below or contact me at tyler@manwithcode.com
Don’t forget to donate! There is a donation link to the right of this
video. On YouTube its in the description box. On my website, it is to
the right of the video.