Last active
December 26, 2015 15:39
-
-
Save netoneko/7174768 to your computer and use it in GitHub Desktop.
~ patch for symbols. Requires https://github.com/banister/binding_of_caller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'binding_of_caller' | |
| class Symbol | |
| def ~ | |
| this = binding.of_caller(1).eval("self") | |
| this.method(self).to_proc | |
| end | |
| end | |
| def x | |
| "hello!" | |
| end | |
| def sum(a, b) | |
| a + b | |
| end | |
| def call(z) | |
| z.call | |
| end | |
| puts call ~:x | |
| def three_plus_two(sum_function) | |
| sum_function.call(3, 2) | |
| end | |
| puts three_plus_two ~:sum | |
| puts (~:sum)[7, 13] | |
| one_two_three = [1, 2, 3] | |
| puts one_two_three.reduce(0, &~:sum) | |
| puts one_two_three.reduce(0, &:+) | |
| def y | |
| def hello(name) | |
| "Hello, #{name}!" | |
| end | |
| ~:hello | |
| end | |
| puts one_two_three.map(&y).inspect | |
| plus_three = (~:sum).curry[3] | |
| puts one_two_three.map(&plus_three).inspect | |
| puts one_two_three.map(&plus_three).reduce(0, &~:sum) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment