daily pastebin goal
60%
SHARE
TWEET

Untitled

a guest May 16th, 2018 118 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. irb
  2. >> module RangedIndex
  3. >>   def [] index
  4. >>     puts "RANGED INDEX CALLED"
  5. >>     if index.class == Range
  6. >>       new_array = []
  7. >>       self.keys.each{|k|new_array |= old_index(k) if index.include? k }
  8. >>       new_array
  9. >>     else
  10. ?>       old_index(index)
  11. >>     end
  12. >>   end
  13. >>  
  14. ?>   def self.extend_object(object)
  15. >>     puts "extend called!"
  16. >>     class << object
  17. >>       alias_method :old_index, :[]
  18. >> #      alias_method :[], :ranged_index
  19. ?>     end
  20. >>   end
  21. >> end
  22. => nil
  23. >>
  24. ?> h = Hash.new
  25. => {}
  26. >> h[1] = [1,2,3]
  27. => [1, 2, 3]
  28. >> h.extend RangedIndex
  29. extend called!
  30. => {1=>[1, 2, 3]}
  31. >> h[1]
  32. => [1, 2, 3]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top