cycle (MOTM 2010.07)

695 views
605 views

Published on

Ruby Method of the Month 2010.07. Lightning talk for the Columbus Ruby Brigade.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
695
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide


















  • cycle (MOTM 2010.07)

    1. 1. cycle Method of the Month Kevin Munc @muncman
    2. 2. cycle It cycles through the values fed to it, returning one value each invocation, over and over.
    3. 3. ActionView::Helpers::TextHelper
    4. 4. Console Setup irb --> require 'active_support' ==> true irb --> require 'action_view' ==> ["ActionView"] irb --> class Object >> include ActionView::Helpers::TextHelper >> end ==> Object
    5. 5. Simple Example > items = (1..6).to_a ==> [1, 2, 3, 4, 5, 6] > items.each { |i| puts "#{i}: #{cycle('odd','even')}" } 1: odd 2: even 3: odd 4: even 5: odd 6: even
    6. 6. With More Elements > items.each do |i| puts "#{i}: #{cycle('odd','even','chunky','bacon')}" end 1: odd 2: even 3: chunky 4: bacon 5: odd 6: even
    7. 7. Mo’ Methods, Mo’ Examples reset_cycle current_cycle irb --> set_one = [0,1,2] ==> [0, 1, 2] irb --> set_two = %w{a b c} ==> ["a", "b", "c"]
    8. 8. Multiple Cycles w/ Naming set_one.each do |s1| prefix = cycle('even','odd', :name => 'outer') set_two.each do |s2| suffix = cycle('yin','yang', :name => 'inner') puts "#{prefix} #{s1} #{s2} #{suffix}" end end
    9. 9. Multiple Cycles w/ Naming set_one.each do |s1| even 0 a yin prefix = cycle('even','odd', :name => 'outer') even 0 b yang set_two.each do |s2| even 0 c yin suffix = cycle('yin','yang', :name => 'inner') odd 1 a yang puts "#{prefix} #{s1} #{s2} #{suffix}" odd 1 b yin end odd 1 c yang end even 2 a yin even 2 b yang even 2 c yin
    10. 10. Restarting Named Cycles set_one.each do |s1| prefix = cycle('even','odd', :name => 'outer') set_two.each do |s2| suffix = cycle('yin','yang', :name => 'inner') puts "#{prefix} #{s1} #{s2} #{suffix}" end reset_cycle('inner') end
    11. 11. Restarting Named Cycles set_one.each do |s1| even 0 a yin prefix = cycle('even','odd', :name => 'outer') even 0 b yang set_two.each do |s2| even 0 c yin suffix = cycle('yin','yang', :name => 'inner') odd 1 a yin puts "#{prefix} #{s1} #{s2} #{suffix}" odd 1 b yang end odd 1 c yin reset_cycle('inner') even 2 a yin end even 2 b yang even 2 c yin
    12. 12. reset_cycle('inner') reset_cycle('outer')
    13. 13. Get the cycle w/out advancing? set_one.each do |s1| puts "#{s1} - #{cycle('on','off')}" puts "#{s1} - #{cycle('on','off')}" end
    14. 14. Get the cycle w/out advancing? set_one.each do |s1| 0 - on puts "#{s1} - #{cycle('on','off')}" 0 - off puts "#{s1} - #{cycle('on','off')}" 1 - on end 1 - off 2 - on 2 - off
    15. 15. Get the cycle w/out advancing? set_one.each do |s1| 0 - on puts "#{s1} - #{cycle('on','off')}" 0 - off puts "#{s1} - #{cycle('on','off')}" 1 - on end 1 - off 2 - on Nope. 2 - off
    16. 16. Get the cycle w/out advancing? set_one.each do |s1| puts "#{s1} - #{cycle('on','off')}" puts "#{s1} - #{current_cycle}" end
    17. 17. Get the cycle w/out advancing? set_one.each do |s1| 0 - on puts "#{s1} - #{cycle('on','off')}" 0 - on puts "#{s1} - #{current_cycle}" 1 - off end 1 - off 2 - on 2 - on
    18. 18. Get the cycle w/out advancing? set_one.each do |s1| 0 - on puts "#{s1} - #{cycle('on','off')}" 0 - on puts "#{s1} - #{current_cycle}" 1 - off end 1 - off 2 - on Yep. 2 - on
    19. 19. Another Example set_one.each do |s1| cycle('ruby','rails','bacon', :name => 'outer') set_two.each do |s2| puts "Inner: #{cycle('C','R','B', :name => 'inner')} and Outer is still #{current_cycle('outer')}" end end
    20. 20. Another Example set_one.each do |s1| cycle('ruby','rails','bacon', :name => 'outer') set_two.each do |s2| puts "Inner: #{cycle('C','R','B', :name => 'inner')} and Outer is still #{current_cycle('outer')}" end Inner: C and Outer is still ruby end Inner: R and Outer is still ruby Inner: B and Outer is still ruby Inner: C and Outer is still rails Inner: R and Outer is still rails Inner: B and Outer is still rails Inner: C and Outer is still bacon Inner: R and Outer is still bacon Inner: B and Outer is still bacon
    21. 21. The cycle method In Rails (not Array#cycle) Often used to ‘zebra stripe’ table rows Supported by: reset_cycle current_cycle cycle names (default is ‘default’)
    22. 22. RRBO.com uses many methods!
    23. 23. Don’t forget to check out rrbo.com ! Questions? Photo Credits http://www.flickr.com/photos/falconmotorcycles/3556349741/ http://www.flickr.com/photos/falconmotorcycles/3556350229/ http://www.flickr.com/photos/falconmotorcycles/3556345789/ http://www.flickr.com/photos/falconmotorcycles/3557161910/ http://www.flickr.com/photos/balakov/3494949386/

    ×