Saturday, January 16

Ruby 101 - Part 1

Some time back I made some Ruby notes while learning and so posting them here. I almost lost the pages, and before I loose to again for real, better to blog it, right? Below is the part 1 of the notes. Some basic concepts explained.

1) Install Ruby
>> ruby -v

2) For interactive ruby
>> irb

3) Create a new file in command prompt
>> notepad first.rb

4) Write your first program
puts ("Hello world")

5) Difference between print and puts
puts creates a new line after the string, print does not provide a new line

6) Get data from user using gets i.e input using gets
>> name = gets
>>"John"
=>"John\n"

gets gives a new line character i.e "\n". To remove "\n" we can use ruby function .chomp. gets receives all data as string. To use the numbers as Integers, we need to convert the string to integers i.e Integer(name) + Integer(name2)

7) Data types available in Ruby
a) string -> Can be created using single or double quotes
>> name = 'jane'
>> name = "Jane"

b) Numbers -> Can work with whole numbers, floats, binary, hexadecimal, negative numbers.

c) Array -> Store value in a structure. Range must have a sequence. Arrays can be just a collection of data.
>> [1,2,3,4,5]
>> names = ["John", "David"]
>>grades = [92, 84, 45, 87]

Access element of array. Use array name with offset/index i.e
>>grades[0]
=>92

>>grades[2]
=>84

Convert a range to array
>> digits = 0..9
>> nums = digits.to_a

d) Hashes -> Store collection of data. It is called associative structure where data is stored as key value pair.
>> number = {'David' => '52', 'John'=>'53'}
>> number['David']
=> '52'

>> number['Not exist']
=> nil


8) Local and Global variables
a) Local variable
number = 100

b) Global variable
$salary (provide a dollar sign)

c) Constants -> Variables hold same value throughout the program, constants are in uppercase.

Example program in ruby to find maximum number.
nums = []
i = 0
while i < 10
  num[i] = Random.rand(101)
  i+ =1
end
print (nums)
max = nums[0]
for i in 1..9
  if nums[i] > max
     max = nums[i]
 end
end
puts("Maximum value is : " + max.to_s )

9) Arithmetic operators
a) Addition
b) Subtraction
c) Multiplication
d) Division
e) Modulo (Returns the remainder of division) i.e 4%2 returns 0
f) Exponential operator 2%%4 returns 16

10) Relational operators
a) Operators for comparisons
1==1
1<2 p="">1<=2
1==10
2>1
1<=1

b) General comparison operator
<=>

c) Case statement
===

d) Logical operator
i) and &&
ii) or ||
iii) not !

eg. a=b=c=0
This is called chain assignment

eg. swapping without use of temp variable
a,b = b,a

11) If statement in Ruby

grade = gets
grade = Integer(grade)
if grade >= 70
   puts("pass")
else
   puts("Fail')
end 
12) Case statement
# case expression
# when exp1
#       statements
# when exp2
#       statements
# else
#       statements
# end

grade = gets
grade = Integer(grade)
case grade
   when 90..100
      letterGrade = "A"
   when 80..89
      letterGrade = "B"
   else
      letterGrade = "C"
end
puts(letterGrade)

13) While loop
# while comparision
#    statement
# end

count =1
while count <=5
   puts ("Hello world")
end

14) Until Loop
# until comparision
#   statement
# end

count = 1
until count > 5
  puts ("Hello world")
  count++
end
15) Iterator based loop
Traverse a range
5.times do
  puts ("Hello world")
end
1.upto (10) do |x|
   puts ("x")
end 
0.step (100,2) { |x| print x, " "}
sum=0
1.step(10,2) { |x| sum+=x }
print sum 

16) Each iterator
num = [1,2,3,4,6]
for num in nums
   print num, "\n"
end 

Altering loop control
eg, next, redo, break

Be the first one to Comment!!!

Post a Comment