Want more videos? Donate.
Programming With Ruby Episode 4, Main Ruby Concepts
Covered In This Episode:
- Basic Variables
- Basic Methods
- Basic Classes
- Interactive Ruby (irb)
# 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")
Hello everybody and welcome to Programming With Ruby Episode 4, Main
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!
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
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.
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
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
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
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
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
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.
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:
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.
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 email@example.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.
Thanks for watching! Bye!Programming With Ruby Episode 3, Getting Help/ToolsProgramming With Ruby Episode 5, Numbers