Stepping to Ruby
Hi I'm Ashik Ajith
ashikajith@gmail.com
Okay..Lets dive into the Topic
Purpose
- Getting Started with Ruby
- Having Fun with Ruby (Hope so...)
- Not a Tutorial :-(
What is Ruby & WHY ?
- Ruby is a Dynamic programming language
- Purely OOP Concept
- Influenced by Python, Perl, Lisp and Small Talk
- Focus on simplicity and productivity
- Created by Yukihiro Matsumoto
History of Ruby (walk through)
- Aim to create a OO Scripting Language
-
A language that is more powerful than Perl, and more object-oriented than Python
-
Code-named after a gemstone's name (àla Perl)
- Released in 1995(v 0.95) -
- Current Version - Ruby (v2.2.3)
Install Ruby
#For Mac and Windows Users
To Get Started.. ( Add screnshots)
-
Try Ruby! (http://tryruby.org/)
-
Ruby in Twenty Minutes
(https://www.ruby-lang.org/en/documentation/quickstart/)
"Programming languages must feel natural to programmers."
- Matz, creator of Ruby
Lets Say "Hello" to Ruby
puts "Hello World"
#=> "Hello World"
Ruby Strings
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"
Ruby With Numbers
#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
Ruby With Data types
#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
Ruby With Arrays
#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"]
Ruby With Ranges
#irb console
# Exclusive Range
numbers = (1...6)
alphabets = ('a' ... 'j')
range_set = ('abc' ... 'abz') #=> ?
# Inclusive Range
numbers = (1 .. 6)
alphabets = ('a' .. 'j')
Ruby With Hashes
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
Ruby With Hashes (Contd..)
#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']
Ruby With Conditions
if condition
#do something
elsif
#do something else
end
do_something if condition
unless condition
#do something
else
#do something else
Ruby With Conditions
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
Ruby With Conditions
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
Ruby With Iterations
array_or_range.each do |value|
puts value
end
array_or_range.map{|e| puts e }
3.times do
print 'ha'
end
Copy of deck
By Ashik Ajith
Copy of deck
- 922