ashikajith@gmail.com
Okay..Lets dive into the Topic
What is Ruby & WHY ?
History of Ruby (walk through)
#For Mac and Windows Users
Try Ruby! (http://tryruby.org/)
Ruby in Twenty Minutes
(https://www.ruby-lang.org/en/documentation/quickstart/)
- Matz, creator of Ruby
puts "Hello World"
#=> "Hello World"
puts "Hello World"
#=> "Hello World"
puts 'Hello World'
#=> "Hello World"
' ' #=> To display as plain string
" " #=> For String manipulation
put "Hello #{ 5 + 3 } World"
#=> "Hello 7 World"
#irb console
2 + 2 #=> 4
2 + 3.14 #=> 5.14
2 - 3.14 #=> -1.1400000000000001
2 ** 100 #=> 1267650600228229401496703205376
# Instead of ++ and -- we use += and -=
x = 2
x += 1 #=> 3
x -= 1 #=> 2
#irb console
x = 4
x.class #=> Fixnum
x = 3.142857
x.class #=> Float
x = (2 ** 100)
x.class #=> Bignum
puts "I can inject variables here #{x} " #=> Use double quotes
#irb console
list = ['a','b','c','d']
list << 'e'
list.pop #=> "e"
list #=> ["a", "b", "c", "d"]
list.push 4 #=> ["a", "b", "c", "d", "4"]
#irb console
# Exclusive Range
numbers = (1...6)
alphabets = ('a' ... 'j')
range_set = ('abc' ... 'abz') #=> ?
# Inclusive Range
numbers = (1 .. 6)
alphabets = ('a' .. 'j')
Hash = {key => value}
hash = {1=> 'one', 2=> 'two', 3=>'three'}
hash[1] #=> 'One'
hash[2] #=> 'Two'
another_hash = { 'a' => 1, 'b' => 2, 'c' => 3 }
another_hash.keys #=> ["a", "b", "c"]
another_hash.values #=> [1, 2, 3]
A Hash is a collection of unique keys and values, Similar to arrays but can use any object type
#Can Have the values as hash instead of String
vehicles = { 'bikes' => {
'honda' => ['CB Uniconorn', 'Dream Yuga', 'Activa'],
'royal_enfield' => ['Bullet Classic', 'Deset Storm',
'Thunder bird']
},
'cars' => {
'Ford' => ['Ford Figo', 'Ford Classic']
}
}
vehicles['bikes']['royal_enfield'] #=> ['Bullet Classic', 'Deset Storm', 'Thunder bird']
if condition
#do something
elsif
#do something else
end
do_something if condition
unless condition
#do something
else
#do something else
Switch Statement
$age = 5
case $age
when 0 .. 2
puts "baby"
when 3 .. 6
puts "little child"
when 7 .. 12
puts "child"
when 13 .. 18
puts "youth"
else
puts "adult"
end
While
$i = 0
$num = 5
while $i < $num do
puts("Inside the loop i = #$i" )
$i +=1
end
For
for i in 0..5
puts "Value of local variable is #{i}"
end
array_or_range.each do |value|
puts value
end
array_or_range.map{|e| puts e }
3.times do
print 'ha'
end