Skip to content

Instantly share code, notes, and snippets.

@mrb
Created April 28, 2014 12:46
Show Gist options
  • Save mrb/11370784 to your computer and use it in GitHub Desktop.
Save mrb/11370784 to your computer and use it in GitHub Desktop.
A Typed Class in Ruby
require 'rtc'
class Post
rtc_annotated
typesig('@id: String')
attr_accessor :id
typesig('@title: String')
attr_accessor :title
typesig('@timestamp: Fixnum')
attr_accessor :timestamp
typesig("self.build: (Hash) -> Post")
def self.build(attrs)
post = new.rtc_annotate("Post")
post.id = attrs.delete(:id)
post.title = attrs.delete(:title)
post.timestamp = attrs.delete(:timestamp)
post
end
end
Post.build({id: "100", title: "Cool", timestamp: "100"})
# -> ruby typed.rb
# /Users/mrb/.rbenv/versions/1.9.3-p545/lib/ruby/gems/1.9.1/gems/rtc-0.0.0/lib/rtc/runtime/method_check.rb:91:in `select_and_check_args': In method timestamp=, annotated types are [Rtc::Types::ProceduralType(10): [ (Fixnum) -> Fixnum ]], but actual arguments are ["100"], with argument types [NominalType(1)<String>] for class Post (Rtc::TypeMismatchException)
# from method_wrapper.rb:41:in `timestamp='
# from /Users/mrb/.rbenv/versions/1.9.3-p545/lib/ruby/gems/1.9.1/gems/rtc-0.0.0/lib/rtc/proxy_object.rb:470:in `method_missing'
# from typed.rb:20:in `build'
# from method_wrapper.rb:55:in `build'
# from typed.rb:25:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment