tags:

views:

115

answers:

5

A function takes a string as argument, example fn("string"). i want to pass in different values depending on three of more condiitons, 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
})

This way 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?

+1  A: 

You can use an if expression, or even a case expression. See the PickAxe book. An example of what you want to do is all the way at the top of the page.

jqno
this solves my problem but i still want to enclose the if condition in a container that looks like a block, not like an if-condition.. u get what i mean?
+1  A: 

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.

def test(parameter)
  puts "I was passed the value: #{parameter}"
end

foo = lambda{|x,y,z|
if x == true
"yes"
elsif y == true
"no"
else
"maybe"
end
}

test(foo[false,true,false])
cgr
i am not sure how i use it. I mean, if i do test( foo), i get an output like I was passed the value: #<Proc:0xb64b8d34@(irb):33>, whereas i want sth like: I was passed the value: true
the lambda foo is expecting 3 inputs. it needs the input to return a result. so you can't pass test(foo) you have to pass test(foo["something",true,false])
cgr
+3  A: 

Block is cool ,but it doesn't mean we always have to use it.

IMHO , It would be much more clear to use a seperated function to get the string you want to pass to the fn.

 def arg w
    if w.eql? "check_1"
            "yes"
    elsif w.eql? "check_2"
            "no"
    else
            "maybe"
    end
 end

def fn s
     puts s
end

fn(arg("check_1"))
fn(arg("check_2"))
fn(arg("check_3"))

Or use a Hash,which is left to you as an exercise :)

pierr
actually, i am using table_helper module in rails, and for a column named for example t.status, whose code i am not changing, i have to specify a value of an image depending on two or three attributes. so i am constrained to passing it a string, and i want to do it in one line, and not write a function separately sort of, so i thought block would be nice here, because ppl know block is like one line even if it spreads over more than one
+1  A: 
arg= check_1&&"yes"||check_2&&"no"||"maybe"
marocchino
+1  A: 

It seems you have some misconceptions about blocks, but it's another matter entirelly.

I think that will help you:

check_1 = false
check_2 = true

def foo(&block)
  puts block.call
end

foo { 
  if check_1 
    arg = "yes"
  elsif check_2
    arg = "no"
  else 
    arg = "maybe"
  end
}
Mereghost