instance_call 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +7 -0
- data/README.md +46 -0
- data/lib/core_ext/object.rb +20 -0
- data/lib/instance_call.rb +1 -0
- metadata +98 -0
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
Installation
|
2
|
+
============
|
3
|
+
|
4
|
+
```bash
|
5
|
+
gem install instance_call
|
6
|
+
```
|
7
|
+
|
8
|
+
Why?
|
9
|
+
====
|
10
|
+
|
11
|
+
Introduces method Object#instance_call what takes Symbol, Proc or block and executes it in the context of instance.
|
12
|
+
|
13
|
+
|
14
|
+
Usage
|
15
|
+
=====
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
require 'instance_call'
|
19
|
+
```
|
20
|
+
|
21
|
+
### Examples:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
array = [1, 2, 3]
|
25
|
+
string = "abc"
|
26
|
+
```
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
array.instance_call(:last) # 3
|
30
|
+
array.instance_call(:push, string) # [1, 2, 3, "abc"]
|
31
|
+
array.instance_call(lambda { [self, last]}) # [[1, 2, 3, "abc"], "abc"] - Note that self is not main
|
32
|
+
array.instance_call() {|| self } # [1, 2, 3, "abc"]
|
33
|
+
```
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
unbound_method = Array.instance_method(:pop)
|
37
|
+
array.instance_call(unbound_method) # "abc"
|
38
|
+
```
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
bound_method = array.method(:pop)
|
42
|
+
array.instance_call(bound_method) # 3
|
43
|
+
```
|
44
|
+
|
45
|
+
|
46
|
+
More examples can be seen in Rspec tests: https://github.com/tione/instance_call/tree/master/spec
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Object
|
2
|
+
|
3
|
+
def instance_call(m=nil, *args, &block)
|
4
|
+
m ||= block
|
5
|
+
case m
|
6
|
+
when Symbol
|
7
|
+
send(m, *args, &block)
|
8
|
+
when Proc
|
9
|
+
instance_exec(*args, &m)
|
10
|
+
when UnboundMethod
|
11
|
+
bound = m.bind(self)
|
12
|
+
bound.call(*args, &block)
|
13
|
+
when Method
|
14
|
+
m.call(*args, &block)
|
15
|
+
else
|
16
|
+
raise ArgumentError, "Unsupported type #{m.class} and no block specified"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'core_ext/object'
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: instance_call
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Margus Pärt
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: yard
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.8.1
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.8.1
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.10.0
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.10.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec-core
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.10.0
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.10.0
|
62
|
+
description: On Object#instance_call its possible to call Symbol, Proc, Block, UnboundMethod
|
63
|
+
and Method
|
64
|
+
email: margus@tione.eu
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- README.md
|
70
|
+
- Gemfile
|
71
|
+
- lib/core_ext/object.rb
|
72
|
+
- lib/instance_call.rb
|
73
|
+
homepage: https://github.com/tione/instance_call
|
74
|
+
licenses: []
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: 1.8.7
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.8.24
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: Introduces Object#instance_call.
|
97
|
+
test_files: []
|
98
|
+
has_rdoc:
|