ashikajith@gmail.com
#include <iostream>
int main()
{
std::cout << "hello world!\n";
return 0;
}
print "Hello World!"
print "Hello World!"
p "Hello World!"
5.times { print "Why so serious !!" }
source:https://www.ruby-lang.org/en/
History of Ruby (walk through)
History of Ruby (walk through)
#For Ubuntu
https://www.ruby-lang.org/en/documentation/installation/
https://gorails.com/setup/ubuntu/14.10(recommended)
#For Ubuntu
https://www.ruby-lang.org/en/documentation/installation/
https://gorails.com/setup/ubuntu/14.10(recommended)
#For Mac and Windows Users
- Matz, creator of Ruby
puts "Hello World"
#=> "Hello World"
my_input = gets #=> Accept the input as string
puts "Lets have some fun"
#=> "Lets have some fun"
# Reverse a string
# Capitalize (with !)
# Concatenate (+)
# Upcase
# Downcase
# Splitting
# gsub, sub
#irb console
2 + 2 #=> 4
2 + 7.14 #=> 9.14
2 - 5.12 #=> -3.12
2 ** 100 #=> 1267650600228229401496703205376
2 / 5 #=>
2 / 5.0 #=>
#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"]
# sort
# insert(pos, ary_elemnt)
# delete_at(index)
#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
end
#do somthing unless condition
Switch Statement
age = 5
case age
when 0 .. 2
puts "between 0 & 2"
when 3 .. 6
puts "between 3 & 6"
when 7 .. 12
puts "between 7 & 12"
when 13 .. 18
puts "between 13 & 18"
else
puts "No Idea"
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
3.times do
print 'ha'
end
array_or_range.map{|e| e }
array_or_range.map{|e| e*2 }
map with select odd or even
class Dog
def greet
'Woof!'
end
end
dog = Dog.new
dog.greet #=> 'Woof!'
class Robot
def initialize(name)
@name = name
end
def say_hello
"Hi, I'm #{@name}"
end
end
robo = Robot.new 'Eva'
robo.say_hello #=> 'Hi, I'm Eva.'
class Cat
@@sound = 'Meow' #Class variable
def greet
@@sound
end
end
garfield = Cat.new
garfield.greet #=> 'Meow!'
# Creating a plus method
2.plus 2 #=> 4
3.plus 5 #=> 8
# Override a reverse method
'Transform'.reverse #> 'Transform' or #some_other_msg
# What about Infinity
1/0 #=> Error or Infinity ?
#Initialize a hash with a list
Hash['key1', 'value1', 'key2', 'value2'] #=> {"key1"=>"value1", "key2"=>"value2"}
Hash[1,2,3,4,5,6] #=> {1=>2, 3=>4, 5=>6}
# Double pipes instead of if condition
a = nil
b = 12
a = a || b #=> ?
# Multiplication with Array
[1, 2, 3] * 3 #= [1, 2, 3, 1, 2, 3, 1, 2, 3]
# Array Flatten
[1,2,3,4,[6,7,8,9],11,12].flatten
%w(Tom Dick Harry)
%x(execute shell command)
%Q(dispaly string with double quotes)
Try Ruby! (http://tryruby.org/)
Ruby in Twenty Minutes
(https://www.ruby-lang.org/en/documentation/quickstart/)
Ruby Monk (https://rubymonk.com/)