SlideShare uma empresa Scribd logo
1 de 23
cycle




Method of the Month
    Kevin Munc
    @muncman
cycle
It cycles through the values
   fed to it, returning one
value each invocation, over
           and over.
ActionView::Helpers::TextHelper
Console Setup
irb --> require 'active_support'
   ==> true

irb --> require 'action_view'
   ==> ["ActionView"]

irb --> class Object
>>       include ActionView::Helpers::TextHelper
>>       end
   ==> Object
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
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
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"]
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
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
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
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
reset_cycle('inner')
reset_cycle('outer')
Get the cycle w/out advancing?
 set_one.each do |s1|
  puts "#{s1} - #{cycle('on','off')}"
  puts "#{s1} - #{cycle('on','off')}"
 end
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
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
Get the cycle w/out advancing?
 set_one.each do |s1|
  puts "#{s1} - #{cycle('on','off')}"
  puts "#{s1} - #{current_cycle}"
 end
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
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
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
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
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’)
RRBO.com uses many methods!
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/

Mais conteúdo relacionado

Semelhante a cycle (MOTM 2010.07)

circ.db.dbcircleserver(1).py#!usrlocalbinpython3im.docx
circ.db.dbcircleserver(1).py#!usrlocalbinpython3im.docxcirc.db.dbcircleserver(1).py#!usrlocalbinpython3im.docx
circ.db.dbcircleserver(1).py#!usrlocalbinpython3im.docx
christinemaritza
 
Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeBuilding Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at Stripe
MongoDB
 
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)Py4inf 05-iterations (1)
Py4inf 05-iterations (1)
karan saini
 

Semelhante a cycle (MOTM 2010.07) (18)

make simple neural network python
make simple neural network pythonmake simple neural network python
make simple neural network python
 
circ.db.dbcircleserver(1).py#!usrlocalbinpython3im.docx
circ.db.dbcircleserver(1).py#!usrlocalbinpython3im.docxcirc.db.dbcircleserver(1).py#!usrlocalbinpython3im.docx
circ.db.dbcircleserver(1).py#!usrlocalbinpython3im.docx
 
Cuke4Duke JavaZone 2009
Cuke4Duke JavaZone 2009Cuke4Duke JavaZone 2009
Cuke4Duke JavaZone 2009
 
Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeBuilding Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at Stripe
 
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)Py4inf 05-iterations (1)
Py4inf 05-iterations (1)
 
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)Py4inf 05-iterations (1)
Py4inf 05-iterations (1)
 
Py4inf 05-iterations
Py4inf 05-iterationsPy4inf 05-iterations
Py4inf 05-iterations
 
Ruby closures, how are they possible?
Ruby closures, how are they possible?Ruby closures, how are they possible?
Ruby closures, how are they possible?
 
TechShift: There’s light beyond LAMP
TechShift: There’s light beyond LAMPTechShift: There’s light beyond LAMP
TechShift: There’s light beyond LAMP
 
Learning Timed Automata with Cypher
Learning Timed Automata with CypherLearning Timed Automata with Cypher
Learning Timed Automata with Cypher
 
A Lifecycle Of Code Under Test by Robert Fornal
A Lifecycle Of Code Under Test by Robert FornalA Lifecycle Of Code Under Test by Robert Fornal
A Lifecycle Of Code Under Test by Robert Fornal
 
Time brings all things to pass
Time brings all things to passTime brings all things to pass
Time brings all things to pass
 
OpenWorld 2018 - Common Application Developer Disasters
OpenWorld 2018 - Common Application Developer DisastersOpenWorld 2018 - Common Application Developer Disasters
OpenWorld 2018 - Common Application Developer Disasters
 
λ | Lenses
λ | Lensesλ | Lenses
λ | Lenses
 
Nested Loops
Nested LoopsNested Loops
Nested Loops
 
The Ring programming language version 1.5.2 book - Part 177 of 181
The Ring programming language version 1.5.2 book - Part 177 of 181The Ring programming language version 1.5.2 book - Part 177 of 181
The Ring programming language version 1.5.2 book - Part 177 of 181
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
Tokyo APAC Groundbreakers tour - The Complete Java Developer
Tokyo APAC Groundbreakers tour - The Complete Java DeveloperTokyo APAC Groundbreakers tour - The Complete Java Developer
Tokyo APAC Groundbreakers tour - The Complete Java Developer
 

Mais de Kevin Munc

Mais de Kevin Munc (12)

Ci for i-os-codemash-01.2013
Ci for i-os-codemash-01.2013Ci for i-os-codemash-01.2013
Ci for i-os-codemash-01.2013
 
NaN, Zero, & Infinities
NaN, Zero, & InfinitiesNaN, Zero, & Infinities
NaN, Zero, & Infinities
 
Basic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setIntervalBasic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setInterval
 
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
 
Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)
 
empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)
 
Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)
 
Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)
 
Removing Methods (MOTM 2010.01)
Removing Methods (MOTM 2010.01)Removing Methods (MOTM 2010.01)
Removing Methods (MOTM 2010.01)
 
gsub (MOTM 2009.09)
gsub (MOTM 2009.09)gsub (MOTM 2009.09)
gsub (MOTM 2009.09)
 
The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)
 
Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 

cycle (MOTM 2010.07)

  • 1. cycle Method of the Month Kevin Munc @muncman
  • 2. cycle It cycles through the values fed to it, returning one value each invocation, over and over.
  • 4. Console Setup irb --> require 'active_support' ==> true irb --> require 'action_view' ==> ["ActionView"] irb --> class Object >> include ActionView::Helpers::TextHelper >> end ==> Object
  • 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. 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. 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. 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. 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. 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. 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
  • 13. Get the cycle w/out advancing? set_one.each do |s1| puts "#{s1} - #{cycle('on','off')}" puts "#{s1} - #{cycle('on','off')}" end
  • 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. 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. Get the cycle w/out advancing? set_one.each do |s1| puts "#{s1} - #{cycle('on','off')}" puts "#{s1} - #{current_cycle}" end
  • 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. 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. 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. 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. 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. RRBO.com uses many methods!
  • 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/

Notas do Editor