README.md in rubype-0.0.1 vs README.md in rubype-0.1.0

- old
+ new

@@ -1,31 +1,116 @@ -# Rubype +# Ruby with Type. -TODO: Write a gem description +Matz has mentioned Ruby3.0 with static type at some confluences. But almost all rubyists(include me) are not sure how typed Ruby is. -## Installation +But it's worth thinking more. This gem is kind of trial without so much side-effect. -Add this line to your application's Gemfile: +```rb +require 'rubype' +# ex1 +class MyClass + def sum(x, y) + x + y + end + type Numeric, Numeric >= Numeric, :sum + + def wrong_sum(x, y) + 'string' + end + type Numeric, Numeric >= Numeric, :sum +end + +MyClass.new.sum(1, 2) +#=> 3 + +MyClass.new.sum(1, 'string') +#=> ArgumentError: Wrong type of argument, type of "str" should be Numeric + +MyClass.new.wrong_sum(1, 2) +#=> TypeError: Expected wrong_sum to return Numeric but got "str" instead + +# ex2: (Ruby 2.1.0+) +class MyClass + type Numeric, Numeric >= Numeric, def sum(x, y) + x + y + end + + type Numeric, Numeric >= Numeric, def wrong_sum(x, y) + 'string' + end +end + +# ex3: (Ruby 2.1.0+) +class People + type People >= Any, def marry(people) + # Your Ruby code as usual + end +end + +People.new.marry(People.new) +#=> no error + +People.new.marry('non people') +#=> ArgumentError: Wrong type of argument, type of "non people" should be People +``` + +## Feature +### Typed method can coexist with non-typed method + ```ruby -gem 'rubype' +# It's totally OK!! +class MyClass + def sum(x, y) + x + y + end + type Numeric, Numeric >= Numeric, :sum + + def wrong_sum(x, y) + 'string' + end +end ``` -And then execute: +### Duck typing - $ bundle +```ruby -Or install it yourself as: +class MyClass + def foo(any_obj) + 1 + end + type Any >= Numeric, :foo +end - $ gem install rubype +# It's totally OK!! +MyClass.new.foo(1) +# It's totally OK!! +MyClass.new.foo('str') +``` -## Usage +## Installation -TODO: Write usage instructions here +gem install rubype or add gem 'rubype' to your Gemfile. -## Contributing +This gem requires Ruby 2.0.0+. -1. Fork it ( https://github.com/[my-github-username]/rubype/fork ) -2. Create your feature branch (`git checkout -b my-new-feature`) -3. Commit your changes (`git commit -am 'Add some feature'`) -4. Push to the branch (`git push origin my-new-feature`) -5. Create a new Pull Request +### Contributing + +Fork it ( https://github.com/[my-github-username]/rubype/fork ) + +Create your feature branch (`git checkout -b my-new-feature`) + + $ bundle install --path vendor/bundle + +Commit your changes (`git commit -am 'Add some feature'`) + + $ bundle exec rake test + + > 5 runs, 39 assertions, 0 failures, 0 errors, 0 skips + +Push to the branch (`git push origin my-new-feature`) + +Create a new Pull Request + +## Credits +[@chancancode](https://github.com/chancancode) first brought this to my attention. I've stolen some idea from him.