Default Arguments And Option Hashes Notes Day 1 Flashcards
1
Q
how do you assign a default argument?
A
def repeat(message, num=1)
message * num
end
p repeat("hi") # => "hi" p repeat("hi", 3) # => "hihihi"
2
Q
If you have a method that accepts a hash as an argument can you omit the braces when passing in the hash?
A
yes you can def modify_string(str, options) str.upcase! if options["upper"] p str * options["repeats"] end
# less readable modify_string("bye", {"upper"=>true, "repeats"=>3}) # => "BYEBYEBYE"
# more readable modify_string("bye", "upper"=>true, "repeats"=>3) # => "BYEBYEBYE"