Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Learn Ruby 2011 - Session 3

910 views

Published on

In our third session we covered the second half of our language crash course. This session addresses Arrays, Hashes, Symbols, Control Structures, Regular Expressions, Blocks and Iterators.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Learn Ruby 2011 - Session 3

  1. 1. Learn Ruby 2011 Session 3
  2. 2. Our SponsorsFlatsourcing, iSeatz, Koda, and Launchpad
  3. 3. Language Crashing Learning the basics in pieces
  4. 4. QuestionsHaving any problems? Discover anything cool?
  5. 5. Arrays• An Array is an ordered list of values• Arrays in Ruby are zero-indexed
  6. 6. Arraysarray = [“One”,”Two”,”Three”]puts array[0]Onearray.each { |v| puts v }OneTwoThree
  7. 7. Hashes• Hashes are unordered lists of values• Hashes are indexed using “keys” which can being either strings or symbols
  8. 8. Hasheshash = {‘one’ => “One”, :two => ”Two”}puts hash[‘one’]Oneputs hash[:two]Three
  9. 9. Symbols• Symbols are essentially constants that don’t need to be declared ahead of time• A Symbol has a simple string-like representation and is guaranteed to have a unique value
  10. 10. Symbolsdef walk(direction) if direction == :north # ... endend
  11. 11. Symbols• Symbols are most commonly found as the keys for hashes• They are slightly more memory efficient than using strings for the same purposes
  12. 12. Control Structures• Control structures allow you to handle conditional behaviors• Control structures allow you to handle iterations/loops
  13. 13. Control Structures• if ... else ... elsif• while• case ... when• for ... in• until• unless
  14. 14. Control Structuresif i == 3 # ...elsif i == 4 # ...else # ...end
  15. 15. Control Structuresunless i == 3 # ...end
  16. 16. Control Structureswhile i < 3 # ...end
  17. 17. Control Structuresuntil i < 3 # ...end
  18. 18. Control Structurescase i when 3 # ... when 4 # ... else # ...end
  19. 19. Control Structuresfor i in 1..3 # ...endfor i in [‘one’,‘two’,‘three’] # ...end
  20. 20. Regular Expressions• Regular Expressions are used for text parsing and manipulation• They can represent a sort of programming language unto themselves
  21. 21. Blocks• Sections of code that can be passed to methods like parameters• Represented as code within curly braces or between do and end
  22. 22. Blocksdef call_block puts "Start of method" yield puts "End of method"endcall_block { puts "In the block" }Start of methodIn the blockEnd of method
  23. 23. Iterators• A type of code block that works with successive values of some collection• Used extensively for arrays and hashes
  24. 24. Iterators[‘One’,‘Two’,‘Three’].each do |val| puts valendOneTwoThree
  25. 25. Reviewing• Arrays & Hashes• Symbols• Control Structures• Regular Expressions• Blocks & Iterators
  26. 26. For Next WeekFor the New to Programming• Read Chapters 5, 6, 7 & 8 in LtP• Complete exercises for each chapterFor Everyone• Read Chapter 2, 7 & 9 in PR1.9• Work on the Ruby Koans at a leisurely pace
  27. 27. Next Week• Deeper into Classes & Variables
  28. 28. Exercises• Visit http://rubykoans.com/ & Download• Begin working through the Koans by running `ruby path_to_enlightenment.rb`•

×