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.
7. Hashes
• Hashes are unordered lists of values
• Hashes are indexed using “keys” which can
being either strings or symbols
8. Hashes
hash = {‘one’ => “One”, :two => ”Two”}
puts hash[‘one’]
One
puts hash[:two]
Three
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
20. Regular Expressions
• Regular Expressions are used for text parsing
and manipulation
• They can represent a sort of programming
language unto themselves
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. Blocks
def call_block
puts "Start of method"
yield
puts "End of method"
end
call_block { puts "In the block" }
Start of method
In the block
End of method
23. Iterators
• A type of code block that works with
successive values of some collection
• Used extensively for arrays and hashes
26. For Next Week
For the New to Programming
• Read Chapters 5, 6, 7 & 8 in LtP
• Complete exercises for each chapter
For Everyone
• Read Chapter 2, 7 & 9 in PR1.9
• Work on the Ruby Koans at a leisurely
pace