[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Question on passing blocks

Ruby Freak

6/25/2008 3:42:00 PM

In the code below, a block, { x += 1 } is magically passed to
thrice ?? or something like that.
I don't understand the usage of the "thrice { iterator }" using {}
and why I can't make a normal block syntax work. The thrice method
doesn't take (normal) parameters, so how does this work?

Ruby, Have you been messing around behind my back?
Thanks in advance.

def thrice
yield
yield
yield
end

x = 5
puts "value of x before: #{x}" # => 5
thrice { x += 1 }
puts "value of x after: #{x}" # => 8

# broken from here down

thrice { |x| x += 1 }

thrice do |x|
x += 1
end
7 Answers

Tim Hunter

6/25/2008 4:05:00 PM

0

Ruby Freak wrote:
> In the code below, a block, { x += 1 } is magically passed to
> thrice ?? or something like that.
> I don't understand the usage of the "thrice { iterator }" using {}
> and why I can't make a normal block syntax work. The thrice method
> doesn't take (normal) parameters, so how does this work?
>
> Ruby, Have you been messing around behind my back?
> Thanks in advance.
>
> def thrice
> yield
> yield
> yield
> end
>
> x = 5
> puts "value of x before: #{x}" # => 5
> thrice { x += 1 }
> puts "value of x after: #{x}" # => 8
>
> # broken from here down
>
> thrice { |x| x += 1 }
>
> thrice do |x|
> x += 1
> end

In the first case, x in the block refers to the x defined outside the
block. In the 2nd and 3rd cases, you've used x as an argument so it
hides the x outside the block. Any changes you make to x-the-argument
are lost each time the block exits.
--
Posted via http://www.ruby-....

Sebastian Hungerecker

6/25/2008 4:09:00 PM

0

Ruby Freak wrote:
> def thrice
> =A0 =A0 yield
> =A0 =A0 yield
> =A0 =A0 yield
> end
> [...]
> # broken from here down
>
> thrice =A0{ |x| x +=3D 1 }

You're passing a block that takes one argument (x) to a method that yields=
=20
none. Also: it's usually not a good idea, to reassign a block argument. Tha=
t=20
almost certainly does not do what you want.

HTH,
Sebastian
=2D-=20
Jabber: sepp2k@jabber.org
ICQ: 205544826

Ruby Freak

6/25/2008 4:24:00 PM

0

Thank you,

I had to get my head around what seems in my mind to be a "backwards
assignment" in code blocks.
Oh. that way!

now it makes perfect sense.

Steve

10/9/2011 4:56:00 PM

0

On Sun, 9 Oct 2011 09:36:52 -0700 (PDT), wy <wy_@myself.com> wrote:

>On Oct 9, 12:26?pm, Steve <stevencan...@yahooooo.com> wrote:
>> On Sun, 09 Oct 2011 11:22:57 -0500, First Post
>>
>>
>>
>>
>>
>> <Liberals...@invalid.net> wrote:
>> >On Sun, 09 Oct 2011 12:14:42 -0400, Steve <stevencan...@yahooooo.com>
>> >wrote:
>>
>> >>On Sun, 9 Oct 2011 09:00:22 -0700 (PDT), wy <w...@myself.com> wrote:
>>
>> >>>On Oct 9, 11:44 am, Steve <stevencan...@yahooooo.com> wrote:
>> >>>> On Sun, 9 Oct 2011 15:18:49 +0000 (UTC), "6250 Dead, 1393 since
>>
>> >>>> 1/20/09" <d...@gone.com> wrote:
>> >>>> >On Sun, 09 Oct 2011 08:06:12 -0700, Phlip wrote:
>>
>> >>>> >> On Oct 8, 9:29 pm, "6250 Dead, 1393 since 1/20/09" <d...@gone.com>
>> >>>> >> wrote:
>>
>> >>>> >>> I'm already hearing murmurs of lawsuits for fraud against her. Seems
>> >>>> >>> she was soliciting donations for a possible run just two weeks ago on
>> >>>> >>> her website, but her daughter Bristol, in a TV interview, was asked
>> >>>> >>> THREE MONTHS ago if she knew what her mum's plans were, and said she
>> >>>> >>> did, but could not discuss it as it was a family matter.
>>
>> >>>> >> Jon Stewart compared her fund raising to a Nigerian Prince Scam.
>>
>> >>>> >> (And, let's face it, if the Koch Bros wanted her to be president, she'd
>> >>>> >> be completely flush, and would have cheerfully declared a while ago!)
>>
>> >>>> >The GOP is less like the comparatively harmless Nigerian scams, and more
>> >>>> >like the Jim Jones operation.
>>
>> >>>> As always, the leftist losers are obsessed with what other people do
>> >>>> with their own money..
>>
>> >>>You mean what other people do with their own money that they got from
>> >>>other people's own money. ?
>>
>> >>Oh you poor thing... <LOL> ? did somebody take your money?
>>
>> >>heh, heh, heh... ?like you ever had any money...
>>
>> >>>And if what they're doing with it is
>> >>>crashing the economy or not hiring people to make life miserable for
>> >>>the 99% other people with the money they got from them, then yeah,
>> >>>those 99% people have every right to be pissed at them.
>>
>> >>I think it's swell that you're pissed... ?my compliments to everyone
>> >>who had anything to do with it...
>>
>> >>> ?Money is a
>> >>>communal thing
>>
>> >>Nope, sorry, <LOL> ?and if you losers come anywhere near MY money I'll
>> >>see that you suffer even more...
>>
>> >Don't think that'll ever happen, at least not in wy's case.
>> >That would involve them having to get up off their ass and out of the
>> >house and actually doing something.
>>
>> Of course not... ? fools like him/her/it expend all their efforts
>> whining and crying... ?as the "occupiers" are doing.. ?They further
>> their demonstrations go, the more damage they do to their cause...
>
>The bigger fools are the ones who support the filthy rich when they
>themselves are penniless, somehow fantasizing that they'll be rewarded
>for their efforts with a spare buck or two from them.

It's you that's hoping for a spare buck, loser... <LOL> I'm just
sitting here enjoying my view out over the gulf, even though it's
raining...

>> >Not to mention, how is anyone supposed to take someone seriously who
>> >claims that those that aren't hiring are doing so to intentionally
>> >make people miserable?
>
>So you think that someone who can't find a job because none are being
>offered is making those people deliriously happy instead?
>
>

I'm thinking that rich people would be happy to hire people if they
believed there was any reward in it for themselves... Too bad Obama
has taken that away...

>> >>>and for a select few to hold back on a good chunk of it
>> >>>just for themselves can only inevitably affect the whole community
>> >>>negatively.

wy

10/9/2011 5:11:00 PM

0

On Oct 9, 12:55 pm, Steve <stevencan...@yahooooo.com> wrote:
> On Sun, 9 Oct 2011 09:36:52 -0700 (PDT), wy <w...@myself.com> wrote:
> >On Oct 9, 12:26 pm, Steve <stevencan...@yahooooo.com> wrote:
> >> On Sun, 09 Oct 2011 11:22:57 -0500, First Post
>
> >> <Liberals...@invalid.net> wrote:
> >> >On Sun, 09 Oct 2011 12:14:42 -0400, Steve <stevencan...@yahooooo.com>
> >> >wrote:
>
> >> >>On Sun, 9 Oct 2011 09:00:22 -0700 (PDT), wy <w...@myself.com> wrote:
>
> >> >>>On Oct 9, 11:44 am, Steve <stevencan...@yahooooo.com> wrote:
> >> >>>> On Sun, 9 Oct 2011 15:18:49 +0000 (UTC), "6250 Dead, 1393 since
>
> >> >>>> 1/20/09" <d...@gone.com> wrote:
> >> >>>> >On Sun, 09 Oct 2011 08:06:12 -0700, Phlip wrote:
>
> >> >>>> >> On Oct 8, 9:29 pm, "6250 Dead, 1393 since 1/20/09" <d...@gone.com>
> >> >>>> >> wrote:
>
> >> >>>> >>> I'm already hearing murmurs of lawsuits for fraud against her.. Seems
> >> >>>> >>> she was soliciting donations for a possible run just two weeks ago on
> >> >>>> >>> her website, but her daughter Bristol, in a TV interview, was asked
> >> >>>> >>> THREE MONTHS ago if she knew what her mum's plans were, and said she
> >> >>>> >>> did, but could not discuss it as it was a family matter.
>
> >> >>>> >> Jon Stewart compared her fund raising to a Nigerian Prince Scam.
>
> >> >>>> >> (And, let's face it, if the Koch Bros wanted her to be president, she'd
> >> >>>> >> be completely flush, and would have cheerfully declared a while ago!)
>
> >> >>>> >The GOP is less like the comparatively harmless Nigerian scams, and more
> >> >>>> >like the Jim Jones operation.
>
> >> >>>> As always, the leftist losers are obsessed with what other people do
> >> >>>> with their own money..
>
> >> >>>You mean what other people do with their own money that they got from
> >> >>>other people's own money.
>
> >> >>Oh you poor thing... <LOL> did somebody take your money?
>
> >> >>heh, heh, heh... like you ever had any money...
>
> >> >>>And if what they're doing with it is
> >> >>>crashing the economy or not hiring people to make life miserable for
> >> >>>the 99% other people with the money they got from them, then yeah,
> >> >>>those 99% people have every right to be pissed at them.
>
> >> >>I think it's swell that you're pissed... my compliments to everyone
> >> >>who had anything to do with it...
>
> >> >>> Money is a
> >> >>>communal thing
>
> >> >>Nope, sorry, <LOL> and if you losers come anywhere near MY money I'll
> >> >>see that you suffer even more...
>
> >> >Don't think that'll ever happen, at least not in wy's case.
> >> >That would involve them having to get up off their ass and out of the
> >> >house and actually doing something.
>
> >> Of course not... fools like him/her/it expend all their efforts
> >> whining and crying... as the "occupiers" are doing.. They further
> >> their demonstrations go, the more damage they do to their cause...
>
> >The bigger fools are the ones who support the filthy rich when they
> >themselves are penniless, somehow fantasizing that they'll be rewarded
> >for their efforts with a spare buck or two from them.
>
> It's you that's hoping for a spare buck, loser... <LOL>

I've got more bucks than I can handle. I'm sure I could even spare
you a buck or two. But you're too stupid to have it.

> I'm just
> sitting here enjoying my view out over the gulf, even though it's
> raining...

Thinking of suicide, are you? Don't let the rain hold you back.

>
> >> >Not to mention, how is anyone supposed to take someone seriously who
> >> >claims that those that aren't hiring are doing so to intentionally
> >> >make people miserable?
>
> >So you think that someone who can't find a job because none are being
> >offered is making those people deliriously happy instead?
>
> I'm thinking that rich people would be happy to hire people if they
> believed there was any reward in it for themselves...  Too bad Obama
> has taken that away...

Rich people already have all the rewards they want. First of all,
they're RICH. They don't need to employ anyone to make them richer
than rich, especially if they can't even spend all the money they have
now on themselves in their own lifetime. Second of all, they're
already getting richer just by not hiring. So if they're rich and
still getting richer without hiring more people, where's the real
incentive in hiring anyone at all, Obama or no Obama? It's already
working for them by *not* having people work for them. That's the new
economy now. Welcome to the 21st century.

>
> >> >>>and for a select few to hold back on a good chunk of it
> >> >>>just for themselves can only inevitably affect the whole community
> >> >>>negatively.

Old Baye

10/9/2011 5:57:00 PM

0

On 10/9/2011 8:06 AM, Phlip wrote:
> (And, let's face it, if the Koch Bros wanted her to be president,
> she'd be completely flush, and would have cheerfully declared a while
> ago!)

Just as Soros planted the Obamessiah in the White House?

6119 Dead, 1262 since 1/20/09

10/9/2011 7:03:00 PM

0

On Sun, 09 Oct 2011 10:57:10 -0700, Old Baye <ob@in.valid> wrote:

>On 10/9/2011 8:06 AM, Phlip wrote:
>> (And, let's face it, if the Koch Bros wanted her to be president,
>> she'd be completely flush, and would have cheerfully declared a while
>> ago!)
>
>Just as Soros planted the Obamessiah in the White House?

Glenn Beck is yesterday's news, but his ignorant loons keep right on
posting.
--
"So called payroll taxes aren't taxes at all" -- Steve Canyon, trying to explain
why millionaires don't actually pay less taxes than median income families.