[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How To Avoid Ugly Declerations

Michael Boutros

12/12/2007 12:16:00 AM

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

12 Answers

Justin Collins

12/12/2007 12:27:00 AM

0

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

yermej

12/12/2007 12:34:00 AM

0

On Dec 11, 6: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
> --
> Posted viahttp://www.ruby-....

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

Alex LeDonne

12/12/2007 12:40:00 AM

0

On Dec 11, 2007 7:15 PM, Michael Boutros <me@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

12/12/2007 12:41:00 AM

0

You don't need to define a method for that.

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

should do the trick.

On 12/12/07, Michael Boutros <me@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
> --
> Posted via http://www.ruby-....
>
>


--
Andrei Maxim
http://andr...

Suraj Kurapati

12/12/2007 12:41:00 AM

0

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
--
Posted via http://www.ruby-....

Michael Boutros

12/12/2007 1:50:00 AM

0

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
end
--
Posted via http://www.ruby-....

bermonruf

12/12/2007 2:15:00 AM

0

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

Phrogz

12/12/2007 2:26:00 AM

0

On Dec 11, 6:49 pm, Michael Boutros <m...@michaelboutros.com> wrote:
> 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
> end

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

Sascha Abel

12/12/2007 10:32:00 AM

0

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
--
Posted via http://www.ruby-....

Michael Boutros

12/12/2007 8:16:00 PM

0

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
--
Posted via http://www.ruby-....