[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Converting a Proc-object into a normal method.

michael.lesniak@gmail.com

6/15/2006 2:41:00 PM

Hello,

on my way through dynamic method creation another question arose:

--- snip ---
def gen_times(factor)
return Proc.new {|n| n*factor }
end

times3 = gen_times(3)
times3.call(12)
--- snap ---

This example from the ruby-book creates a function on the fly which I
quite what I searched for. From my little scheme background I supposed
that I can directly bind the return value of gen_times to a variable
which is then equal to a method call. But, as exemplified above, I have
to go through call(...). Is there any way to convert a Proc-object to a
normal method, e.g. I would only have to do

new_times3 = gen_times(3).magic_call

and could then just write

new_times3(12)


Thanks for helping,
Michael

4 Answers

Trans

6/15/2006 4:34:00 PM

0


Michael Lesniak wrote:
> Hello,
>
> on my way through dynamic method creation another question arose:
>
> --- snip ---
> def gen_times(factor)
> return Proc.new {|n| n*factor }
> end
>
> times3 = gen_times(3)
> times3.call(12)
> --- snap ---
>
> This example from the ruby-book creates a function on the fly which I
> quite what I searched for. From my little scheme background I supposed
> that I can directly bind the return value of gen_times to a variable
> which is then equal to a method call. But, as exemplified above, I have
> to go through call(...). Is there any way to convert a Proc-object to a
> normal method, e.g. I would only have to do
>
> new_times3 = gen_times(3).magic_call
>
> and could then just write
>
> new_times3(12)

Not extactly as you eg'd, but:

def magic_call( name, &proc )
define_method( name, &proc )
end

T.

Robert Klemme

6/15/2006 4:54:00 PM

0

Michael Lesniak wrote:
> Hello,
>
> on my way through dynamic method creation another question arose:
>
> --- snip ---
> def gen_times(factor)
> return Proc.new {|n| n*factor }
> end
>
> times3 = gen_times(3)
> times3.call(12)
> --- snap ---
>
> This example from the ruby-book creates a function on the fly which I
> quite what I searched for. From my little scheme background I supposed
> that I can directly bind the return value of gen_times to a variable
> which is then equal to a method call. But, as exemplified above, I have
> to go through call(...). Is there any way to convert a Proc-object to a
> normal method, e.g. I would only have to do
>
> new_times3 = gen_times(3).magic_call
>
> and could then just write
>
> new_times3(12)

irb(main):009:0> def gen_times(sym,fact)
irb(main):010:1> class <<self;self;end.instance_eval do
irb(main):011:2* define_method(sym) {|x| x*fact}
irb(main):012:2> end
irb(main):013:1> end
=> nil
irb(main):014:0> gen_times :foo, 3
=> #<Proc:0x003780f8@(irb):11>
irb(main):015:0> foo 10
=> 30

If you want to work like in functional languages it's probably easier to
use lambdas only:

def curry_1(*args, &b)
lambda {|*a| b[*(args+a)]}
end
def curry_2(b, *args)
lambda {|*a| b[*(args+a)]}
end

irb(main):025:0> m3 = curry_1(3){|x,y| x*y}
=> #<Proc:0x100e9fa8@(irb):17>
irb(main):026:0> m3[10]
=> 30
irb(main):027:0> m3 = curry_2 lambda {|x,y| x*y}, 3
=> #<Proc:0x003c68c0@(irb):20>
irb(main):028:0> m3[10]
=> 30

Kind regards

robert



Kind regards

robert

Zev

1/13/2010 10:26:00 AM

0

"dsharavi@gmail.com" <dsharavi@yahoo.com> ???
??????:c54d86b0-06ad-4fd1-
a868-3c3c91dd93b9@s31g2000yqs.googlegroups.com...
> >"iconocl...@yahoo.com" <coaster132...@yahoo.com>wrote:

>>>Gaza is not in Israel's hands? That's ludicrous, Zev. You know better.
>>>It's worse-off than when it was occupied with infantry. At least made
>>>the IDF a little more careful with its bombing and rocketing.
>
>>I asked a simple question, and got no answer.
>
> Those of us who know H are reeling with surprise.
>
>>But there's an interesting point here.
>>Artillery shells are cheap, but not accurate.
>>Cruise missiles are accurate, but expensive.
>>By selling them to Israel,
>>each for the price of a standard explosive shell,
>>your country can save Palestinian lives.
>>If you're against that, your concern is insincere.
>
> How can that save Palestinian lives?

By reducing collateral damage.

Zev

1/15/2010 12:27:00 PM

0

On Jan 15, 6:19 am, "iconocl...@yahoo.com" <coaster132...@yahoo.com>
wrote:
> On Jan 13, 5:30 am, Zev <zev_h...@yahoo.com> wrote:
> > On Jan 12, 10:39 pm, "iconocl...@yahoo.com" <coaster132...@yahoo.com>
> > wrote:
> > > On Jan 12, 5:15 am, Zev <zev_h...@yahoo.com> wrote:
> > > > On Jan 11, 9:08 pm, "iconocl...@yahoo.com" <coaster132...@yahoo.com>
> > > > wrote:
> > > > > On Jan 11, 6:52 am, Zev <zev_h...@yahoo.com> wrote:
> > > > > > "iconocl...@yahoo.com" <coaster132...@yahoo.com> ???
> > > > > > ??????:35de56f0-4648-472e-9a2f-
> > > > > > d31cafe21...@l30g2000yqb.googlegroups.com...
> > > > > > > On Jan 8, 5:09 am, dsharavi <dshara...@hotmail.com> wrote:

> > > > > > > A little aside: the nearby parts of West Bank have been vastly
> > > > > > > improved both directly and indirectly with American money.
> > > > > > > That value
> > > > > > > added in the form of housing, infrastructure, etc., must be a
> > > > > > > factor
> > > > > > > in the trade-off because it belongs, lock, stock and barrel to
> > > > > > > the
> > > > > > > Palestinians, improvements and all. If you build a house on my
> > > > > > > land, I
> > > > > > > own it, period. That's American common law anyway. The more
> > > > > > > Israel
> > > > > > > keeps, the greater the multiples in raw land she will have to
> > > > > > > cede to
> > > > > > > the Palestinians to equalize the *value*. It seems to me that
> > > > > > > this
> > > > > > > should militate in favor of abandoning some of the large
> > > > > > > settlements,
> > > > > > > INTACT AND UNDAMAGED that is, with value not trashed as it was
> > > > > > > in
> > > > > > > Gaza. Doesn't Israel owe that at least to the United States?
> > > > > > > She
> > > > > > > certainly does. How could she consider playing fast and loose
> > > > > > > with
> > > > > > > our investment given the fact that in accord with tradition in
> > > > > > > the
> > > > > > > relationship, we will have to fund the rehabilitation of
> > > > > > > Palestine
> > > > > > > which Israel destroyed. If this new infrastructure is
> > > > > > > destroyed we
> > > > > > > will in essence have to pay for it twice. We'll also probably
> > > > > > > have to
> > > > > > > pay Israel to behave herself sort of like the Swiss Banks did
> > > > > > > the
> > > > > > > Holocaust Industry. There is not much doubt about that.
>
> > > > > > The fact that Israel is sovereign in the entire WB
> > > > > > destroys your argument.
>
> > > > > Pathetic, Zev.
>
> > > > An anomaly, Ico.
> > > > Your point?
>
> > > That you ignore law, politics and reality. Everything else aside, she
> > > is not sovereign even in the functional sense. She runs a military
> > > occupation. She is attempting to colonize it and is having
> > > difficulties doing that. The struggle with its native people is
> > > ongoing. Gaining territory by conquest and colonizing it are both
> > > expressly illegal. There is even a competing government there which
> > > denies her sovereignty absolutely. And as I say, the war against the
> > > occupied people renewed in 1967 continues. Her occupation is deemed
> > > illegitimate by the entire world, including her poodle, the US. Israel
> > > badly lacks legitimacy on the West Bank. And legitimacy, i.e.,
> > > acceptance by the world community as the rightful ruler, is an aspect
> > > of sovereignty.
>
> > > You ask questions to which you already know the answer but that's fine
> > > with me.
>
> > Everywhere in the world, except the M.E.
> > all of the aspects of sovereignty come together,
> > either you have them all, or you don't have any.
> > That's why it's anomalous.
> > It shouldn't be *that* difficult to understand.
>
> Ah, I see what you mean by anomalous. It was sort of a whine. Every
> other nationality got to kick, shove, bite and push to carve out the
> land they wanted. Poor Israel came to the game too late---or is it
> antisemitism in another form? Call it what you like but the law's been
> changed. 17th Century Law of War no longer applies.Occupations are
> regulated and must be brief. Subject populations are protected. They
> can not be displaced nor can they be divested of a right of return.
> Military occupations the result of war can not evolve into
> sovereignty. International human rights law is a relatively new
> animal. There is also the problem Israel has with her sponsor. We are
> not expansionist. We oppose aggression for territorial gain. It's not
> something we can oppose and support at the same time.
>
> So no, it is not anomalous. Israel is out of step with the modern
> world.

No border, no previous sovereignty,
since 1922, Jewish right to live in modern Palestine
without discrimination.
In short, an anomaly.

And BTW, if you don’t take an interest in Goa or Tibet,
don’t expect me to take your supposed
legality/morality seriously.