Ruby: Passing optional objects to method
Posted
by Sam
on Stack Overflow
See other posts from Stack Overflow
or by Sam
Published on 2010-05-21T00:18:11Z
Indexed on
2010/05/21
0:20 UTC
Read the original article
Hit count: 611
ruby
Class TShirt
def size(suggested_size)
if suggested_size == nil
size = "please choose a size"
else
size = suggested
end
end
end
tshirt = TShirt.new
tshirt.size("M")
== "M"
tshirt = TShirt.new
size = tshirt.size(nil)
== "please choose a size"
What is a better way to have optional objects in a method? Procs?
© Stack Overflow or respective owner