A function takes a string as argument, example fn(“string”).
I want to pass different values depending on three of more conditions, and one approach which is slightly unreadable is:
fn(check_1 ? "yes" : check_2 ? "no" : "maybe")
I want to replace the argument with a block that returns a string. i am hoping i can do something like:
fn({|arg| if check_1
arg = "yes"
elsif check_2
arg = "no"
else
arg = "maybe"
end
})
I can have more conditions and would still be able to write all this as if this was in one line.
How do I do this in Ruby?
This sounds a little strange. Are you sure you don’t want to pass in an array and process the arguments from within the method?
either way, it kind of sounds like you what you are asking for is a lambda to process the logic, and then you can pass the lambda into the function to return the actual results.