inverse_methods 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b107057c14015315b7c09b3d9c342f848b50eb57
4
+ data.tar.gz: ed2ed1c3b559bdb011d15841d1040b95b2236b13
5
+ SHA512:
6
+ metadata.gz: '0702282f19a800fe6ce2d0cb3dccfac7c16ab26fcc113828932de87fb81b00ca25aadf6d52ce7ad3642e99d97852033b083294037e9e8aecbc109c8ae5d82fdd'
7
+ data.tar.gz: dff2fcb76a39d5d6e63748c5465d171dd0241d8ea23a5bfa7692beb0f2f50501d833979dcf50112a4a78400fd91ff8484223f93f9b9c91af1c8e1bdfdce3ff18
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ require 'inverse_methods'
3
+ class InverseMethods::CLI < Thor
4
+ desc "test", "run tests"
5
+ def test
6
+ puts "No tests have been wrritten"
7
+ exit
8
+ end
9
+ end
10
+ InverseMethods::CLI.start ARGV
@@ -0,0 +1,31 @@
1
+ require 'debug_inspector'
2
+ require 'byebug'
3
+
4
+ EvalProc = ->(sym, argument, context){
5
+ eval %{ #{sym}(Marshal.load "#{Marshal.dump argument}") }, context
6
+ }
7
+
8
+ module InverseMethods
9
+
10
+ def pass_to(*syms)
11
+ RubyVM::DebugInspector.open { |inspector|
12
+ caller_context = inspector.frame_binding(2)
13
+ syms.each { |sym| EvalProc.call(sym, self, caller_context) }
14
+ }
15
+ end
16
+
17
+ def chain_to(*syms)
18
+ RubyVM::DebugInspector.open { |inspector|
19
+ caller_context = inspector.frame_binding(2)
20
+ initial_result = EvalProc.call syms.shift, self, caller_context
21
+ syms.reduce(initial_result) { |result, sym|
22
+ EvalProc.call sym, result, caller_context
23
+ }
24
+ }
25
+ end
26
+
27
+ refine Object do
28
+ include InverseMethods
29
+ end
30
+
31
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module InverseMethods
2
+ VERSION = '0.0.1'
3
+ end
data/readme.md ADDED
@@ -0,0 +1,27 @@
1
+ Two methods which can be added on Object:
2
+
3
+ Both use `debug_inspector` to do some stuff with Ruby that wouldn't otherwise be possible.
4
+
5
+ `pass_to` is kind of like a reverse tap. It still works as a "tap" (it returns the caller) but takes arguments differently:
6
+
7
+ foo = []
8
+ 1.pass_to *%i{ foo.push foo.push }
9
+ puts foo # => [1,1]
10
+
11
+ 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
+
13
+ The above example, if modified like so, wouldn't work:
14
+
15
+ foo = []
16
+ 1.pass_to :foo.push :foo.push
17
+ puts foo
18
+
19
+ 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.
20
+
21
+ 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
+
23
+ foo = []
24
+ [1].chain_to *%i{[0].concat [2].concat}
25
+ puts foo # => [0,1,2]
26
+
27
+ Only the first symbol (`:"[].concat"`) gets the original argument, `[1]`, passed when evaluated. The next symbol `:"[2].concat"` gets the first evaluation's result (`[0, 1]`) passed.
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: inverse_methods
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - maxpleaner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: debug_inspector
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: ''
28
+ email: maxpleaner@gmail.com
29
+ executables:
30
+ - inverse_methods
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - bin/inverse_methods
35
+ - lib/inverse_methods.rb
36
+ - lib/version.rb
37
+ - readme.md
38
+ homepage: http://github.com/maxpleaner/inverse_methods
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - "~>"
49
+ - !ruby/object:Gem::Version
50
+ version: '2.3'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 2.5.1
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.5.1
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: two added methods on Object
62
+ test_files: []