self 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/self.rb +45 -0
  2. metadata +50 -0
@@ -0,0 +1,45 @@
1
+ class Object
2
+ # Returns an object that lets you access instance variables of and call
3
+ # private methods on this Object.
4
+ #
5
+ # @example
6
+ #
7
+ # class House
8
+ # def initialize(windows)
9
+ # @windows = windows
10
+ # end
11
+ #
12
+ # private
13
+ #
14
+ # def window_cleaning_bill
15
+ # @windows * 10.00
16
+ # end
17
+ # end
18
+ #
19
+ # h = House.new(4)
20
+ # h.self.windows
21
+ # # => 4
22
+ # h.self.windows = 6
23
+ # # => 6
24
+ # h.self.window_cleaning_bill
25
+ # # => 60.0
26
+ #
27
+ def self
28
+ ivars = instance_variables.map{ |x| x.to_s.sub('@', '') }
29
+ methods = (protected_methods - Object.protected_instance_methods) + (private_methods - Object.private_instance_methods)
30
+
31
+ Class.new do
32
+ define_method(:initialize) { |_self| @self = _self }
33
+ define_method(:inspect) { "#{@self.inspect}.self" }
34
+
35
+ methods.each do |method|
36
+ define_method(method) { |*a, &b| @self.send(method, *a, &b) }
37
+ end
38
+
39
+ ivars.each do |ivar|
40
+ define_method(ivar) { @self.instance_variable_get '@' + ivar }
41
+ define_method(ivar + '=') { |value| @self.instance_variable_set '@' + ivar, value }
42
+ end
43
+ end.new(self)
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: self
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Conrad Irwin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-14 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: When debugging in pry, it's useful to be able to access the instance
15
+ variables of any object. This gem makes that trivial! Using foo.self.bar will give
16
+ you @bar from the foo object, all while allowing tab-completion!
17
+ email:
18
+ - me@cirw.in
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/self.rb
24
+ homepage: https://github.com/ConradIrwin/self
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.23
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Provides easy (tab-completable) access to instance variables and private
48
+ methods
49
+ test_files: []
50
+ has_rdoc: