George
12/16/2006 3:31:00 AM
On 12/16/06, Alber Eric <alberthier@yahoo.fr> wrote:
> OK, thank you for your quick answer
>
> I will just expose what I'm trying to do : I have four differents
> methods that are yielding strings. I want to use one or the other
> depending on the context but the rest of the process is the same, so I
> wrote something like that
>
> [...]
>
> What I'm looking for is a sort of function pointer.
Hi Eric,
Blocks can't take blocks in 1.8, so I think you'll need to resort
to methods if you do it this way. Example:
def methodI_foo_bar
methodI{|s| yield "foo-#{s}-bar"}
end
def methodIII_oof_rab
methodIII{|s| yield "oof-#{s}-rab"}
end
# First argument handling
case arg1
when 1
p1 = method(:methodI)
when 2
p1 = method(:methodI_foo_bar)
end
# Second argument handling
case arg2
when 3
p2 = method(:methodIII)
when 4
p2 = method(:methodIII_oof_rab)
end
p1.call { |i|
p2.call { |j|
process(i, j)
}
}
Or, perhaps more idiomatically:
def methodI_foo_bar
methodI{|s| yield "foo-#{s}-bar"}
end
def methodIII_oof_rab
methodIII{|s| yield "oof-#{s}-rab"}
end
# First argument handling
case arg1
when 1
m1 = :methodI
when 2
m1 = :methodI_foo_bar
end
# Second argument handling
case arg2
when 3
m2 = :methodIII
when 4
m2 = :methodIII_oof_rab
end
send(m1) { |i|
send(m2) { |j|
process(i, j)
}
}
Does that help?