Skip to content

Instantly share code, notes, and snippets.

@amolpujari
Created October 14, 2012 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amolpujari/3887565 to your computer and use it in GitHub Desktop.
Save amolpujari/3887565 to your computer and use it in GitHub Desktop.
override rassoc
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