Default Arguments And Option Hashes Notes Day 1 Flashcards

You may prefer our related Brainscape-certified 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"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly