Created
October 14, 2012 06:08
-
-
Save amolpujari/3887565 to your computer and use it in GitHub Desktop.
override rassoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
def rassoc obj, place=1 | |
if place | |
place = place.to_i rescue -1 | |
return if place < 0 | |
end | |
self.each do |item| | |
next unless item.respond_to? :include? | |
if place | |
return item if item[place]==obj | |
else | |
return item if item.include? obj | |
end | |
end | |
nil | |
end | |
end | |
array = ["apple", "orange", "lemon"] | |
array2 = [["apple", "good taste", "red"], ["orange", "bad taste", "orange"], ["lemon" , "no taste", "yellow"]] | |
Hash[ array.map{ |fruit| [fruit, array2.rassoc(fruit, nil)]}] | |
array2 = [["good taste", "red", "apple"], ["no taste", "lemon", "yellow"], ["orange", "bad taste", "orange"]] | |
Hash[ array.map{ |fruit| [fruit, array2.rassoc(fruit, nil)]}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment