Topics Covered: calling and creating methods; objects; classes
What's the output of each of the following? Go through them and write the answers down. Then check your answers 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.
Suppose I had a class called FishClass
. Give an example of:
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
get_louder
return?get_louder
method does (Hint: there are 3 steps).loudness
after the call to get_louder
(i.e. at the end of the program)? Why?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.
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:
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 :)
Hover for answer
split
is called on Strings."Coding is cool".split(" ") => ["Coding", "is", "cool"]
join
, which takes an array and merges all of its items into a string, can behave like its opposite:
"Coding is cool".split(" ").join(" ")
=> "Coding is cool"
Notice how we can chain methods (use one after another wihtout assigning the intermediate result to a variable)—it's a very powerful technique.
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
.
Hover for answer
# twist_and_whisper
def whisper(message)
return message.downcase
end
def twist(message)
return message.split("").shuffle.join
end
Write a method invert
that inverts the keys and values of a Hash.
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:
nil
{:a => ["b", "c"]}
{:a => ["b", {:c => "d"}]}
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?
Hover for answer