daily pastebin goal
0%
SHARE
TWEET

Untitled

a guest May 16th, 2018 123 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. >> module RangedIndex
  2. >>   def [] index
  3. >>     puts "RANGED INDEX CALLED"
  4. >>     if index.class == Range
  5. >>       new_array = []
  6. >>       self.keys.each{|k|new_array |= old_index(k) if index.include? k }
  7. >>       new_array
  8. >>     else
  9. ?>       old_index(index)
  10. >>     end
  11. >>   end
  12. >>  
  13. ?>   def self.extend_object(object)
  14. >>     puts "extend called!"
  15. >>     class << object
  16. >>       alias_method :old_index, :[]
  17. >> #      alias_method :[], :ranged_index
  18. ?>     end
  19. >>   end
  20. >> end
  21. => nil
  22. >> h = Hash.new
  23. => {}
  24. >> h[1] = [1,2,3]
  25. => [1, 2, 3]
  26. >> h.extend RangedIndex
  27. extend called!
  28. => {1=>[1, 2, 3]}
  29. >> h[2] = [4,5,6]
  30. => [4, 5, 6]
  31. >> h[3] = [7]
  32. => [7]
  33. >> h[(1..2)]
  34. => nil
RAW Paste Data
Top