Robert Klemme wrote:
> ** SPOILER **
>
> Don't read on if you first want to experiment yourself.
>
>
> On 17.01.2007 17:48, Neutek wrote:
> > Robert, I was actually reading the summation wiki and they had a few
> > code examples in C++/Java.. I thought I'd goof a bit and write
> > something out in ruby. Of course, I hit the roadblock when trying to
> > pass math operators(or methods rather) to a method...
>
> What is the "summation wiki"?  Are you referring to this page?
> http://en.wikipedia.org/wiki/Sum
>
> If yes, here are some more ways:
>
> # plain values
> sum = (m..n).inject(0){|s,x| s + x}
>
> # with a function
> f = lambda {|i| i * 2}
> sum = (m..n).inject(0){|s,x| s + f[x]}
>
> # in a method
> def sum(m, n, f)
>    (m..n).inject(0){|s,x| s + f[x]}
> end
>
> s = sum( 1, 2, lambda {|x| x * 2} )
>
> etc.
>
> Advantage of using lambdas is that they are more flexible than method
> names and can contain arbitrary calculations.
>
> Kind regards
>
> 	robert
>
>
> PS: Please don't top post.

What's top post? (sorry, I'm new to google groups --I put my reply at
the bottom if this is what you meant )

I'll read through your example now but figured what I worked on in the
interim was worth posting..


#works
def test(a, to_do, b)
	return a.send(to_do, b)
end
puts test(2, :**, 3)

#does not work when trying to send an entire mathematical expresion as
a param
def sigma(floor, to_do, cap)
	x = 0
	floor.upto(cap) {|i|
		x += i.send(to_do)
	}
	return x
end
puts sigma(4, :**2, 20)


#does not work.. but another example of what I would expect :(

def do_it(n, to_do)
	return n.send(to_do)
end
puts sigma(3, :+4/2) #should yield 5


(Thank you)?