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 }
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 }
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
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.