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

Ruby is Magic: Cupcakes

Ruby is Magic: Cupcakes

This episode is mainly about the use of enumerators in Ruby. The slides are in German but the code snippets maybe useful to you.

Copyright Notice: My Little Pony - Friendship is Magic is Property of Hasbro and The Hub.

Dirk Breuer

November 21, 2012
Tweet

More Decks by Dirk Breuer

Other Decks in Programming

Transcript

  1. Struct.new("Pony", :name, :type) # => Struct::Pony applejack = Struct::Pony.new("Applejack", "Earth

    Pony") # => #<struct Struct::Pony name="Applejack", type="Earth Pony">
  2. class end # => Pony applejack = Pony.new("Applejack", "Earth Pony")

    # => #<struct Pony name="Applejack", type="Earth Pony"> Pony < Struct.new(:name, :type)
  3. class Pony < MyStruct.new(:name, :type) end rainbow_dash = Pony.new("Rainbow Dash",

    "Pegasus") # => #<Pony:0x007f995d083150 @name="Rainbow Dash", @type="Pegasus">
  4. class Pony attr_reader :name, :street, :street_number, :zip, :city def initialize(opts)

    @name = opts[:name] @street = opts[:street] @street_number = opts[:street_number] @zip = opts[:zip] @city = opts[:city] end end
  5. class Pony attr_reader :name, :address Address = Struct.new(:street, :street_number, :zip,

    :city) def initialize(opts) @name = opts[:name] @address = Address.new(opts[:street], opts[:street_number], opts[:zip], opts[:city]) end end
  6. require 'ostruct' config = OpenStruct.new config.environment = "development" config.cache_store =

    "redis://127.0.0.1:6379" config.logger = STDOUT config.time_zone = "Europe/Berlin" p config # => #<OpenStruct environment="development", # cache_store="redis://127.0.0.1:6379", # logger=#<IO:<STDOUT>>, # time_zone="Europe/Berlin">
  7. names.next # Fluttershy names.next # Applejack names.next # Rainbow Dash

    names.next # Pinkie Pie names.next # Twighlight Sparkle names.next # Rarity
  8. sequence = Enumerator.new do |yielder| number = 0 loop do

    number += 1 yielder.yield number end end
  9. … names.next # Pinkie Pie names.next # Twighlight Sparkle names.next

    # Rarity names.next sequence.rb:46:in `next': iteration reached an end (StopIteration) ! from sequence.rb:46:in `<main>'
  10. class List def initialize(elements) @elements = elements end def each(&block)

    build_each(@elements.to_enum, &block) end def build_each(enum) while true begin next_value = enum.next yield next_value rescue StopIteration => iterator return iterator.result end end end end list = List.new [1,2,3,4] p list.each { |i| puts i }
  11. class List def initialize(elements) @elements = elements end def each(&block)

    build_each(@elements.to_enum, &block) end def build_each(enum) loop do yield enum.next end enum.to_a # Needed to make both versions equivalent end end list = List.new [1,2,3,4] p list.each { |i| puts i }
  12. sequence = Enumerator.new do |yielder| number = 0 loop do

    number += 1 yielder.yield number end end Endlosschleife?! What in tarnation?
  13. require "fiber" sequence = Fiber.new do number = 0 loop

    do number += 1 Fiber.yield number end end def sequence.next resume end
  14. class Sequencer def initialize @start = 0 end def each

    loop do @start += 1 yield @start end end end
  15. seq = Sequencer.new.to_enum ten_above_100 = [] while true n =

    seq.next ten_above_100 << n if n > 100 break if ten_above_100.size == 10 end p ten_above_100 # => [101, 102, 103, 104, 105, 106, 107, 108, 109, 110]
  16. seq = Sequencer.new.to_enum p seq.lazy.select { |x| x > 100

    }.take(10).to_a # => [101, 102, 103, 104, 105, 106, 107, 108, 109, 110]
  17. Structs sind praktisch, um “Domain Concepts” zu verpacken Wir benutzen

    ständig Sequenzen in Ruby Wenn erforderlich, können wir uns leicht infinite Sequenzen bauen
  18. ✓Alle Getränke inklusive ✓Professionelle Reinigung ✓Hochwertige Büromöbel ✓100 Mbit Breitband-Internet

    ✓Nutzung von Drucker etc. ✓Erstklassiger Kaffee * inkl. 19% UmSt für pro Monat 359 € *
  19. Thanks! Q & A? Dirk Breuer / @railsbros_dirk Sebastian Cohnen

    / @tisba ? “My Little Pony” © Hasbro Studios and DHX Media Vancouver rubyismagic.de
  20. class Pony def say_hello(a_binding = nil) eval('puts "Hello, my name

    is #{@name}"', a_binding || binding) end end somepony = Pony.new somepony.say_hello # Hello, my name is @name = "Rainbow Dash" somepony.say_hello(binding) # Hello, my name is Rainbow Dash @name = "Applejack" somepony.say_hello(binding) # Hello, my name is Applejack
  21. require 'erb' class View def friend "Pinkie Pie" end def

    get_binding binding end end template = ERB.new <<-ERB Welcome to Ruby is Magic! Let me introduce you to my good friend <%= friend %>. ERB puts template.result # (erb):3:in `<main>': undefined local variable or method `friend' for main:Object # from /lib/ruby/1.9.1/erb.rb:838:in `eval' # from /lib/ruby/1.9.1/erb.rb:838:in `result' # from Ruby is Magic.rb/011-clip-show/code/erb_demo.rb:19:in `<main>' puts template.result(View.new.get_binding) # Let me introduce you to my good friend Pinkie Pie.