Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

define_methods

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

define_methods

  • 1.0.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

DefineMethods

Defines multiple methods with common behavior.

Installation

Add this line to your application's Gemfile:

gem 'define_methods'

And then execute:

$ bundle

Or install it yourself as:

$ gem install define_methods

Usage

Here's a class with similar methods.

class Greet
  def hi(greeted)
    "Hi #{greeted}!"
  end

  def hello(greeted)
    "Hello #{greeted}!"
  end

  def goodbye(greeted)
    "Goodbye #{greeted}!"
  end
end

greet = Greet.new
greet.hello('world') # => "Hello world!"
greet.goodbye('world') # => "Goodbye world!"

DefineMethods#define_methods

Defines methods just like Module#define_method. However it accepts a list of method names as arguments. To use it, just extend the class like so.

class Greet
  extend DefineMethods

  define_methods(:hi, :hello, :goodbye) do |greeted|
    "#{__method__.capitalize} #{greeted}!"
  end
end

DefineMethods::call

Accepts the same arguments as define_methods. It returns a Module with the defined methods. Here's the equivalent class, but with one less line of code.

class Greet
  include DefineMethods.(:hi, :hello, :goodbye) do |greeted|
    "#{__method__.capitalize} #{greeted}!"
  end
end

DefineMethods::each

Accepts just the method definition block. It returns a class holding the same method definition for building modules later. Use this to share the same behavior across many classes.

Exclaiming = DefineMethods.each do |subject|
  "#{__method__.capitalize} #{subject}!"
end

Greet.include Exclaiming[:hi, :hello, :goodbye]
Command.include Exclaiming[:stop, :go, :turn]

Contributing

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

License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 05 Jun 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc