Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby introduction for newbies

Ruby introduction for newbies

Ruby introduction for newbies. A presentation me & Sidu did at Bangalore Ruby user group meeting

Hemant Kumar

June 16, 2012
Tweet

More Decks by Hemant Kumar

Other Decks in Programming

Transcript

  1. 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
  2. STRONGLY TYPED Javascript 1 + "Hello" => "1Hello" Ruby 1

    + "Hello" => TypeError class Fixnum def +(other); "#{self}#{other}"; end end Saturday 16 June 12
  3. CLASSES & OBJECTS Everything in Ruby is a object methods

    classes lambdas integers Saturday 16 June 12
  4. 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
  5. 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
  6. 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
  7. MODULES module AnimalBehaviour def eat puts "Eating now" end end

    class Dog include AnimalBehaviour end tommy = Dog.new() tommy.eat() Saturday 16 June 12
  8. 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
  9. Car Object BasicObject ferrari = Car.new class Car def initialize

    @name = "Ferrari" end end Saturday 16 June 12
  10. Car Object BasicObject ferrari = Car.new class Car def initialize

    @name = "Ferrari" end end Saturday 16 June 12
  11. Class Car Object BasicObject ferrari = Car.new class Car def

    initialize @name = "Ferrari" end end Saturday 16 June 12
  12. Class Car Object BasicObject ferrari = Car.new class Car def

    initialize @name = "Ferrari" end end Saturday 16 June 12
  13. Class Module Car Object BasicObject ferrari = Car.new class Car

    def initialize @name = "Ferrari" end end Saturday 16 June 12
  14. Class Module Car Object BasicObject ferrari = Car.new class Car

    def initialize @name = "Ferrari" end end Saturday 16 June 12
  15. Class Module Object Car Object BasicObject ferrari = Car.new class

    Car def initialize @name = "Ferrari" end end Saturday 16 June 12
  16. Class Module Object Car Object BasicObject ferrari = Car.new class

    Car def initialize @name = "Ferrari" end end Saturday 16 June 12
  17. Class Module Object BasicObject Car Object BasicObject ferrari = Car.new

    class Car def initialize @name = "Ferrari" end end Saturday 16 June 12
  18. Class Module Object BasicObject Car Object BasicObject ferrari = Car.new

    class Car def initialize @name = "Ferrari" end end Saturday 16 June 12
  19. Class Module Object BasicObject Car Object BasicObject ferrari = Car.new

    class Car def initialize @name = "Ferrari" end end Saturday 16 June 12
  20. Class Module Object BasicObject Car Object BasicObject ferrari = Car.new

    class Car def initialize @name = "Ferrari" end end Saturday 16 June 12
  21. Class Module Object BasicObject Car Object BasicObject ferrari = Car.new

    class Car def initialize @name = "Ferrari" end end Saturday 16 June 12
  22. RECAP module AnimalBehaviour def eat puts "Eating now" end end

    class Dog end tommy = Dog.new() tommy.eat() Saturday 16 June 12
  23. RECAP module AnimalBehaviour def eat puts "Eating now" end end

    class Dog end tommy = Dog.new() tommy.eat() Class Saturday 16 June 12
  24. RECAP module AnimalBehaviour def eat puts "Eating now" end end

    class Dog end tommy = Dog.new() tommy.eat() Class Module Saturday 16 June 12
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. DUCK TYPING class Cow def milkit; end end class Goat

    def milkit; end end Saturday 16 June 12
  43. 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
  44. 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
  45. 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
  46. 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
  47. 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
  48. 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
  49. KNOW THY DATA STRUCTURES fruits = ["apple","orange","apple"] fruits.uniq fruits =

    Set.new() fruits += ["apple","orange","apple"] Saturday 16 June 12
  50. KNOW THY DATA STRUCTURES fruits = ["apple","orange","apple"] fruits.uniq fruits =

    Set.new() fruits += ["apple","orange","apple"] Output : [“apple”,”orange”] Saturday 16 June 12
  51. IMO in Rails - DHH did a mistake by putting

    all the module, controller, helpers in global scope Saturday 16 June 12
  52. 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
  53. fortune_cookie = lambda { stocks = ['GROUPON','FACEBOOK','ZYNGA'] puts "You should

    invest in #{stocks[rand(3)]}" end fortune_cookie.call() Saturday 16 June 12
  54. 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
  55. 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
  56. 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
  57. 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
  58. 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
  59. 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
  60. 0 22.5 45 67.5 90 Newbie Can hold my ground

    Expert Guru Meta Programming Usage Saturday 16 June 12
  61. 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
  62. 0 22.5 45 67.5 90 No Understand Kinda Understand Public

    Enemy Guru Meta Programming Usage Saturday 16 June 12
  63. 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