Last active
August 29, 2015 14:04
-
-
Save rjgroller/8062bc852c206fbc7916 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Q: Define a function to find the hypotenuse of a right triangle with side lengths a and b | |
# A: | |
def hypotenuse(a, b) | |
# Return the hypotenuse of a triangle with lenght a and height b. | |
return Math.sqrt(a **2 + b **2) | |
end | |
# Q: What methods do strings have that symbols don't? Describe a few. What methods do symbols have that strings don't? | |
# A: join, reverse, strip, chomp, sub - strings have many methods focused on manipulating strings | |
# Q: Why doesn't Fixnum.new work? | |
# A: Array and Hash are containers that hold other objects and are mutable, Fixnum is not a container and immutable | |
# Q: Write code using methods on at least one number, string, symbol, array and hash. Make a gist from the code. | |
# A: | |
4.modulo(2) | |
"Ruby, Ruby, Ruby".split(", ") | |
:ruby.to_s | |
[1, 2, "three", "four", 5].first | |
{ a: "Ruby", b: "is", c: "pretty", d: "cool" }.invert | |
# Q: Define Musher class such that Musher.new("+").mush(["array", "of", "strings"]) == "array + of + strings" | |
# A: | |
class Musher | |
def initialize(sep) | |
@sep = sep | |
end | |
def mush(arr) | |
arr.join(" #{@sep} ") | |
end | |
end | |
# Q: Define an Averager class that can compute the average of two numbers. Bonus: any number of numbers. | |
# A: | |
class Averager | |
def self.average(*args) | |
args.reduce(:+).to_f / args.length | |
end | |
end | |
# Q: Write a script to read list of lines from a file and print one at random | |
# A: | |
def sample_line(fname) | |
File.readlines(fname).sample.strip | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment