inverse_methods 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/lib/inverse_methods.rb +3 -0
- data/lib/version.rb +1 -1
- data/readme.md +23 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a00cfd4c5c28a08d41837a2281cd898397de830
|
4
|
+
data.tar.gz: 57b9ef38aa446c7b34cafb8ddabd7085295ad30e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f21f8419c2e40b9bd5bccd0584dfb942b59ca41f14023297a00ec3bda19aaf96113a19e64df3fe3480c6490baa4f3ec4741937df6fb37f412971ab4cbd87ef3
|
7
|
+
data.tar.gz: 7bc2cd0367f58c1ed88872c58993ec258eecfdef9383b89a86887a4473eb921a6fc63e2a2a7dc8dc666f384c00a84246fa5106bfce39a7577a0e36148b3b5ea4
|
data/lib/inverse_methods.rb
CHANGED
@@ -8,13 +8,16 @@ EvalProc = ->(sym, argument, context){
|
|
8
8
|
module InverseMethods
|
9
9
|
|
10
10
|
def pass_to(*syms)
|
11
|
+
syms = syms[0] if syms[0].is_a?(Array)
|
11
12
|
RubyVM::DebugInspector.open { |inspector|
|
12
13
|
caller_context = inspector.frame_binding(2)
|
13
14
|
syms.each { |sym| EvalProc.call(sym, self, caller_context) }
|
14
15
|
}
|
16
|
+
self
|
15
17
|
end
|
16
18
|
|
17
19
|
def chain_to(*syms)
|
20
|
+
syms = syms[0] if syms[0].is_a?(Array)
|
18
21
|
RubyVM::DebugInspector.open { |inspector|
|
19
22
|
caller_context = inspector.frame_binding(2)
|
20
23
|
initial_result = EvalProc.call syms.shift, self, caller_context
|
data/lib/version.rb
CHANGED
data/readme.md
CHANGED
@@ -1,3 +1,18 @@
|
|
1
|
+
### Install
|
2
|
+
|
3
|
+
`gem install inverse_methods`
|
4
|
+
`require 'inverse_methods'`
|
5
|
+
|
6
|
+
## Accessing methods
|
7
|
+
|
8
|
+
There is a single module `InverseMethods` which can be included where it's needed or patched on `Object` for global access.
|
9
|
+
|
10
|
+
To load it globally, use `Object.include InverseMethods`. Or use a more specific scope such as `MyClass.include InverseMethods` or `MyClass.extend InverseMethods`.
|
11
|
+
|
12
|
+
It can be used as a refinement: `using InverseMethods`.
|
13
|
+
|
14
|
+
### Explanation
|
15
|
+
|
1
16
|
Two methods which can be added on Object:
|
2
17
|
|
3
18
|
Both use `debug_inspector` to do some stuff with Ruby that wouldn't otherwise be possible.
|
@@ -5,15 +20,18 @@ Both use `debug_inspector` to do some stuff with Ruby that wouldn't otherwise be
|
|
5
20
|
`pass_to` is kind of like a reverse tap. It still works as a "tap" (it returns the caller) but takes arguments differently:
|
6
21
|
|
7
22
|
foo = []
|
8
|
-
1.pass_to
|
23
|
+
1.pass_to %i{ foo.push foo.push }
|
24
|
+
# => 1
|
9
25
|
puts foo # => [1,1]
|
10
26
|
|
11
27
|
The caller is being passed as an argument (serialized using `Marshal.dump`), and the symbols in the list are methods which it is passed to. They are evaluated in the caller context, which is why the local variable `foo` can be referenced.
|
12
28
|
|
29
|
+
The method names passed to `pass_to` are not chained. This is why `[1,1]` was returned instead of `[1, [...]]` (the three dots being a recursive reference). The `foo.push 1` calls happen independently of one another.
|
30
|
+
|
13
31
|
The above example, if modified like so, wouldn't work:
|
14
32
|
|
15
33
|
foo = []
|
16
|
-
1.pass_to :foo.push :foo.push
|
34
|
+
1.pass_to [:foo.push, :foo.push]
|
17
35
|
puts foo
|
18
36
|
|
19
37
|
The reason is that `:foo.push` is a `SyntaxError`. It needs to be written as `:"foo.push"`, which the `%i{ array.of symbols }` shorthand would do automatically.
|
@@ -21,7 +39,7 @@ The reason is that `:foo.push` is a `SyntaxError`. It needs to be written as `:"
|
|
21
39
|
The second method is `chain_to`, which is perhaps the 'functional programming' alternative to `pass_to` (and `tap`, which it's based on). It works similarly:
|
22
40
|
|
23
41
|
foo = []
|
24
|
-
[1].chain_to
|
25
|
-
puts foo # => [
|
42
|
+
[1].chain_to %i{[2].concat [3].concat}
|
43
|
+
puts foo # => [3,2,1]
|
26
44
|
|
27
|
-
Only the first symbol (`:"[].concat"`) gets the original argument, `[1]`, passed when evaluated. The next symbol `:"[
|
45
|
+
Only the first symbol (`:"[2].concat"`) gets the original argument, `[1]`, passed when evaluated. The next symbol `:"[3].concat"` gets the first evaluation's result (`[2, 1]`) passed.
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: inverse_methods
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- maxpleaner
|
@@ -55,7 +55,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
55
|
version: 2.5.1
|
56
56
|
requirements: []
|
57
57
|
rubyforge_project:
|
58
|
-
rubygems_version: 2.
|
58
|
+
rubygems_version: 2.6.11
|
59
59
|
signing_key:
|
60
60
|
specification_version: 4
|
61
61
|
summary: two added methods on Object
|