Coder Social home page Coder Social logo

forwardable's Introduction

Forwardable

The Forwardable module provides delegation of specified methods to a designated object, using the methods #def_delegator and #def_delegators.

Installation

Add this line to your application's Gemfile:

gem 'forwardable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install forwardable

Usage

For example, say you have a class RecordCollection which contains an array @records. You could provide the lookup method #record_number(), which simply calls #[] on the @records array, like this:

  require 'forwardable'

  class RecordCollection
    attr_accessor :records
    extend Forwardable
    def_delegator :@records, :[], :record_number
  end

We can use the lookup method like so:

  r = RecordCollection.new
  r.records = [4,5,6]
  r.record_number(0)  # => 4

Further, if you wish to provide the methods #size, #<<, and #map, all of which delegate to @records, this is how you can do it:

  class RecordCollection # re-open RecordCollection class
    def_delegators :@records, :size, :<<, :map
  end

  r = RecordCollection.new
  r.records = [1,2,3]
  r.record_number(0)   # => 1
  r.size               # => 3
  r << 4               # => [1, 2, 3, 4]
  r.map { |x| x * 2 }  # => [2, 4, 6, 8]

You can even extend regular objects with Forwardable.

  my_hash = Hash.new
  my_hash.extend Forwardable              # prepare object for delegation
  my_hash.def_delegator "STDOUT", "puts"  # add delegation for STDOUT.puts()
  my_hash.puts "Howdy!"

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/forwardable.

License

The gem is available as open source under the terms of the 2-Clause BSD License.

forwardable's People

Contributors

akr avatar alexwayfer avatar bobby02832 avatar dependabot[bot] avatar drbrain avatar hsbt avatar ioquatix avatar jeremyevans avatar k0kubun avatar knu avatar mame avatar marcandre avatar nobu avatar nurse avatar olleolleolle avatar rm155 avatar shugo avatar shyouhei avatar sorah avatar stratigos avatar unak avatar znz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

forwardable's Issues

def_*_delegator from v1.3.0 does not return name of delegator method in Ruby 2.6 and older

In v1.2.0, def_delegator :@obj, :data, :fallback_data would return :fallback_data but v1.3.0 just returns nil

The cause for this is the last expression mod.send(:ruby2_keywords, ali) if RUBY_VERSION >= '2.7' in the following definition:

def def_instance_delegator(accessor, method, ali = method)
gen = Forwardable._delegator_method(self, accessor, method, ali)
# If it's not a class or module, it's an instance
mod = Module === self ? self : singleton_class
mod.module_eval(&gen)
mod.send(:ruby2_keywords, ali) if RUBY_VERSION >= '2.7'
end

The last expression is evaluated only if RUBY_VERSION >= '2.7'.
The preferred solution would be to stash the result of the previous expression and return that stashed value for Ruby 2.6 and older:

 def def_instance_delegator(accessor, method, ali = method)
   gen = Forwardable._delegator_method(self, accessor, method, ali)

   # If it's not a class or module, it's an instance
   mod = Module === self ? self : singleton_class

   # stash in a lvar
   result = mod.module_eval(&gen)
   RUBY_VERSION >= '2.7' ? mod.send(:ruby2_keywords, ali) : result
 end

Based on the diff of #5, this issue would affect :single_delegator as well.

/cc @jeremyevans @hsbt Requesting a patch to be shipped at the earliest.
Thank you.

Add CI ?

would be nice if PRs had automated testing

Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call

Hello.

I see ruby2_keywords usage, but I'm getting such warnings for some reason in my project:

/home/alex/Projects/ruby/forwardable/lib/forwardable.rb:238: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/home/alex/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/ferrum-0.9/lib/ferrum/page/screenshot.rb:35: warning: The called method `pdf' is defined here
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]

I see this referenced source: https://github.com/rubycdp/ferrum/blob/6906d98/lib/ferrum/page/screenshot.rb#L35

def pdf(**opts)

What changes are expected and where?

already initialized constant Forwardable::FORWARDABLE_VERSION

Hi at forwardable,

You might want to fix this one:

/usr/local/lib/ruby/gems/2.6.0/gems/forwardable-1.3.1/lib/forwardable/version.rb:3: warning: previous definition of VERSION was here
/usr/local/lib/ruby/2.6.0/forwardable.rb:117: warning: already initialized constant Forwardable::FORWARDABLE_VERSION

It actually is the last gem in my bundle
that gives me a warning message like that.
All the other gems cleaned that up already.

Let's bring this forward!

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.