Last active
December 26, 2015 09:48
-
-
Save Keoven/7131712 to your computer and use it in GitHub Desktop.
Static Typing Experiment
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 'pry' | |
class StrongType | |
def initialize | |
@binding = binding | |
end | |
def self.func(name, arguments, &method_body) | |
define_method(name) do |*args| | |
types = arguments.values | |
arg_names = arguments.keys | |
args.each_with_index do |a, i| | |
puts a.is_a? types[i] | |
eval "#{arg_names[i]} = #{a}", @binding | |
end | |
instance_exec(&method_body) | |
end | |
end | |
def method_missing(m, *args) | |
eval m.to_s, @binding | |
end | |
end | |
class Test < StrongType | |
func :test, | |
hello: String, | |
world: Integer do | |
puts hello | |
puts world | |
end | |
func :test_scope, | |
hello: String, | |
world: Integer do | |
@hello ||= 0 | |
@hello += 1 | |
puts @hello | |
end | |
def check_scope | |
puts @hello | |
end | |
end | |
x = Test.new | |
x.test 1, 2 | |
x.test_scope | |
x.test_scope | |
x.test_scope | |
x.check_scope |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment