Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How To Avoid Ugly Declerations

0 views
Skip to first unread message

Michael Boutros

unread,
Dec 11, 2007, 7:15:43 PM12/11/07
to
Hello! More and more I find myself having to do something like this:

def some_method(foo)
results = []
foo.each {|f| results << f.reverse}
return results
end

As you can see, that is some pretty ugly code, but it's also a common
scenario. How can I make it a little less ugly?

Thanks,
Michael Boutros
--
Posted via http://www.ruby-forum.com/.

yermej

unread,
Dec 11, 2007, 7:34:18 PM12/11/07
to

Map:

def some_method(foo)
foo.map {|f| f.reverse}
end

or inject, but it's less clear in this case:

def some_method(foo)
foo.inject([]) {|arr, f| arr << f.reverse}
end

Justin Collins

unread,
Dec 11, 2007, 7:26:36 PM12/11/07
to
Michael Boutros wrote:
> Hello! More and more I find myself having to do something like this:
>
> def some_method(foo)
> results = []
> foo.each {|f| results << f.reverse}
> return results
> end
>
> As you can see, that is some pretty ugly code, but it's also a common
> scenario. How can I make it a little less ugly?
>
> Thanks,
> Michael Boutros
>

In this specific case...

def some_method(foo)
foo.map { |f| f.reverse }
end

Use methods which return new objects and take advantage of implicit
return values.

-Justin

Alex LeDonne

unread,
Dec 11, 2007, 7:40:29 PM12/11/07
to
On Dec 11, 2007 7:15 PM, Michael Boutros <m...@michaelboutros.com> wrote:
> Hello! More and more I find myself having to do something like this:
>
> def some_method(foo)
> results = []
> foo.each {|f| results << f.reverse}
> return results
> end
>
> As you can see, that is some pretty ugly code, but it's also a common
> scenario. How can I make it a little less ugly?
>
> Thanks,
> Michael Boutros

If I understand your goal, that's what map is for:

foo.map {|f| f.reverse}

"map" can also be spelled "collect".

-A

Andrei Maxim

unread,
Dec 11, 2007, 7:41:11 PM12/11/07
to
You don't need to define a method for that.

foo.each { |word| word.reverse! }

should do the trick.


--
Andrei Maxim
http://andreimaxim.ro

Suraj Kurapati

unread,
Dec 11, 2007, 7:41:23 PM12/11/07
to
Michael Boutros wrote:
> def some_method(foo)
> results = []
> foo.each {|f| results << f.reverse}
> return results
> end
>
> As you can see, that is some pretty ugly code, but it's also a common
> scenario. How can I make it a little less ugly?

def some_method foo
foo.map {|f| f.reverse}
end

Michael Boutros

unread,
Dec 11, 2007, 8:49:31 PM12/11/07
to
Suraj Kurapati wrote:
> Michael Boutros wrote:
>> def some_method(foo)
>> results = []
>> foo.each {|f| results << f.reverse}
>> return results
>> end
>>
>> As you can see, that is some pretty ugly code, but it's also a common
>> scenario. How can I make it a little less ugly?
>
> def some_method foo
> foo.map {|f| f.reverse}
> end

Thanks for that, I'll be using that more often now :) However, I'm
having trouble refactoring this code into something like yours...

def get_text(*elements)
results = []
elements.each {|element| results << self.search(element).first}
return results

Phrogz

unread,
Dec 11, 2007, 9:25:37 PM12/11/07
to

def get_text( *elements )
elements.map{ |el| search( element ).first }
end

Bernardo Monteiro Rufino

unread,
Dec 11, 2007, 9:14:49 PM12/11/07
to
Note: parts of this message were removed by the gateway to make it a legal Usenet post.

def get_text(*elements)
elements.map{|element| self.search(element).first}
end

Sascha Abel

unread,
Dec 12, 2007, 5:31:56 AM12/12/07
to
Michael Boutros wrote:

> Suraj Kurapati wrote:
> def get_text(*elements)
> results = []
> elements.each {|element| results << self.search(element).first}
> return results
> end

def get_text(*elements)
elements.map {|elem| self.search(elem).first }
end

This should do what you want.

Greetings,
Sascha

Michael Boutros

unread,
Dec 12, 2007, 3:16:24 PM12/12/07
to
Thanks so much for all the replies :) So, what I've gathered is that map
basically loops through all the values of an array, does whatever the
block says to do, and then places it back into the array. Correct?

- Michael Boutros

Rob Biedenharn

unread,
Dec 12, 2007, 4:12:34 PM12/12/07
to
On Dec 12, 2007, at 3:16 PM, Michael Boutros wrote:

> Thanks so much for all the replies :) So, what I've gathered is that
> map
> basically loops through all the values of an array, does whatever the
> block says to do, and then places it back into the array. Correct?
>
> - Michael Boutros


map creates a new array

If you really want to modify the array in place, you can use map!
instead.

-Rob

Rob Biedenharn http://agileconsultingllc.com
R...@AgileConsultingLLC.com

Chad Perrin

unread,
Dec 13, 2007, 8:43:17 PM12/13/07
to
On Thu, Dec 13, 2007 at 05:16:24AM +0900, Michael Boutros wrote:
> Thanks so much for all the replies :) So, what I've gathered is that map
> basically loops through all the values of an array, does whatever the
> block says to do, and then places it back into the array. Correct?

If I understand your sentence correctly -- no, that's incorrect.

My understanding is that you expect #map or #collect to do this:

foo = %w(rgb irs rib)
foo.map {|f| f.gsub('r', 'e')}
puts foo
> egb
> ies
> eib

What it actually does is this:

foo = %w(rgb irs rib)
bar = foo.map {|f| f.gsub('r', 'e')}
puts foo
> rgb
> irs
> rib
puts bar
> egb
> ies
> eib

Thus, the proposed method declaration:

def some_method(foo)
foo.map { |f| f.reverse }
end

. . would return the reversed list, exactly the same as a version with
an explicit return statement. It does not alter the provided variable at
all, but instead outputs a brand-new list object.

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
They always say that when life gives you lemons you should make lemonade.
I always wonder -- isn't the lemonade going to suck if life doesn't give
you any sugar?

0 new messages