Slide 1

Slide 1 text

RUBY Hemant & Sidu Saturday 16 June 12

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

ABOUT DYNAMIC LANGUAGES Dynamically Typed. Dynamic Language. Saturday 16 June 12

Slide 4

Slide 4 text

STRONGLY TYPED Javascript 1 + "Hello" => "1Hello" Ruby 1 + "Hello" => TypeError class Fixnum def +(other); "#{self}#{other}"; end end Saturday 16 June 12

Slide 5

Slide 5 text

Javascript collect_milk([cow, hen,python]) Ruby collect_milk([cow, hen,python]) Saturday 16 June 12

Slide 6

Slide 6 text

CLASSES & OBJECTS Everything in Ruby is a object methods classes lambdas integers Saturday 16 June 12

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

CLASS HIERARCHY Saturday 16 June 12

Slide 13

Slide 13 text

Saturday 16 June 12

Slide 14

Slide 14 text

class Car def initialize @name = "Ferrari" end end Saturday 16 June 12

Slide 15

Slide 15 text

Car class Car def initialize @name = "Ferrari" end end Saturday 16 June 12

Slide 16

Slide 16 text

Car Object class Car def initialize @name = "Ferrari" end end Saturday 16 June 12

Slide 17

Slide 17 text

Car Object class Car def initialize @name = "Ferrari" end end Saturday 16 June 12

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

RECAP Saturday 16 June 12

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

OBJECT VS BASICOBJECT class Object < BasicObject include Kernel end Saturday 16 June 12

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

http://www.ruby-doc.org/core-1.9.3/BasicObject.html Ruby 1.8 does not have BasicObject Metaclass Saturday 16 June 12

Slide 56

Slide 56 text

DUCK TYPING Saturday 16 June 12

Slide 57

Slide 57 text

DUCK TYPING class Cow def milkit; end end class Goat def milkit; end end Saturday 16 June 12

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

Embrace Duck typing Saturday 16 June 12

Slide 60

Slide 60 text

def collect_milk(animal) if animal.is_a?(Cow) animal.milkit end end Saturday 16 June 12

Slide 61

Slide 61 text

Saturday 16 June 12

Slide 62

Slide 62 text

def collect_milk(animal) if animal.respond_to?(:milkit) animal.milkit else raise InvalidArgument.new("Cant milk #{animal.inspect}") end end Saturday 16 June 12

Slide 63

Slide 63 text

EMBRACE POLYMORPHISM Saturday 16 June 12

Slide 64

Slide 64 text

def total_score(test) if test.is_a?(EnglishTest) test.subjective_score elsif test.is_a?(MathTest) test.total_marks end end Saturday 16 June 12

Slide 65

Slide 65 text

class EnglishTest def total_score end end class MathTest def total_score end end test.total_score Saturday 16 June 12

Slide 66

Slide 66 text

NAH I AM NOT THAT DUMB Saturday 16 June 12

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

USE BUT DON’T ABUSE HASH Saturday 16 June 12

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

Saturday 16 June 12

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

OMFG performance and garbage collector What is wrong with hashes? Saturday 16 June 12

Slide 75

Slide 75 text

You Mad Bro? Saturday 16 June 12

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

DEFINE YOUR CLASSES INSIDE MODULES Saturday 16 June 12

Slide 79

Slide 79 text

module Leman class Ferrari end class Maruti800 end end Saturday 16 June 12

Slide 80

Slide 80 text

IMO in Rails - DHH did a mistake by putting all the module, controller, helpers in global scope Saturday 16 June 12

Slide 81

Slide 81 text

Saturday 16 June 12

Slide 82

Slide 82 text

Saturday 16 June 12

Slide 83

Slide 83 text

LAMBDAS Saturday 16 June 12

Slide 84

Slide 84 text

I DON’T KNOW WHY THEY EVEN EXISTS! Saturday 16 June 12

Slide 85

Slide 85 text

Saturday 16 June 12

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

fortune_cookie = lambda { stocks = ['GROUPON','FACEBOOK','ZYNGA'] puts "You should invest in #{stocks[rand(3)]}" end fortune_cookie.call() Saturday 16 June 12

Slide 88

Slide 88 text

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

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

AS ANONYMOUS FUNCTION Saturday 16 June 12

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

BLOCKS & LAMBDAS Saturday 16 June 12

Slide 94

Slide 94 text

BROTHERS IN ARMS Saturday 16 June 12

Slide 95

Slide 95 text

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

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

http://eli.thegreenplace.net/2006/04/18/understanding-ruby- blocks-procs-and-methods/ Saturday 16 June 12

Slide 98

Slide 98 text

EAT THEM FOR BREAKFAST & LUNCH Saturday 16 June 12

Slide 99

Slide 99 text

META- PROGRAMMING Saturday 16 June 12

Slide 100

Slide 100 text

Saturday 16 June 12

Slide 101

Slide 101 text

0 22.5 45 67.5 90 Newbie Can hold my ground Expert Guru Meta Programming Usage Saturday 16 June 12

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

0 22.5 45 67.5 90 No Understand Kinda Understand Public Enemy Guru Meta Programming Usage Saturday 16 June 12

Slide 104

Slide 104 text

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

Slide 105

Slide 105 text

http://pragprog.com/screencasts/v-dtrubyom/the-ruby-object- model-and-metaprogramming Saturday 16 June 12

Slide 106

Slide 106 text

ENGINEERING IS NOT OPTIONAL Saturday 16 June 12

Slide 107

Slide 107 text

DESIGN IS NOT JUST FOR DESIGNERS Saturday 16 June 12

Slide 108

Slide 108 text

OO IS NOT JUST FOR JAVA PROJECTS Saturday 16 June 12

Slide 109

Slide 109 text

SE IS ABOUT PEOPLE Saturday 16 June 12

Slide 110

Slide 110 text

STUDY PEOPLE STUDY PRODUCTIVITY Saturday 16 June 12