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 B.
Michael B. 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
Suraj K. wrote:
Michael B. 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
Michael B. wrote:
Suraj K. 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
def get_text(*elements)
elements.map{|element| self.search(element).first}
end
On Dec 11, 6:15 pm, Michael B. [email protected] wrote:
Thanks,
Michael B.
Posted viahttp://www.ruby-forum.com/.
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
You don’t need to define a method for that.
foo.each { |word| word.reverse! }
should do the trick.
On Dec 11, 6:49 pm, Michael B. [email protected] wrote:
return results
end
def get_text( *elements )
elements.map{ |el| search( element ).first }
end
On Dec 11, 2007 7:15 PM, Michael B. [email protected] wrote:
Thanks,
Michael B.
If I understand your goal, that’s what map is for:
foo.map {|f| f.reverse}
“map” can also be spelled “collect”.
-A
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 B. wrote:
Thanks,
Michael B.
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
On Dec 12, 2007, at 3:16 PM, Michael B. 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?
map creates a new array
If you really want to modify the array in place, you can use map!
instead.
-Rob
Rob B. http://agileconsultingllc.com
[email protected]
On Thu, Dec 13, 2007 at 05:16:24AM +0900, Michael B. 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.