Steady with ruby
-
Upload
christopher-spring -
Category
Software
-
view
116 -
download
0
description
Transcript of Steady with ruby
![Page 2: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/2.jpg)
![Page 3: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/3.jpg)
1. {}, proc{}, ->{}
2. meta programming
3. modules
![Page 4: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/4.jpg)
1) blocks,!procs and!lambdas
![Page 5: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/5.jpg)
(1..3).each do |i| puts i end # => 1 # => 2 # => 3
(1..3).each{ |i| puts i }
![Page 6: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/6.jpg)
(1..3).each do |i| puts i end # => 1 # => 2 # => 3
(1..3).each{ |i| puts i }
![Page 7: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/7.jpg)
def each for i in collection yield i end end
![Page 8: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/8.jpg)
def some_method(*arguments) # ... pre conditions yield( some_result ) # ... post conditions end
![Page 9: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/9.jpg)
def some_method(*arguments, &block) # ... pre conditions block.call( some_result ) # ... post conditions end
![Page 10: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/10.jpg)
file = File.new('credit_cards.txt', 'w') file.puts 'XXXX XXXX XXXX 5534' file.close
File.new('credit_cards.txt', 'w') do |file| file.puts 'XXXX XXXX XXXX 5534' end
Vs.
![Page 11: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/11.jpg)
proc{} vs lambda{}
![Page 12: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/12.jpg)
p = proc{ |i| puts "proc #{i}" } p.call("hey!") # => proc hey! !!!!l = lambda{ |i| puts "lamb #{i}" } l.call('yo!') # => lamb yo!
![Page 13: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/13.jpg)
p = proc{ |i| puts "proc #{i}" } l = lambda{ |i| puts "lamb #{i}" } !(1..3).each(&p) # => proc 1 # => proc 2 # => proc 3 !(1..3).each(&l) # => lamb 1 # => lamb 2 # => lamb 3
![Page 14: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/14.jpg)
def do_something(a_p, b_l, &block) print a_p.call() print block.call() print b_l.call() end !!p = proc{ "I'm a " } l = lambda{ " and I'm ok!" } !!do_something(p, l){ "lumberjack" } # => I'm a lumberjack and I'm ok!
![Page 15: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/15.jpg)
p.class # => Proc p.inspect # => “#<Proc:0x0000010194d8b8@(irb):97>" !!l.class # => Proc l.inspect # => "#<Proc:0x0000010190cd68@(irb):98 (lambda)>"
![Page 16: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/16.jpg)
p = proc{|i| puts i} p.call() # => nil !p.call(1) # => 1 !p.call(1, 2, 3) # => 1
![Page 17: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/17.jpg)
l = lambda{|i| puts i} l.call() # ArgumentError: wrong number of arguments (0 for 1) !l.call(1) # => 1 !l.call(1, 2, 3) # ArgumentError: wrong number of arguments (3 for 1)
![Page 18: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/18.jpg)
def proc_return p = proc{ return ‘Never ' } p.call 'Always ' end !!def lambda_return l = lambda{ return 'eat your vegetables!'} l.call 'give up!' end !!"#{proc_return} #{lambda_return}"
![Page 19: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/19.jpg)
Never give up!
![Page 20: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/20.jpg)
![Page 21: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/21.jpg)
2) META PROGRAMMING!
![Page 22: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/22.jpg)
send :<3
![Page 23: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/23.jpg)
class Statistics def initialize(account) @account = account end ! def increment_counts(metric, by=1) case metric when :sent @account.increment_sent_count(by) when :viewed @account.increment_viewed_count(by) when :bounced @account.increment_bounced_count(by) end end end
![Page 24: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/24.jpg)
class Statistics def initialize(account) @account = account end ! def increment_counts(metric, by=1) method = "increment_#{metric}_count" @account.send(method, by) end end
![Page 25: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/25.jpg)
class Account # ... private def schedule_billing # ... end end !class BillingRunner def initialize(account) @account end ! def run! # ... @account.send(:schedule_billing) # ... end end
![Page 26: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/26.jpg)
class Account # ... private def schedule_billing # ... end end !class BillingRunner def initialize(account) @account end ! def run! # ... @account.send(:schedule_billing) # ... end end
![Page 27: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/27.jpg)
define_method :awesome {}
![Page 28: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/28.jpg)
class User def admin! @role = :admin end ! def admin? @role == :admin end ! def client! @role = :client end ! def client? @role == :client end end
![Page 29: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/29.jpg)
class User ROLES = %i(admin client) ! ROLES.each do |role| define_method "#{role}?" do @role == role end ! define_method "#{role}!" do @role = role end end end
![Page 30: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/30.jpg)
%i(admin client supervisor technician vendor manager)
![Page 31: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/31.jpg)
method_missing m, *args, &b
![Page 32: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/32.jpg)
class KeyValueStore def initialize(store={}) @store = store end ! def insert(key, value) @store[key] = value end end !store = KeyValueStore.new store.respond_to?(:insert) # => true !store.insert(:one, 1) !m = store.method(:insert) m.call(:two, 2) !store.instance_variable_get('@store') # => {one: 1, two: 2}
![Page 33: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/33.jpg)
class KeyValueStore def initialize(store={}) @store = store end ! def insert(key, value) @store[key] = value end end !store = KeyValueStore.new store.respond_to?(:insert) # => true !store.insert(:one, 1) !m = store.method(:insert) m.call(:two, 2) !store.instance_variable_get('@store') # => {one: 1, two: 2}
![Page 34: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/34.jpg)
class KeyValueStore # ... ! def method_missing(method_name, *args, &block) if @store.respond_to?(method_name) @store.send(method_name, *args, &block) else super end end ! def respond_to_missing?(method_name, include_private) @store.respond_to?(method_name) || super end end
![Page 35: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/35.jpg)
kv = KeyValueStore.new kv.insert(:three, 3) !kv.respond_to?(:include?) # => true kv.include?(:three) # => true !m = kv.method(:include?) m.call(:four) # => false !kv.keys # => [:three] kv.values # => [3]
![Page 36: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/36.jpg)
![Page 37: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/37.jpg)
3) Modules
![Page 38: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/38.jpg)
module Mathematics class Plane # ... end end !!module Airport class Plane # ... end end
![Page 39: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/39.jpg)
module TehForce def sense '... a disturbance in the force' end end !class Person include TehForce # ... end !
p = Person.new p.sense # => "... a disturbance in the force"
![Page 40: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/40.jpg)
Person = Class.new() p = Person.new !!p.sense # NoMethodError: undefined method `sense' … !!p.extend TehForce p.sense # => "... a disturbance in the force"
![Page 41: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/41.jpg)
module UserDianostics def perform_diagnostics check_counts validate_dates find_orphans end ! # .... end !user = User.find(some_id) user.extend UserDianostics user.perform_diagnostics
![Page 42: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/42.jpg)
module RepublicPersonnel def storm_trooper Person.new end end !!class CloningVat extend RepublicPersonnel end !!CloningVat.storm_trooper # => #<Person:0x0000010187c150>
![Page 43: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/43.jpg)
Person = Class.new() CloningVat = Class.new() !!CloningVat.storm_trooper # => NoMethodError: undefined method `storm_trooper' … !!CloningVat.extend RepublicPersonnel CloningVat.storm_trooper # => #<Person:0x0000010187c150>
![Page 44: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/44.jpg)
include and extend?
![Page 45: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/45.jpg)
module SomeMixin module ClassMethods # ... end module InstanceMethods # ... end def self.included(receiver) receiver.extend ClassMethods receiver.send :include, InstanceMethods end end
![Page 46: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/46.jpg)
A contrived example…
![Page 47: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/47.jpg)
admin = Administrator.new admin.allowed_to_toggle_alarm? # => true admin.allowed_to_visit_facebook? # => false !lacky = Lacky.new lacky.allowed_to_be_seen? # => true lacky.allowed_to_be_heard? # => false lacky.allowed_to_make_eye_contact? # => false
![Page 48: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/48.jpg)
class Administrator include Permissions ! can :toggle_alarm end !admin = Administrator.new admin.allowed_to_toggle_alarm? # => true admin.allowed_to_visit_facebook? # => false
![Page 49: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/49.jpg)
class Lacky include Permissions ! can :be_seen end !lacky = Lacky.new lacky.allowed_to_be_seen? # => true lacky.allowed_to_be_heard? # => false
![Page 50: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/50.jpg)
module Permissions module ClassMethods def can(do_something) define_method "allowed_to_#{do_something}?" do true end end end ! module InstanceMethods def method_missing(*args, &block) method_name = args[0] if method_name && method_name =~ /allowed_to_.*\?/ false else super end end end ! def self.included(receiver) receiver.extend ClassMethods receiver.send :include, InstanceMethods end end
![Page 51: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/51.jpg)
![Page 52: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/52.jpg)
Thanks forListening
![Page 53: Steady with ruby](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bc3c6b4c90530298b530a/html5/thumbnails/53.jpg)
Questions?