The Ruby Basics 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.

Jackie Syddall

David Pope

Scripting Languages 210-102

Ruby Basics for Beginners

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. He 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 this make 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 cars has attributes that apply to that particular animal. ( 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. The first step to begin using and programming is to install Ruby onto the machine in which we will be coding.

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 accept license agreement. Select the optional check boxes to get the full package and install Ruby into the default location. Click finish. Now we can verify that Ruby was installed by locating Ruby in start menu. Open the interactive Ruby link and a command window will open. Interactive Ruby is a program that you can write code and verify that the code is correct. Test out your 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, but learning the code and verifying the code you can run it in Interactive Ruby program that comes with the Ruby installation.

Computer can’t understand English they can only simple instruction. In Ruby programming these instructions are called commands. When we write a Ruby program we write many instructions for the computer each on its own line. A program is lines of commands that achieve the program goal. The first program that we will write is Hello World. 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 what you 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 if you were to 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 down we will look into the data types that are available within Ruby.

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. Using the previous sample "Hello World" everything within the quotes are 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 a literal. A double quote will import variable 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 are referred to as floats within Ruby programing language.

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 a 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.

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 equals symbol behind and a value or string. In the Interactive Ruby program lets set a couple of variables:

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

This will set x as 15 so that every time we call x 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 we call x the value of 10 will be returned. So we can do any math function on the variables to get different outputs.

Type x + y and =>25 is returned

Type x - y and => 5 is returned.

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.

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. Let’s try this in the Interactive Ruby program by creating a simple class for animals:

Type class Animal and press enter

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

Type end press enter and =>nil is returned

This creates a class of animals with the attributes name, age, and trait that will be used in describing the animal that we are creating. Now we need to create an object to be within the class by using a variable:

Type first_animal = Animal.new and press enter

Type first_animal.name = "Floyd" and press enter

Type first_animal.age = 12 and press enter

Type first_animal.trait = "loud" and press enter

Type puts first_animal.name and press enter and the output is Floyd

Type puts first_animal.trait and press enter and the output is loud

This creates the first animal in our class. Atributes are not the only thing that can be contained in a class. Class can also contain methods within Ruby



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