Sie sind auf Seite 1von 10

Lesson - 1

Introduction to Ruby
A dynamic, open source programming language with a focus on simplicity
and productivity. It has an elegant syntax that is natural to read and easy to
write. Ruby is also an object oriented language.

Installation

Installation on Windows
Installation on Mac

Installation on Windows:
To download Ruby, go to rubyinstaller.org/downloads
Download the latest version of Ruby. The one which is in the top most.
For 64 bit Operating Systems, download installer that end with (x64).
After downloading the exe file, double click to open the file.
Choose the language.

Click I accept the License.

Browse and select the desired installation path you want. But it is
recommended to install on the default directory. And Check the 3 options below

Install Td/k support

Add Ruby executables to your PATH

Associate .rb and .rbw files with Ruby installation

And click Install

You will see the installation of ruby in following screen.

Finally click Finish.

Installation on Mac:
Ruby has already installed on Macintosh.
So dont need to go through installation procedures.

Using the Ruby Interactive Shell:


It is also called as irb.
It allows us to type ruby expressions or statements.
Open the command prompt and type irb. (both Windows and Macintosh)

As you can see in the above screen, we can write ruby expressions and
statements.
The texts entered within quotes are called as String.
We can perform concatenation operation with two strings.
We can store the value to the variable like
name = Welcome to Ruby!!!
Ruby is a case sensitive programming language. So, name is different
from Name and NAME is different from both.
We can also perform some complicated operations. See the below screens.

This statements prints 1 to 9.

Whoa!! What just had happened? You might be confused after seeing this.
I said it prints 1 to 9. But its showing all 1s that too many. To get out of this,
press Ctrl + C
This is because the variable i is not incremented.
The following code prints from 1 to 9
i=1
while i < 10
print i

i += 1
end
This code prints from 1 to 9.
We can also define functions in irb.
A function is a set of statement(s) to perform a particular task.
Here is a function that performs square of a number.
def square(n)
return n * n
end
Calling the function: square(4)
After calling this function by passing 4 as argument, it returns 16 by
performing the operation 4 * 4.
Likewise, a function to find the area of a circle.
def circle(radius)
return radius * radius * 3.14
end
Calling the function: circle (5) by passing the argument as 5.
This function returns 78.5 by multiplying 5 * 5 * 3.14

Ruby Scripts
We are going to run ruby from a text editor rather that a Command
Prompt.
You can use any text editor like Notepad or Notepad++.
Ruby scripts have .rb extension. So, you have to save the file with the
extension .rb
Now, in your editor enter the following code and save it with any name
with .rb extension:
puts Welcome to Ruby!!!
Im saving this as first.rb. To run the program, go to Command Prompt,
and type ruby first.rb without quotes.
You can actually run by simply typing first.rb, because during installation,
you have checked Add Ruby executables to your PATH. So, its enough to type
first.rb for running the program.

Likewise, you can use any text editor in Macintosh and follow the same
procedures as in Windows.

Lesson - 2
Displaying Data
Now, we can see how to display data to the screen.
To display text, we have two functions
puts()
print()

puts adds a new line automatically to the data.


puts ("hello, world")
puts "hello, world"
You can use both the ways to display the data.
puts "hello, world", "Goodbye, world"

puts "practice", "makes", "a", "man", "perfect"

print doesnt adds a new line.


print "hello, world"
If you want to include new line at the end of the data using print method,
then you have to include special character \n

print "hello, world\n"


print "practice\n", "makes\n", "a\n", "man\n", "perfect\n"

You can also display the numeric data using these functions.
puts(1)
print(1)
Also you can evaluate the expression and display the result.
puts (2+2)
print(2+2)

Getting Data
To make the program to interact with user, we have to get the input data
from the users.
To do this, we can use gets() method.
gets() function gets input from the keyboard in string format and stores
the value in the variables.
name = gets
This statement gets a string input from the user and stores it in the
variable called name.
It also appends a \n new line character at the end of the input entered by
the user.

So, when you type the variable name, it displays what it is stored in it.
When you display using print function, it displays the text entered by the
user along with a new line.
If you want to remove the newline, you can use a function called chomp
The following statement removes the newline from the input entered by
the user.
print name.chomp
The following code performs addition of two whole numbers with the
numbers you have got from the user:
number1 = gets
number 2 = gets
(Integer)number1 + Integer(number2)
Integer() part of the code looks tricky for you now.
As said before gets method gets input in string format. So, you have to
convert it to whole number. This can be done using Integer() function.

If you dont convert those variables into integer and when you try to
perform addition, it concatenates two string. Do you remember concatenating
two strings which we have seen earlier in Chapter 1?
Likewise, to add two decimal numbers, look the following code:
num1 = gets
num2 = gets
Float(num1) + Float(num2)
Float() method is used to convert data into decimal format. If the
conversion is not done, it simply performs the concatenation operation as said
before.

Das könnte Ihnen auch gefallen