How To Avoid Ugly Declerations

M

Michael Boutros

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
 
J

Justin Collins

Michael said:
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
 
Y

yermej

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

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
 
A

Alex LeDonne

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
 
A

Andrei Maxim

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

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

should do the trick.

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
 
S

Suraj Kurapati

Michael said:
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
 
M

Michael Boutros

Suraj said:
Michael said:
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
 
B

Bernardo Monteiro Rufino

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
 
P

Phrogz

Suraj said:
Michael said:
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
 
S

Sascha Abel

Michael said:
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
 
M

Michael Boutros

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
 
R

Rob Biedenharn

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
(e-mail address removed)
 
C

Chad Perrin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,274
Messages
2,571,366
Members
48,051
Latest member
noblesalt

Latest Threads

Top