Data Types, Expressions, Control Flow and Unit Tests
You will explore these topics with a partner.
Lesson: Data Types
Review: Ruby Data Types
On a piece of paper:
- Write a line of Ruby code that illustrates that arrays are heterogeneous
- Write a line of Java code that highlights the fact that Java arrays are homogeneous.
- Write a line of code that illustrates that ArrayList in Java can be heterogeneous
Symbols are ubiquitous in Ruby, but can be confusing at first. Read:
- http://www.troubleshooters.com/codecorn/ruby/symbols.htm#_What_are_symbols
- http://www.randomhacks.net/2007/01/20/13-ways-of-looking-at-a-ruby-symbol/
Add to your paper:
- Explain the statement “A Ruby symbol is an object with O(1) comparison”.
- Name one common usage for a symbol
Do a quick Google-search to explore: How do Ruby’s symbol compare to C++
enums? to Java enums?
Lesson: Expressions
Review: Ruby Expressions. From the Ruby demo files you downloaded in the Intro exercise, take a look at ruby_expressions.rb.
Self Test
Explore, no need to record answers, but DO answer every question!
- Does Ruby have overloaded operators? Is there any difference between puts
a[0]
and putsa.[](0)
? - What is the effect of:
a.[]=(0, 5)
? - What is an lvalue?
- How does parallel assigment work?
- Are constants really constant in Ruby?
- What does it mean to be right associative?
- What is a programming idiom?
- What is short-circuit evaluation?
- What can you do with the splat?
- In a Ruby program, you see the name
whatever
. How would Ruby decide whether this is a variable or a method?
Control Flow
Review: Ruby Control Flow. From the downloaded files, take a look at ruby_control_flow.rb.
Slides 1-11: Part of the popularity of Ruby stems from its expressive syntax. Review these slides and play with the code corresponding code, through the comment that says #yield.
Add to your paper:
- Slide 12 asks you to trace the code on the following slides
- Be ready to discuss with the class when/why yield might be useful
- Do the in-class challenge on slide 14
- We’ll explore blocks in more detail (slide 15) later
Ruby Unit Tests
On your own (not an in-class exercise).
The concept of unit tests is the same as other languages. Review Ruby Unit
Tests. From the downloaded files,
take a look at ruby_unit_test.rb. The only
self-test is to be sure you understand why we need assert_in_delta
rather than
just assert_equal.
Note: The hardest part of using Ruby unit tests is that the syntax changed after Ruby 1.8. Be sure you have installed a later version of Ruby. Be sure that the tests really run. You’ll practice this in the Ruby Basics homework. If you have issues, submit questions on Piazza.
Submit/Rubric
No submission. Keep your answers as a study guide.