Guest User

Untitled

a guest
Jul 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #
  2. # Define handy methods for debug in irb.
  3. #
  4. module Handy
  5. # Call Kernel#p method with self.
  6. def _p
  7. p self
  8. self
  9. end
  10.  
  11. # Call Kernel#pp method with self.
  12. def _pp
  13. pp self
  14. self
  15. end
  16.  
  17.  
  18. # Call Kernel#puts method with self.
  19. def pt
  20. puts self
  21. self
  22. end
  23.  
  24. # Stans for 'puts methods'.
  25. #
  26. # Show instance methods with detail.
  27. def pm(pattern = nil)
  28. klass = self.is_a?(Class) ? self : self.class
  29. ancs = klass.ancestors
  30. mtds = self.methods
  31. mtds = mtds.grep(pattern) if pattern
  32. mtds.sort.each do |e|
  33. ent = ancs.find {|c| c.instance_methods(false).include?(e) }
  34. ent ||= "self"
  35. puts "#{e} in #{ent}"
  36. end
  37. end
  38.  
  39. # Stans for 'puts ancestors'.
  40. #
  41. # Show ancestors for self class. Included modules show with prefix '-'.
  42. def pa
  43. klass = self.is_a?(Class) ? self : self.class
  44. inc_mods = klass.included_modules
  45. klass.ancestors.each do |c|
  46. puts inc_mods.include?(c) ? "-#{c}" : "#{c}"
  47. end
  48. end
  49.  
  50. # Stans for 'puts hierarchy'.
  51. #
  52. # Show classes extended or included self class.
  53. def ph
  54. klass = self.is_a?(Class) ? self : self.class
  55. ObjectSpace.each_object(Class).select {|c| c < klass }.pt
  56. end
  57.  
  58. # Stans for 'puts class variables'.
  59. #
  60. # Show class variables name with value.
  61. def pcv
  62. klass = self.is_a?(Class) ? self : self.class
  63. klass.class_variables.each do |vn|
  64. puts "#{vn} = #{klass.class_eval(vn).inspect}"
  65. end
  66. end
  67.  
  68. # Stans for 'puts instance variables'.
  69. #
  70. # Show instance variables name with value.
  71. def piv
  72. self.instance_variables.each do |vn|
  73. puts "#{vn} = #{self.instance_eval(vn).inspect}"
  74. end
  75. end
  76. end
  77.  
  78. Object.send(:include, Handy)
Add Comment
Please, Sign In to add comment