Homework for 3B

Review

Topics Covered: calling and creating methods; objects; classes


Exercises

E1: Guess the Class

What's the output of each of the following? Check in pry.

1.class
1.0.class
-1.class
-1.1.class
"hi".class
(7>6).class
(6>7).class
>.class
x = ["a", "b"]
x.class
x.reverse.class
x.join.class
x.length.class # .length is the same as .size
x.class.class
x.class.class.class

Classes are why things like 1 + "hi" don't work—now the error message TypeError: String can't be coerced into Fixnum might start making some sense. Methods like .to_i and .to_s make it easy to convert between classes.

E2: Bass Class

Suppose I had a class called FishClass. Give an example of:

E3: Scoped out

Look at this code, and answer some questions. As usual, write down your answers first, then try your code in pry.

loudness = 10
def get_louder
  loudness = 20
  puts "IT'S SO LOUD"
  loudness
end

get_louder

Make sure to keep track in your head which variables are inside a method and which variables are outside (indentation really helps to visualize this). No matter what you call your variables, keep track that inside methods, there's a whole new scope—in other words, it's like a whole new program is running.

E4: Split Up

Use your best friend Google (or Bing, if you're one of those) to research the method split in Ruby. Then answer the following questions:

  1. What class of objects can it be called on?
  2. What class does it return?
  3. Does it take any arguments? How many? Are they required or optional?
  4. If it takes arguments, what class should they be?
  5. Give example usage.
  6. Can you think of a method we've learned that behaves like the opposite of split?

Only after you've written down your answers should you check them. If you don't know, give it your best shot—you're not being graded, so you're only helping yourself :)

E5: Twist and Whisper

Somewhat like the shout method, write a method whisper that prints out an input string in lower case.

whisper("HI THERE I AM HUNGRY")
# => "hi there i am hungry"

Next, write a method twist that takes a string and prints out the letters randomly. The output should be different each time you run the function.

twist("HI THERE I AM STILL HUNGRY")
# => "EUSI LLAM NTGY IR H IHRTHE"

Hint: There is a convenient method named shuffle that mixes up all the elements randomly, but only works for arrays and not strings. However, can you think of an easy way that we've already learned to convert a string to an array of letters? After you split up the string into an array, how do you suppose we can join them back together? This is a good case of chaining methods, or calling one method one after another, like you might have done with gets.strip.downcase.

E6: hsaH

Write a method invert that inverts the keys and values of a Hash.

Testing

A huge part of writing code is testing to make sure it works, in particular edge cases. Edge cases are common inputs that break a program: things like passing in nil, negative numbers, very large numbers, etc. What kinds of test cases and edge cases can you think of for this method? Here are some:

Coincidentally, Ruby comes with a built-in method called invert on the Hash class. Try these test cases on those (i.e. {:a => ["b", "c"]}.invert). How does your program's output compare to Ruby's? Can you think of any other test cases?