Skip to content

Instantly share code, notes, and snippets.

@Keoven
Last active December 26, 2015 09:48
Show Gist options
  • Save Keoven/7131712 to your computer and use it in GitHub Desktop.
Save Keoven/7131712 to your computer and use it in GitHub Desktop.
Static Typing Experiment
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