Man With Code 👨‍💻

In which I occasionally teach you things.

Programming With Ruby Episode 4, Main Ruby Concepts

Series: Ruby Programming

Covered In This Episode: Code: [sourcecode language="ruby"] # 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") [/sourcecode] 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: [sourcecode language="ruby"] 2 + 2 #=> 4 2**6 #=> 64 2 * 5 #=> 10 [/sourcecode] We can write any valid Ruby code. So we could write the greet method again. [sourcecode language="ruby"] def greet(name) puts "Hello " + name end [/sourcecode] And then call it: [sourcecode language="ruby"] greet("Tyler") [/sourcecode] 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. [sourcecode language="ruby"] 0.class #=> Fixnum "Hello".class #=>; String String.class #=> Class [/sourcecode] 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 [email protected] 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!

Comments

Siriquelle on

Really enjoying this video series thanks for putting it together. :)

Tyler on

Your welcome!

Rick on

Hey Tyler whats good man? I really like the way you break down the lessons in the video. You don't drag it out and put people to sleep. I'm new to Ruby and I was wondering what can you do or typically do with the Ruby language? Is it specifically for games/ What else can be done with it?

Tyler on

I'm glad you like them! :) Ruby can be used for a lot of things. By far it's most popular use is web development on the server-side. See the rails framework, sinatra, and other libraries. You can make games with Ruby, or you can embed Ruby within an existing game engine written in C or C++, but really making games with Ruby is a much smaller community than web development is. Ruby is a general purpose languages, so people also use it for all kinds of other things, from building small utilities to larger applications. It's really up to what it is that you want to do :) Sorry it took me so long to reply to you!

Nasib on

you cant teach man

Tyler on

I'm sorry I can't teach. Do you have any advice on how I could improve?

Rod on

Good job on the lesson!