Functions as Objects

DevOps By Rultor.com We recommend RubyMine

rake PDD status Gem Version Test Coverage Yard Docs Hits-of-Code License

Let's say you need an object that consists of a single function:

require 'others'
x = others(foo: 42) do |*args|
  @foo + args[1]
end
assert(x.bar(10) == 52)

You can also do this in a class:

require 'others'
class Foo
  def foo(a)
    a + 1
  end
  others do |*args|
    args[1] + 10
  end
end
assert(x.foo(10) == 11)
assert(x.bar(42) == 52)

That's it.

How to contribute

Read these guidelines. Make sure you build is green before you contribute your pull request. You will need to have Ruby 3.2+ and Bundler installed. Then:

bundle update
bundle exec rake

If it's clean and you don't see any error messages, submit your pull request.