Share
Embed
Ruby introduction for newbies
Ruby introduction for newbies. A presentation me & Sidu did at Bangalore Ruby user group meeting
Hemant Kumar
June 16, 2012
Transcript
110 slides
-
RUBY LANGUAGE Ruby is a object oriented programming language deeply
inspired by Smalltalk, Lisp & Perl. It is also a dynamic but strongly typed language. Saturday 16 June 12 -
STRONGLY TYPED Javascript 1 + "Hello" => "1Hello" Ruby 1
+ "Hello" => TypeError class Fixnum def +(other); "#{self}#{other}"; end end Saturday 16 June 12 -
CLASSES & OBJECTS Everything in Ruby is a object methods
classes lambdas integers Saturday 16 June 12 -
CLASSES class Car def initialize @name = "Ferrari" end def
say_name puts @name end end ferrari = Car.new() ferrari.say_name Saturday 16 June 12 -
CLASSES class Car def initialize @name = "Ferrari" end def
say_name puts @name end end ferrari = Car.new() ferrari.say_name Output: Ferrari Saturday 16 June 12 -
MODULES A Module is a collection of methods and constants.
Instance methods appear as methods in a class when the module is included. Saturday 16 June 12 -
MODULES module AnimalBehaviour def eat puts "Eating now" end end
class Dog include AnimalBehaviour end tommy = Dog.new() tommy.eat() Saturday 16 June 12 -
MODULES module AnimalBehaviour def eat puts "Eating now" end end
class Dog include AnimalBehaviour end tommy = Dog.new() tommy.eat() Output : Eating now Saturday 16 June 12 -
Car class Car def initialize @name = "Ferrari" end end
Saturday 16 June 12 -
Car Object class Car def initialize @name = "Ferrari" end
end Saturday 16 June 12 -
Car Object class Car def initialize @name = "Ferrari" end
end Saturday 16 June 12 -
Car Object BasicObject class Car def initialize @name = "Ferrari"
end end Saturday 16 June 12 -
Car Object BasicObject class Car def initialize @name = "Ferrari"
end end Saturday 16 June 12 -
Car Object BasicObject ferrari = Car.new class Car def initialize
@name = "Ferrari" end end Saturday 16 June 12 -
Car Object BasicObject ferrari = Car.new class Car def initialize
@name = "Ferrari" end end Saturday 16 June 12 -
Class Car Object BasicObject ferrari = Car.new class Car def
initialize @name = "Ferrari" end end Saturday 16 June 12 -
Class Car Object BasicObject ferrari = Car.new class Car def
initialize @name = "Ferrari" end end Saturday 16 June 12 -
Class Module Car Object BasicObject ferrari = Car.new class Car
def initialize @name = "Ferrari" end end Saturday 16 June 12 -
Class Module Car Object BasicObject ferrari = Car.new class Car
def initialize @name = "Ferrari" end end Saturday 16 June 12 -
Class Module Object Car Object BasicObject ferrari = Car.new class
Car def initialize @name = "Ferrari" end end Saturday 16 June 12 -
Class Module Object Car Object BasicObject ferrari = Car.new class
Car def initialize @name = "Ferrari" end end Saturday 16 June 12 -
Class Module Object BasicObject Car Object BasicObject ferrari = Car.new
class Car def initialize @name = "Ferrari" end end Saturday 16 June 12 -
Class Module Object BasicObject Car Object BasicObject ferrari = Car.new
class Car def initialize @name = "Ferrari" end end Saturday 16 June 12 -
Class Module Object BasicObject Car Object BasicObject ferrari = Car.new
class Car def initialize @name = "Ferrari" end end Saturday 16 June 12 -
Class Module Object BasicObject Car Object BasicObject ferrari = Car.new
class Car def initialize @name = "Ferrari" end end Saturday 16 June 12 -
Class Module Object BasicObject Car Object BasicObject ferrari = Car.new
class Car def initialize @name = "Ferrari" end end Saturday 16 June 12 -
RECAP module AnimalBehaviour def eat puts "Eating now" end end
class Dog end tommy = Dog.new() tommy.eat() Saturday 16 June 12 -
RECAP module AnimalBehaviour def eat puts "Eating now" end end
class Dog end tommy = Dog.new() tommy.eat() Class Saturday 16 June 12 -
RECAP module AnimalBehaviour def eat puts "Eating now" end end
class Dog end tommy = Dog.new() tommy.eat() Class Module Saturday 16 June 12 -
RECAP module AnimalBehaviour def eat puts "Eating now" end end
class Dog end tommy = Dog.new() tommy.eat() Class Module Object Saturday 16 June 12 -
RECAP module AnimalBehaviour def eat puts "Eating now" end end
class Dog end tommy = Dog.new() tommy.eat() Class Module Object BasicObject Saturday 16 June 12 -
RECAP module AnimalBehaviour def eat puts "Eating now" end end
class Dog end tommy = Dog.new() tommy.eat() Class Module Object BasicObject Saturday 16 June 12 -
RECAP module AnimalBehaviour def eat puts "Eating now" end end
class Dog end tommy = Dog.new() tommy.eat() Class Module Object BasicObject Saturday 16 June 12 -
RECAP module AnimalBehaviour def eat puts "Eating now" end end
class Dog end tommy = Dog.new() tommy.eat() Class Module Object BasicObject Saturday 16 June 12 -
RECAP module AnimalBehaviour def eat puts "Eating now" end end
class Dog end tommy = Dog.new() tommy.eat() Class Module Object BasicObject Saturday 16 June 12 -
RECAP module AnimalBehaviour def eat puts "Eating now" end end
class Dog end tommy = Dog.new() tommy.eat() Class Module Object BasicObject Saturday 16 June 12 -
OBJECT VS BASICOBJECT class Object < BasicObject include Kernel end
Saturday 16 June 12 -
VARIABLES module GameWorld class Planet def initialize(name) @name = name
end end class Alien @alien_count = 0 @@planet = Planet.new("Mars") def self.make_alien @alien_count += 1 alien end def position @position end end end alien = GameWorld::Alien.make_alien() Saturday 16 June 12 -
module GameWorld class Planet def initialize(name) @name = name end
def name @name end end class Alien @alien_count = 0 @@planet = Planet.new("Mars") def self.make_alien @alien_count += 1 alien end def position @position end end end alien = GameWorld::Alien.make_alien() Saturday 16 June 12 -
module GameWorld class Planet def initialize(name) @name = name end
def name @name end end class Alien @alien_count = 0 @@planet = Planet.new("Mars") def self.make_alien @alien_count += 1 alien end def position @position end end end alien = GameWorld::Alien.make_alien() earth = Planet.new("Earth") earth.name Saturday 16 June 12 -
module GameWorld class Planet def initialize(name) @name = name end
def name @name end end class Alien @alien_count = 0 @@planet = Planet.new("Mars") def self.make_alien @alien_count += 1 alien end def position @position end end end alien = GameWorld::Alien.make_alien() earth = Planet.new("Earth") earth.name Output : Earth Saturday 16 June 12 -
module GameWorld class Planet def initialize(name) @name = name end
end class Alien @alien_count = 0 @@planet = Planet.new("Mars") def self.make_alien @alien_count += 1 alien end def self.alien_count @alien_count end def position @position end end end alien = GameWorld::Alien.make_alien() Saturday 16 June 12 -
module GameWorld class Planet def initialize(name) @name = name end
end class Alien @alien_count = 0 @@planet = Planet.new("Mars") def self.make_alien @alien_count += 1 alien end def self.alien_count @alien_count end def position @position end end end alien = GameWorld::Alien.make_alien() Alien.alien_count Saturday 16 June 12 -
module GameWorld class Planet def initialize(name) @name = name end
end class Alien @alien_count = 0 @@planet = Planet.new("Mars") def self.make_alien @alien_count += 1 alien end def self.alien_count @alien_count end def position @position end end end alien = GameWorld::Alien.make_alien() Alien.alien_count Output : 1 Saturday 16 June 12 -
module GameWorld class Planet def initialize(name) @name = name end
end class Alien @alien_count = 0 @@planet = Planet.new("Mars") def self.make_alien @alien_count += 1 alien end def self.planet @@planet end def position @position end end end alien = GameWorld::Alien.make_alien() Saturday 16 June 12 -
module GameWorld class Planet def initialize(name) @name = name end
end class Alien @alien_count = 0 @@planet = Planet.new("Mars") def self.make_alien @alien_count += 1 alien end def self.planet @@planet end def position @position end end end alien = GameWorld::Alien.make_alien() Alien.planet Saturday 16 June 12 -
module GameWorld class Planet def initialize(name) @name = name end
end class Alien @alien_count = 0 @@planet = Planet.new("Mars") def self.make_alien @alien_count += 1 alien end def self.planet @@planet end def position @position end end end alien = GameWorld::Alien.make_alien() Alien.planet Output : Planet(“Mars”) Saturday 16 June 12 -
DUCK TYPING class Cow def milkit; end end class Goat
def milkit; end end Saturday 16 June 12 -
DUCK TYPING def collect_milk(animal) if animal.respond_to?(:milkit) animal.milkit else raise InvalidArgument.new("Cant
milk #{animal.inspect}") end end class Cow def milkit; end end class Goat def milkit; end end Saturday 16 June 12 -
class EnglishTest def total_score end end class MathTest def total_score
end end test.total_score Saturday 16 June 12 -
HASH student_scores = { hemant: {math: 10, science: 20, total:
30}, kiran: {math: 20, science: 30, total: 50}, sidu: {math: 20, science: 30, total: 50} } Saturday 16 June 12 -
def scrap_pirate_bay(response) scrap_output = {} scrap_output[:title] = response.title ? response.title
: "Pirates Rule" scrap_output[:body] = response.body scrap_output end Saturday 16 June 12 -
def scrap_pirate_bay(response) scrap_output = {} scrap_output[:title] = response.title ? response.title
: "Pirates Rule" scrap_output[:body] = response.body scrap_output end Saturday 16 June 12 -
def scrap_pirate_bay(response) scrap_output = {} scrap_output[:title] = response.title ? response.title
: "Pirates Rule" scrap_output[:body] = response.body scrap_output end Saturday 16 June 12 -
def scrap_pirate_bay(response) scrap_output = {} scrap_output[:title] = response.title ? response.title
: "Pirates Rule" scrap_output[:body] = response.body scrap_output end Use a Class Saturday 16 June 12 -
OMFG performance and garbage collector What is wrong with hashes?
Saturday 16 June 12 -
KNOW THY DATA STRUCTURES fruits = ["apple","orange","apple"] fruits.uniq fruits =
Set.new() fruits += ["apple","orange","apple"] Saturday 16 June 12 -
KNOW THY DATA STRUCTURES fruits = ["apple","orange","apple"] fruits.uniq fruits =
Set.new() fruits += ["apple","orange","apple"] Output : [“apple”,”orange”] Saturday 16 June 12 -
IMO in Rails - DHH did a mistake by putting
all the module, controller, helpers in global scope Saturday 16 June 12 -
I know why it exists and I am using them
with ActiveRecord scopes. But can never think why will my own method need a lambda. Saturday 16 June 12 -
fortune_cookie = lambda { stocks = ['GROUPON','FACEBOOK','ZYNGA'] puts "You should
invest in #{stocks[rand(3)]}" end fortune_cookie.call() Saturday 16 June 12 -
fortune_cookie = lambda { stocks = ['GROUPON','FACEBOOK','ZYNGA'] puts "You should
invest in #{stocks[rand(3)]}" end fortune_cookie.call() You should invest in GROUPON Saturday 16 June 12 -
YOU CAN PASS THEM AS ARGUMENTS class MailParser def parse(filename,mailer)
mail_object = do_some_hocus_pocus(filename) mailer.call(mail_object) ensure filename.close() end end mail_parser = MailParser.new() mail_parser.parse("foo.mail", lambda { |mail| mail.send() }) Saturday 16 June 12 -
AS ANONYMOUS FUNCTION def shoot(target,gun, crouch_flag = false) return true
if target.dead? loop { if gun.has_bullets? dead_yet = target.shoot(gun,crouch_flag) if dead_yet break end else gun.reload end } end Saturday 16 June 12 -
AS ANONYMOUS FUNCTION def shoot(target,gun, crouch_flag = false) shoot_algo =
lambda do return false if gun.has_bullets? target.shoot(gun,crouch_flag) end return true if target.dead? loop { x = shoot_algo.call() x ? break : gun.reload } end def shoot(target,gun, crouch_flag = false) return true if target.dead? loop { if gun.has_bullets? dead_yet = target.shoot(gun,crouch_flag) if dead_yet break end else gun.reload end } end Saturday 16 June 12 -
class MailParser def parse(filename) mail_object = do_some_hocus_pocus(filename) yield mail_object ensure
filename.close() end end mail_parser = MailParser.new() mail_parser.parse("foo.mail") {|mail| mail.send } Saturday 16 June 12 -
class MailParser def parse(filename) mail_object = do_some_hocus_pocus(filename) yield mail_object ensure
filename.close() end end mail_parser = MailParser.new() mail_parser.parse("foo.mail") {|mail| mail.send } Saturday 16 June 12 -
0 22.5 45 67.5 90 Newbie Can hold my ground
Expert Guru Meta Programming Usage Saturday 16 June 12 -
0 22.5 45 67.5 90 Newbie Can hold my ground
Expert Guru Meta Programming Usage You know nothing John Snow Saturday 16 June 12 -
0 22.5 45 67.5 90 No Understand Kinda Understand Public
Enemy Guru Meta Programming Usage Saturday 16 June 12 -
0 22.5 45 67.5 90 No Understand Kinda Understand Public
Enemy Guru Meta Programming Usage You know nothing John Snow Saturday 16 June 12