Learning Ruby Programming : The Basic Intro

Jahnavi Sananse
5 min readJul 10, 2021

--

What is Ruby ?

Ruby is a pure object-oriented programming language. Ruby is most used for building web applications. Probably the most obvious implementation of Ruby is Rails web, the development framework built with Ruby.

Ruby is 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.

Let us write a simple program in ruby. All ruby files will have extension .rb. So, put the following source code in a example.rb file.

example.rb file :-

puts "Hello, I'm learning Ruby Programming!";

Now here, we assumed that you have Ruby interpreter available in your directory. Now, try to run this program like :-

$ ruby example.rb

This will produce the result like−

Hello, I'm learning Ruby Programming!

Ruby - Variables

In computer programming, a variable is a storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value, a variable is a container for different types of data like integer, float, string etc…

Assigning value to variables are like :-

for integer :-
syntax : variable_name = value
example : a = 5
for float :-
syntax : variable_name = value
example : a = 10.5
for string :-
syntax : variable_name = "value"
example : a = "abc"

Ruby - Types of variables

There are five types of variables.

1.Constants
2.Global Variable
3.Class Variable
4.Instance Variable
5.Local Varible
  1. Example of a constant declaration:
a = "This is my book."

2. Example of a global variable declaration: (use $ symbol)

$var = 'I love my-self'

3. Example of a class variable declaration: (use @@ symbol)

@@instances = 0

4. Example of an instance variable declaration: (use @ symbol)

@var = 'Ruby Programming'

5. Example of a local variable declaration:

Local variables are the most common variables you will come across and obey all scope boundaries. These variables are declared by starting the variable name with neither $ nor @, as well as not capitalizing the entire variable name.

var = 10

Getting data from User

One way to get information from the user is to call the ‘gets’ method.

‘gets’ stands for "get string". When you use it, the program waits for the user to 1) type in information and 2) press the enter key.

>> a = gets
demo
>> "demo\n"

After the code, a = gets, the computer waited for us to type in some information. We typed ‘demo’ and then pressed enter and the program returned "demo\n".

The \n at the end is the "newline" character and represents the enter key. But we don't want that as part of our string. We'll use chomp chained to gets to get rid of that - you can put .chomp after any string to remove the carriage return characters at the end.

>> a = gets.chomp
demo
>> "demo"

Example :-

>> puts 'Enter value for a : '
>> a = gets.chomp.to_i
>> puts 'Enter value for b: '
>> b = gets.chomp.to_i
>> c = a + b
>> puts "Sum : #{c}"
output :
Enter value for a :
10
Enter value for a :
20
Sum = 30

Methods

What is Method/Function ?

A method, like a function, is a set of instructions that perform a task. The difference is that a method is associated with an object, while a function is not.

In Ruby Method names should begin with a lowercase letter. If you begin a method name with an uppercase letter, Ruby might think that it is a constant and hence can parse the call incorrectly.

Methods should be defined before calling them, otherwise Ruby will raise an exception for undefined method invoking.

syntax :

def method_name 
expr..
end

Whenever you call the simple method, you write only the method name as follows −

method_name

example :-

def demo(a1 = "Ruby", a2 = "programming")
puts "This is #{a1}"
puts "This is #{a2}"
end
test "C", "C++"
test
output :-This is C
This is C++
This is Ruby
This is programming

Ruby - Date & Time

The Time class represents dates and times in Ruby.

Getting Current Date and Time :

example :-

time = Time.newputs "Current Time : " + time.inspect
puts time.year # => Year of the date
puts time.month # => Month of the date (1 to 12)
puts time.day # => Day of the date (1 to 31 )
puts time.wday # => 0: Day of week: 0 is Sunday
puts time.yday # => 365: Day of year
puts time.hour # => 23: 24-hour clock
puts time.min # => 59
puts time.sec # => 59
puts time.usec # => 999999: microseconds
puts time.zone # => "UTC": timezone name
output :-Current Time : Mon Jun 02 12:03:08 -0700 2008
2021
7
2
1
154
12
3
8
247476
UTC

Array

what is an Array ?

In Programming, An Array is a collection of different types of data into a single container or variable.

There are many ways to create or initialize an array.

One way is with the new class method −

names = Array.new

You can set the size of an array at the time of creating array −

names = Array.new(20)

You can assign a value to each element in the array as follows −

names = Array.new(4, "system")
puts "#{names}"
output :-["system", "system", "system", "system"]

You can also use a block with new,

nums = Array.new(10) { |e| e = e * 2 }
puts "#{nums}"
output :-[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

There is another method of Array, []. It works like this −

nums = Array.[](1, 2, 3, 4,5)

One more form of array creation is as follows −

nums = Array[1, 2, 3, 4,5]

Here, the method takes a range as an argument to create an array of digits −

digits = Array(0..9)
puts "#{digits}"
output :-[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

--

--

Jahnavi Sananse
Jahnavi Sananse

Written by Jahnavi Sananse

Story teller from my preliterate days. I write them down now.