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

ruby-method-arguments

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ruby-method-arguments

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

method_arguments gem

With the method_arguments gem you can easily set object attributes from method arguments.

Transform this:

  def initialize(user_entity, first_name:, last_name:, email:, phone:, dob:)
    @user_entity = user_entity
    @first_name = first_name
    @last_name = last_name
    @email = email
    @phone = phone
    @dob = dob
  end

into this streamlined version:

  def initialize(user_entity, first_name:, last_name:, email:, phone:, dob:)
    set_instance_vars(__method_args__)
  end

Features

1. Reading Method Arguments

Within any method, call __method_args__ to retrieve its arguments as a Hash.

  def initialize(user_entity, first_name:, last_name:, age:, **kw_rest)
    "Method ##{__method__} received #{__method_args__.inspect} arguments"
  end
Limitations
  1. Argument types *rest and **keyrest are ignored regardless of their name:

    def initialize(arg, *arg_rest, kw_arg:, **kw_rest)
      __method_args__ # returns { a: <value>, kw_arg: <value> }
    end
    

    Casting unknown arbitrary arguments to instance variables is not recommended for security reasons.

  2. The method does not support argument forwarding:

    def self.call(...)
      new(__method_args__).call # raises error
    end
    

2. Filling instance @variables

Use set_instance_vars(hash) to assign instance variables from a Hash.

3. Filling instance @variables using attribute writers

To leverage setters (attribute writers), add use_writers: true argument. If a writer with the corresponding name exists, it will be invoked.

Example

Before:

def initialize(user_entity, first_name:, last_name:, age:)
  @user_entity = user_entity
  @first_name = first_name
  @last_name = last_name
  self.age = age
end

def age=(val)
  @age = Integer(val)
end

After:

def initialize(user_entity, first_name:, last_name:, age:, ...)
  set_instance_vars(__method_args__, use_writers: true)
end

def age=(val)
  @age = Integer(val)
end

Installation

  1. Add to your Gemfile, then run bundle install:

    gem "method_arguments"
    
  2. Prepend your code with

    require "method_arguments"
    

Supported Ruby Versions

This gem is designed to be compatible with Ruby 2.7 and above. It has been tested on the following MRI versions:

  • v2.7.2
  • v3.0.3
  • v3.1.2

If you encounter any issues with later versions, please feel free to report them.

FAQs

Package last updated on 05 Mar 2024

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