ruby 101
TRANSCRIPT
Hi, I’m Harisankar P S
Known in the world wide web as @coderhs
Cochin, India
I program in Ruby and Rails for living :)
Kerala Ruby User Group@keralarbhttp://krug.github.io
A group of people who loves working, learning and talking about Ruby.
We have been active for the last three years.
We meet every 3rd sunday
So thank you guys for hosting the meetup for April, 2015.
Yukihiro Matsumoto aka Matz
He created ruby in 1993 Released to the public is 1995. First public version was 0.95
It was developed in Japan.
First english mailing list was created in 1999
Numbers2 + 2 #=> 4
2 + 3.15 #=> 5.14
2 ** 100 #=> 1267650600228229401496703205376
# The largest number ruby can handle it the largest # number your system can store
What about data types?
• Everything in Ruby is an Object
• 2.class #=> Fixnum
• 3.14.class #=> Float
• (2**100).class #=> Bignum
Variables
x = 1 # x is an integer (class FixNum)
x = x + 1.5 # x is now a Float
x = 'Ruby' # Now a string
puts "Hello, #{x}!" #=> 'Hello, Ruby!'
Arrays
items = [1, 2, 3]
items << 'a' #=> [1, 2, 3, ‘a']
x = items.pop #=> 'a'
items #=> [1, 2, 3]
items.push 4 #=> [1, 2, 3, 4]
Hashesperson = {
name: 'Matz',
languages: ['C', 'Python', 'Ruby']
}
# OR
person = { 'Matz' }
person[:languages] = ['C', 'Python', 'Ruby']
Blocksdef foo
puts 'Before yield'
yield
puts 'After yield'
end
foo { puts 'Inside the block' }
#=> Before yield
#=> Inside the block
#=> After yield
Instance variablesclass Dog
def name=(n)
@name = n
end
def greet
"Woof, I'm #{@name}"
end
end
dog = Dog.new
dog.name = 'Scooby'
Constructorsclass Dog
def initialize(name)
@name = name
end
def greet
"Woof, I'm #{@name}"
end
end
dog = Dog.new 'Scooby'
dog.greet #=> 'Woof! I'm Scooby.'
Class variablesclass Cat
@@sound = 'Meow'
def greet
@sound
end
end
garfield = Cat.new
garfield.greet #=> 'Meow!'
Inheritanceclass Dog < Animal
def greet
"Hello, I'm #{@name}! Woof, woof!"
end
end
goofy = Dog.new('Goofy')
goofy.greet #=> "Hello, I'm Goofy! Woof, woof!"
Modules - extendmodule Speech
def greeting
'Hello'
end
end
class Dog
extend Speech
end
Dog.greeting #=> 'Hello'
Modules - includemodule Speech
def greet
'Hello'
end
end
class Dog
include Speech
end
dog = Dog.new
dog.greet #=> 'Hello'
method_missingclass Dog
def method_missing(method_name, *args)
"Ï don't understand!"
end
end
scooby = Dog.new
scooby.hello #=> "I don't understand!"
method_missingclass Dog
def method_missing(method_name, *args)
if method_name.to_s =~ /^say_(.*)/
"Dog says: #{$1}"
end
end
end
scooby = Dog.new
scooby.hello #=> nil
scooby.say_hello #=> 'Dog says: hello'
Websites
• rubymonk.com
• rubylearning.com
• tryruby.org
creditsThis presentation is built using information from
• Ruby 101 - Nithin Bekal (http://nithinbekal.com/slides/ruby-101)
• Lets Learn Ruby (http://www.slideshare.net/aquarianboy/lets-learn-ruby-basic)
• Ruby Basics (http://www.slideshare.net/Robertdennyson/ruby-basics)