[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

returning keyword

S. Robert James

11/13/2006 4:58:00 AM

What is the "returning" keyword? It's usually used with a block, but I
can't find what it is.

11 Answers

Wilson Bilkovich

11/13/2006 5:15:00 AM

0

On 11/13/06, S. Robert James <srobertjames@gmail.com> wrote:
> What is the "returning" keyword? It's usually used with a block, but I
> can't find what it is.
>

It's not a keyword; it is a method added by the ActiveSupport library.
More here: http://weblog.jamisbuck.org/2006/10/27/mining-activesupport-object...

S. Robert James

11/13/2006 7:02:00 AM

0

Thanks.

Wow! That's an example of clever code. The code's less clear, no
shorter, and confusing to someone not familiar with this new method -
and for what? To be able to show off cool meta programming?

I think Rails is great, but someone needs to remind people that "clever
code" is an insult.


Wilson Bilkovich wrote:
> On 11/13/06, S. Robert James <srobertjames@gmail.com> wrote:
> > What is the "returning" keyword? It's usually used with a block, but I
> > can't find what it is.
> >
>
> It's not a keyword; it is a method added by the ActiveSupport library.
> More here: http://weblog.jamisbuck.org/2006/10/27/mining-activesupport-object...

Trans

11/13/2006 1:12:00 PM

0


S. Robert James wrote:
> Thanks.
>
> Wow! That's an example of clever code. The code's less clear, no
> shorter, and confusing to someone not familiar with this new method -
> and for what? To be able to show off cool meta programming?
>
> I think Rails is great, but someone needs to remind people that "clever
> code" is an insult.

I disagree. It's not an uncommon to see the pattern:

def foo
o = Somthing.new
# muckaround with o
return o
end

The #returning method provides the same kind of "block orientation" we
use with files.

def foo
returning Something.new do |o|
# muckaround with o
end
end

In simple cases, sure, it's no dig deal. But in more complex cases it
lends to readability b/c it tells us what we're after from the start.

T.

dblack

11/13/2006 1:46:00 PM

0

Ara.T.Howard

11/13/2006 2:37:00 PM

0

S. Robert James

11/13/2006 3:10:00 PM

0

I still maintain that this is clever code. Every new (pseudo)keyword
comes at a cost, of having to learn (and keep in your head) something
new. They have to justify their cost by truly cleaning up the code -
and I haven't seen an example of that yet.

However, I do accept that my words were out of tact, and could have,
and should have, been said politely. I stand corrected - thanks for
pointing that out.

Ara.T.Howard

11/13/2006 3:44:00 PM

0

dblack

11/13/2006 4:26:00 PM

0

David Vallner

11/13/2006 10:30:00 PM

0

ara.t.howard@noaa.gov wrote:
> On Tue, 14 Nov 2006, S. Robert James wrote:
>
>> I still maintain that this is clever code. Every new (pseudo)keyword
>> comes
>> at a cost, of having to learn (and keep in your head) something new.
>> They
>> have to justify their cost by truly cleaning up the code - and I haven't
>> seen an example of that yet.
>
> that is a reasonable thing to say - imho you'd need something like this
> to do
> so
>
>
> def factory
> returning Object.new do |o|
> o.property_0 = 'value_0'

[snip]

> o.property_127 = 'value_127'
> end
> end
>
> -a

^(Foo new) name: 'fred';
surname: 'flintstone';
wife: wilma;
yourself.

I think that one's on the top of my list of Little Smalltalk Features I
Miss. No pseudokeywords, no having to bend setters / add_whatever
methods backwards, or decide in the library whether to return self or
the added object / setter value.

David Vallner

Louis J Scoras

11/14/2006 2:38:00 PM

0

On 11/13/06, S. Robert James <srobertjames@gmail.com> wrote:

> I still maintain that this is clever code. Every new (pseudo)keyword
> comes at a cost, of having to learn (and keep in your head) something
> new. They have to justify their cost by truly cleaning up the code -
> and I haven't seen an example of that yet.

It's not a new keyword though, it's simply a method and a very useful
one at that. In my opinion, the intention comes off pretty clearly,
and if not there is always the documentation.

Consider how many times you see the idiom:

def foo
o = Object.new
o.mutate!
o
end

The fact that you see that so much is just crying out for an
additional abstraction, and that's what 'returning' provides.

In fact, something like this could be helpful in other scenarios as
well. Take Enumerable#inject for example. Many times you are just
making changes to the state parameter and returning that at the end of
the block. Try something like this:

module Enumerable
def injecting(s)
inject(s) do |k, i|
yield(k, i); k
end
end
end

Say you want to count letters--

some_text.inject(Hash.new(0)) {|h,l| h[l] +=1; h}

vs

some_text.injecting(Hash.new(0) {|h,l| h[l] += 1}

It's not about being clever, or saving some typing (as this example
shows, its the same about of characters). It's about abstracting away
repeated patterns; that's almost always a win in my book.


--
Lou.