Ruby use method only if condition is true
Posted
by Vincent
on Stack Overflow
See other posts from Stack Overflow
or by Vincent
Published on 2010-05-18T17:12:27Z
Indexed on
2010/05/18
17:30 UTC
Read the original article
Hit count: 359
ruby
So I have this code:
class Door
# ...
def info attr = ""
return {
"width" => @width,
"height" => @height,
"color" => @color
}[attr] if attr != ""
end
end
mydoor = Door.new(100, 100, "red")
puts mydoor.info("width")
puts mydoor.info
The method "info" should return the hash if no argument is provided, otherwise the value of the argument in the hash. How can I achieve that?
© Stack Overflow or respective owner