Returning keyword

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

On 11/13/06, S. Robert J. [email protected] 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-returning

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.

S. Robert J. 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.

HI –

On Mon, 13 Nov 2006, S. Robert J. 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.

Hang on a second. I know that there’s a vogue for snideness and
“rants” these days, but please – try not to be swept along by the
tide.

If you know Sam, who committed the #returning method, you’ll know that
there’s literally no possibility that he’s “showing off cool meta
programming”. If you don’t know Sam, come to a Rails conference some
time and introduce yourself. You can also check out his best-known
work, a JavaScript library called Prototype. It ships with Rails, and
has currency and popularity well beyond Rails.

In general, remember that when you participate in this forum, you’re
not sitting in a darkened theater making wisecracks to images on a
screen. Rather, you’re among colleagues and fellow practitioners, an
astounding number of whom are willing and able to discuss technical
matters intelligently and enthusiastically with you. It’s not just a
matter of not flinging insults at people who might actually hear you
(though that’s part of it). It’s also about taking advantage of the
knowledge and, most importantly, the collegiality available to you
here if you choose not to spurn it.

David

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.

On Mon, 13 Nov 2006, S. Robert J. 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.

nothing clever about it afaikt - made sense instantly: it’s a factory
method.
i use this idiom in code all the time

def m
ret = Array.new

 ...

 return ret

end

or even

doesn’t make sense for short methods, but once they reach 20/30 lines
and have
complex logic is really nice to see up front what a method returns.
it’s the
same reason some people prefer

char *hostname();

over

def hostname()

a refinement of

def m
Object.new.instance_eval{
configure @object
self
}
end

which can make debugging hard due to having two scopes in the method
body

in summary you know at a glace what’s being returned - that’s quite
useful
sometimes.

-a

Hi –

On Tue, 14 Nov 2006, [email protected].invalid wrote:

 o.property_8 = 'value_8'
 o.property_19 = 'value_19'
 o.property_30 = 'value_30'
 o.property_41 = 'value_41'
 o.property_42 = 'value_42'
 o.property_43 = 'value_43'
               ^^

Wow, you must feel very strongly about this :slight_smile:

David

On Tue, 14 Nov 2006, S. Robert J. 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’
o.property_1 = ‘value_1’
o.property_2 = ‘value_2’
o.property_3 = ‘value_3’
o.property_4 = ‘value_4’
o.property_5 = ‘value_5’
o.property_6 = ‘value_6’
o.property_7 = ‘value_7’
o.property_8 = ‘value_8’
o.property_9 = ‘value_9’
o.property_10 = ‘value_10’
o.property_11 = ‘value_11’
o.property_12 = ‘value_12’
o.property_13 = ‘value_13’
o.property_14 = ‘value_14’
o.property_15 = ‘value_15’
o.property_16 = ‘value_16’
o.property_17 = ‘value_17’
o.property_18 = ‘value_18’
o.property_19 = ‘value_19’
o.property_20 = ‘value_20’
o.property_21 = ‘value_21’
o.property_22 = ‘value_22’
o.property_23 = ‘value_23’
o.property_24 = ‘value_24’
o.property_25 = ‘value_25’
o.property_26 = ‘value_26’
o.property_27 = ‘value_27’
o.property_28 = ‘value_28’
o.property_29 = ‘value_29’
o.property_30 = ‘value_30’
o.property_31 = ‘value_31’
o.property_32 = ‘value_32’
o.property_33 = ‘value_33’
o.property_34 = ‘value_34’
o.property_35 = ‘value_35’
o.property_36 = ‘value_36’
o.property_37 = ‘value_37’
o.property_38 = ‘value_38’
o.property_39 = ‘value_39’
o.property_40 = ‘value_40’
o.property_41 = ‘value_41’
o.property_42 = ‘value_42’
o.property_43 = ‘value_43’
o.property_44 = ‘value_44’
o.property_45 = ‘value_45’
o.property_46 = ‘value_46’
o.property_47 = ‘value_47’
o.property_48 = ‘value_48’
o.property_49 = ‘value_49’
o.property_50 = ‘value_50’
o.property_51 = ‘value_51’
o.property_52 = ‘value_52’
o.property_53 = ‘value_53’
o.property_54 = ‘value_54’
o.property_55 = ‘value_55’
o.property_56 = ‘value_56’
o.property_57 = ‘value_57’
o.property_58 = ‘value_58’
o.property_59 = ‘value_59’
o.property_60 = ‘value_60’
o.property_61 = ‘value_61’
o.property_62 = ‘value_62’
o.property_63 = ‘value_63’
o.property_64 = ‘value_64’
o.property_65 = ‘value_65’
o.property_66 = ‘value_66’
o.property_67 = ‘value_67’
o.property_68 = ‘value_68’
o.property_69 = ‘value_69’
o.property_70 = ‘value_70’
o.property_71 = ‘value_71’
o.property_72 = ‘value_72’
o.property_73 = ‘value_73’
o.property_74 = ‘value_74’
o.property_75 = ‘value_75’
o.property_76 = ‘value_76’
o.property_77 = ‘value_77’
o.property_78 = ‘value_78’
o.property_79 = ‘value_79’
o.property_80 = ‘value_80’
o.property_81 = ‘value_81’
o.property_82 = ‘value_82’
o.property_83 = ‘value_83’
o.property_84 = ‘value_84’
o.property_85 = ‘value_85’
o.property_86 = ‘value_86’
o.property_87 = ‘value_87’
o.property_88 = ‘value_88’
o.property_89 = ‘value_89’
o.property_90 = ‘value_90’
o.property_91 = ‘value_91’
o.property_92 = ‘value_92’
o.property_93 = ‘value_93’
o.property_94 = ‘value_94’
o.property_95 = ‘value_95’
o.property_96 = ‘value_96’
o.property_97 = ‘value_97’
o.property_98 = ‘value_98’
o.property_99 = ‘value_99’
o.property_100 = ‘value_100’
o.property_101 = ‘value_101’
o.property_102 = ‘value_102’
o.property_103 = ‘value_103’
o.property_104 = ‘value_104’
o.property_105 = ‘value_105’
o.property_106 = ‘value_106’
o.property_107 = ‘value_107’
o.property_108 = ‘value_108’
o.property_109 = ‘value_109’
o.property_110 = ‘value_110’
o.property_111 = ‘value_111’
o.property_112 = ‘value_112’
o.property_113 = ‘value_113’
o.property_114 = ‘value_114’
o.property_115 = ‘value_115’
o.property_116 = ‘value_116’
o.property_117 = ‘value_117’
o.property_118 = ‘value_118’
o.property_119 = ‘value_119’
o.property_120 = ‘value_120’
o.property_121 = ‘value_121’
o.property_122 = ‘value_122’
o.property_123 = ‘value_123’
o.property_124 = ‘value_124’
o.property_125 = ‘value_125’
o.property_126 = ‘value_126’
o.property_127 = ‘value_127’
end
end

-a

[email protected] wrote:

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 V.

On 11/13/06, S. Robert J. [email protected] 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.

:From => “Louis J Scoras [mailto:[email protected]]”

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}

clever indeed.
+1 on my-bag-of-tricks.
thanks for the tip.
kind regards -botp

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs