Interactive Ruby For Beginners

Print   

02 Nov 2017

Disclaimer:
This essay has been written and submitted by students and is not an example of our work. Please click this link to view samples of our professional work witten by our professional essay writers. Any opinions, findings, conclusions or recommendations expressed in this material are those of the authors and do not necessarily reflect the views of EssayCompany.

Programing is fun! Well for certain people that is true and for others it is not. Ruby was developed by a programmer, software engineer, and open source evangelist Yukihiro "Matz" Matsumoto. Ruby is strongly based on Yukihiro’s experiences with Perl and Python. Matz was interested in creating a language that focused on productivity and having fun while doing it. Ruby is a blended language that balanced the functionality of programming with individualist programming into one language. In 1995 Yukihiro released Ruby free of charge to use, copy modify, and distribute. (Ruby-lang.org, 2006) One may ask what type of language is Ruby?

Ruby is a programming language that is interpreted and is fully object-oriented. Interpreted language means that Ruby will use source code through the Ruby interpreter to output, running line by line until completion. To Ruby everything is an object and this makes the language fully object-oriented. Object-oriented is what we do in normal life and can be seen everywhere. For example we have a class called Cars, and within this class we would have different kinds of cars. To take it a step further each kind of car has attributes that apply to that particular vehicle. ( ie. model, size, color and year etc…) This is the blueprint that describes objects in the real world and within Ruby. This function of Ruby will be discussed later within the tutorial. The first step to begin using and programming is to install Ruby onto the machine in which coding will take place.

Windows version of Ruby is available for download at http://rubyinstaller.org/ by clicking the download link. Click the most resent as long as it is not in beta. Next run the executable file and agree to run by accepting the license agreement. Select the optional check boxes to get the full package and install Ruby into the default location. Finally click finish. Next verify that Ruby was installed by locating Ruby in the start menu. Open the interactive Ruby link and a command window will open. Interactive Ruby is a program that allows the user to write code and verify that the code is correct. Test out the installation by typing 2+12 and verify that the code returns => 14. This will verify that the installation was successful. The next step is to start with a very simple program. Ruby code can be written in a text editor and saved to disk then run in a command window by typing ruby filename.rb. Learning the and verifying code that can run is better illustrated by using the Interactive Ruby program that comes with the Ruby installation. The Interactive program will teach you the proper format of commands that you can use and catch any errors in syntax. Makes learning Ruby more fun!

Computers can’t understand English they can only follow simple instruction. In Ruby programming these instructions are called commands. When program is written in Ruby many instructions for the computer are written, each on its own line. A program is lines of commands that achieve the program goal. The first program addressed here is Hello World. First open Interactive Ruby window and type print "Hello World" then press enter. This very simple programs output in the Interactive Ruby window will be Hello World =>nil. The output is enclosed within the Parentheses with =>nil at the end. The =>nil literally means nothing is wrong and the code is what Ruby expected to achieve the outcome of printing Hello World to the screen. Ruby has another command that would achieve the same results; type the command puts "Hello World" the output would be the same except for a line break between Hello World and the =>nil in the Window. With this simple command accomplished, next item to address is the data types that are available within Ruby. (Roberts, 2012)1.jpg

Within Ruby there are two data types that are available, String and Numbers. String data types are an object that represents text that can be read by the computer and used in the program. Using the previous sample "Hello World" everything within the quotes is our String data type. You can use either the single quote or double quote to be the delimiter for your text. A single quote will not interpret any variables that you place within them and will be read literally. A double quote will import variables or other programing functions that they contain. The other data types are numbers which can be Integers or a Floating point numbers. Ruby also breaks Integers into two classes; there are fixnum which are smaller numbers and bignum which are huge numbers. Floating point numbers are numbers that contain a decimal point and arereferred to as floats within Ruby programing language. (Holland, 2007)

With the number data types Ruby can do simple math functions. This is shown in the interactive Ruby program:

Type 4+9 and press enter => 13 is returned.

Type 32-14 and press enter => 18 is returned.

Type 4*6 and press enter => 24 is returned.

Type 9/3 and press enter the output is => 3.

The data type that was input in the whole number and return a basic whole number to the output. Now type in 13/4 the output as => 3 and not what was expected by the user. That is because Ruby was reading the whole number or int as input and then returned an int number as the output. To correct a programmer would have to let Ruby know by calling for a float. float.jpg

Type 13.0/4.0 and press enter => 3.25 returned

Data types are what you would place in a variable, a place holder for the data to be recalled later within a program. Setting variables is Ruby is as simple as just naming them and putting the = symbol behind and a value or string. In the Interactive Ruby program let’s set a couple of variables:

Type x = 15 and press enter => 20 is returned.

This will set x as 15 so that every time x is called the value of 15 will be returned.

Type y = 10 and press enter => 10 is returned.

This will set y as 10 so that every time x is called the value of 10 will be returned. So math functions are available on the variables to get different outputs.

Type x + y and =>25 is returned

Type x - y and => 5 is returned. var.jpg

Variables can also be string data and the syntax is the same variablename = "Hello World". This will set the variable as having the value of the text Hello World. (Roberts, 2012)

Classes are the blueprint of the objects that are to be used in the program. Every object has attributes that can be used to describe or distinguish between other objects. Next ty creating a simple class for animal in Interactive Ruby program:

Type class Animal and press enter (Note that the class name is always typed with a capital letter)

Type attr_accessor :name, :age, :trait and press enter

Type end press enter and =>nil is returned

This set of commands creates a class of animals with the attributes name, age, and trait that will be used in describing the animal that is being created. Creating an object to be within the class by using a variables to set the animals attributes works as follows:

Type first_animal = Animal.new and press enter Type first_animal.name = "Bagel" and press enter

Type first_animal.age = 2 and press enter

Type first_animal.trait = "playful" and press enter

Type puts first_animal.name and press enter computer recalls animal name as Bagel

Type puts first_animal.trait and press enter computer recalls animal trait as playful

This creates the first animal in our class and allows for the variables saved to be recalled within the program. Classes can inherit attributes from another class; if you want to create different classes from a template you would do the following.

Type class Animal and press enter

Type attr_accessor :name, :age, :trait and press enter

Type end press enter and =>nil is returned

Type class Dog < Animal and press enter

Type end press enter and =>nil is returned

Type class Cat < Animal and press enter

Type end press enter and =>nil is returned

Type class Fish < Animal and press enter

Type end press enter and =>nil is returned

This creates the original Animal class with the attributes of name, age and trait. By using the less than sign when creating the subsequent classes of Dog, Cat, and Fish will assign the same attributes from the Animal class to that class. This saves time as to not have to type attributes that will be associated with the class. Now if we would need an additional attribute for fish called type we could add it by simply rewrite the Fish class but before end you would add the line attr_accessor :type before entering the end command as displayed below:

Notice that if the programmer tries to add a type to dog that an error was produced and when we added it to the fish we were able to recall the data. (Roberts, 2012)

Attributes are not the only thing that can be contained in a class. Class can also contain methods are something that the within Ruby. A method should be defined within a class and will be something that the object is capable of doing. Try the following:

Type class Dog and press enter

Type def bark and press enter

Type puts "Woof, Woof, Woooooofffff!" press enter

Type end press enter to end the definition of the method

Type end press enter and =>nil is returned ending the definition of the class

Type bagle = Dog.new and press enter

Type bagle.bark press enter and Woof, Woof, Wooooofffff! is returned

Ruby offers a unique way to find out what class objects belong to. The syntax for finding out what class bagle is part of is bagle.class which as you could of guessed returned => Dog. (Roberts, 2012)

Matz has created an object programing language that is elegant and functional. That being said the language is complex and cannot be completely learned through a mere look into establishing classes, objects and method. Hopefully the concept of objects was conveyed in this lesson. With Ruby’s blend of Perl and Python languages for an experience programmer can help them be productive with their programs. Ruby being fully object-oriented and the straight forward approach to creating objects will assist in make learning Ruby programing a little easier to understand. There are several other location one can learn of Ruby and a great source is the ‘Little Book of Ruby’ by Huw Collingbourne and can be downloaded from the following URL http://www.sapphiresteel.com/IMG/pdf/LittleBookOfRuby.pdf.

Huw also has several well put together videos on line that can be reverenced. The key to Ruby is to always remember "Programing can be fun!"



rev

Our Service Portfolio

jb

Want To Place An Order Quickly?

Then shoot us a message on Whatsapp, WeChat or Gmail. We are available 24/7 to assist you.

whatsapp

Do not panic, you are at the right place

jb

Visit Our essay writting help page to get all the details and guidence on availing our assiatance service.

Get 20% Discount, Now
£19 £14/ Per Page
14 days delivery time

Our writting assistance service is undoubtedly one of the most affordable writting assistance services and we have highly qualified professionls to help you with your work. So what are you waiting for, click below to order now.

Get An Instant Quote

ORDER TODAY!

Our experts are ready to assist you, call us to get a free quote or order now to get succeed in your academics writing.

Get a Free Quote Order Now