ruby brownbag yesware oct 2014

16
Ruby methods Featuring: cats

Upload: linh-bui

Post on 22-Mar-2017

91 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Ruby brownbag Yesware Oct 2014

Ruby methods Featuring: cats

Page 2: Ruby brownbag Yesware Oct 2014

public class Cat { public static int getNumLegs() { return 4; } public void eat() { System.out.println("om nom nom"); } public static void main(String[] args) { Cat happyCat = new Cat(); happyCat.eat(); }}

Page 3: Ruby brownbag Yesware Oct 2014

class Cat def self.number_of_paws # identity crisis 4 endend

Page 4: Ruby brownbag Yesware Oct 2014

class Cat; end

happy_cat = Cat.new

def happy_cat.bark puts "woof" end

happy_cat.bark # woof

Page 5: Ruby brownbag Yesware Oct 2014

class Cat; end

happy_cat = Cat.new

class << happy_cat def bark puts "woof" endend

Page 6: Ruby brownbag Yesware Oct 2014

class SomeClass class << self def class_method_1 end

def class_method_2 end endend

Page 7: Ruby brownbag Yesware Oct 2014

class Cat def self.number_of_paws 4 endend

Page 8: Ruby brownbag Yesware Oct 2014
Page 9: Ruby brownbag Yesware Oct 2014

module CatStuff def eat puts "eating the cat way" endend

class Cat include CatStuffend

Page 10: Ruby brownbag Yesware Oct 2014

module CatClassStuff def num_of_legs 4 endend

class Cat extend CatClassStuffend

Page 11: Ruby brownbag Yesware Oct 2014

module CatStuff def bark puts "woof" endend

class Cat; end

happy_cat.include(CatStuff)

Page 12: Ruby brownbag Yesware Oct 2014
Page 13: Ruby brownbag Yesware Oct 2014

module CatStuff def bark puts "woof" endend

class Cat; end

happy_cat.extend(CatStuff)happy_cat.bark# woof

Page 14: Ruby brownbag Yesware Oct 2014
Page 15: Ruby brownbag Yesware Oct 2014
Page 16: Ruby brownbag Yesware Oct 2014

class C; end

C.class_eval do def d puts "d" endend

C.dC.new.d

class C; end

C.instance_eval do def d puts "d" endend

C.dC.new.d