Git Product home page Git Product logo

rubype's Introduction

Ruby + Type = Rubype

Gem Version Build Status Dependency Status Code Climate

# Assert class of both args is Numeric and class of return is String
def sum(x, y)
  (x + y).to_s
end
typesig :sum, [Numeric, Numeric] => String

# Assert first arg has method #to_i
def sum(x, y)
  x.to_i + y
end
typesig :sum, [:to_i, Numeric] => Numeric

This gem brings you advantage of type without changing existing code's behavior.

Feature

Advantage of type

  • Meaningful error
  • Executable documentation
  • Don't need to check type of method's arguments and return .
require 'rubype'

# ex1: Assert class of args and return
class MyClass
  def sum(x, y)
    x + y
  end
  typesig :sum, [Numeric, Numeric] => Numeric

  def wrong_sum(x, y)
    'string'
  end
  typesig :wrong_sum, [Numeric, Numeric] => Numeric
end

MyClass.new.sum(1, 2)
#=> 3

MyClass.new.sum(1, 'string')
#=> Rubype::ArgumentTypeError: Expected MyClass#sum's 2th argument to be Numeric but got "string" instead

MyClass.new.wrong_sum(1, 2)
#=> Rubype::ReturnTypeError: Expected MyClass#wrong_sum to return Numeric but got "string" instead


# ex2: Assert object has specified method
class MyClass
  def sum(x, y)
    x.to_i + y
  end
  typesig :sum, [:to_i, Numeric] => Numeric
end

MyClass.new.sum('1', 2)
#=> 3

MyClass.new.sum(:has_no_to_i, 2)
#=> Rubype::ArgumentTypeError: Expected MyClass#sum's 1th argument to have method #to_i but got :has_no_to_i instead


# ex3: You can use Any class, if you want
class People
  def marry(people)
    # Your Ruby code as usual
  end
  typesig :marry, [People] => Any
end

People.new.marry(People.new)
#=> no error

People.new.marry('non people')
#=> Rubype::ArgumentTypeError: Expected People#marry's 1th argument to be People but got "non people" instead

Typed method can coexist with non-typed method

# It's totally OK!!
class MyClass
  def method_with_type(x, y)
    x + y
  end
  typesig :sum, [Numeric, Numeric] => Numeric

  def method_without_type(x, y)
    'string'
  end
end

Duck typing

You can use Any class.

class MyClass
  def foo(any_obj)
    1
  end
  typesig :foo, [Any] => Numeric

  def sum(x, y)
    x.to_i + y
  end
  typesig :sum, [:to_i, Numeric] => Numeric
end

# It's totally OK!!
MyClass.new.foo(1)
# It's totally OK!!
MyClass.new.foo(:sym)


# It's totally OK!!
MyClass.new.sum(1, 2)
# It's totally OK!!
MyClass.new.sum('1', 2)

Check type info everywhere!

class MyClass
  def sum(x, y)
    x.to_i + y
  end
  typesig :sum, [:to_i, Numeric] => Numeric
end

MyClass.new.method(:sum).type_info
# => [:to_i, Numeric] => Numeric

MyClass.new.method(:sum).arg_types
# => [:to_i, Numeric]

MyClass.new.method(:sum).return_type
# => Numeric

Installation

gem install rubype or add gem 'rubype' to your Gemfile.

And reuire 'rubype', enjoy typed Ruby.

This gem requires Ruby 2.0.0+.

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

......

Finished in 0.010961s, 547.3953 runs/s, 5017.7903 assertions/s.

7 runs, 61 assertions, 0 failures, 0 errors, 0 skips

Push to the branch (git push origin my-new-feature)

Create a new Pull Request to develop branch

Credits

@chancancode and This article first brought this to my attention. I've stolen some idea from them.

rubype's People

Contributors

garbles avatar gogotanaka avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.