Created
November 15, 2018 19:23
-
-
Save kkuchta/ccf1ba79339ebdc47f4df17ab50c2ca3 to your computer and use it in GitHub Desktop.
This file contains 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
console = Object.new | |
def console.log(*x) | |
puts x.map(&:to_s).join(',') | |
end | |
class Object | |
def method_missing(*args) | |
return args[0] | |
end | |
end | |
def function(*args, &block) | |
func_name = :"func_#{rand(1000000)}" | |
impl_name = :"func_#{rand(1000000)}" | |
klass = Class.new { attr_accessor *args } | |
function_block = Proc.new { |*arg_values| | |
obj = klass.new | |
klass.send(:define_method, impl_method_name, &block) | |
args.zip(arg_values).each {|arg, arg_value| obj.send(:"#{arg}=", arg_value) } | |
obj.send(impl_method_name) | |
} | |
define_method(func_name, &function_block) | |
func_name | |
end | |
define_method(:var) { |random_function_name| | |
var_name = local_variables.find do |local_var| | |
local_var != :random_function_name && eval(local_var.to_s) == random_function_name | |
end | |
define_method(var_name) { |*args| | |
send(random_function_name, *args) | |
} | |
} | |
var foo = function(a, b) { | |
return a + b | |
} | |
var first = 3; | |
var second = 4; | |
var sum = function(a, b) { | |
a + b; | |
} | |
console.log("Sum = ", sum(first, second)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you'll want to define your
method_missing
function on object as:...like you did in your talk.
Great presentation, btw! My favorite one that I saw at RubyConf!