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

local_eval

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

local_eval

  • 0.2.7
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

LocalEval

(C) John Mair (banisterfiend) 2010

instance_eval without changing self

Using local_eval you get most of the benefits of instance_eval with very few of the drawbacks: local instance variables are accessible within the block and method lookups are directed to the correct receiver. Unlike instance_eval the self of the block is not changed.

LocalEval provides the local_eval and local_eval_with methods.

  • Install the gem: gem install local_eval
  • Read the documentation
  • See the source code

example: local_eval

Using local_eval we can add the functionality of the receiver to the block:

class C
  def hello(name)
    "hello #{name}!"
  end
end

o = C.new

@name = "John"
o.local_eval { hello(@name) } #=> "hello John!"

example: capture

Since local_eval does not alter the self inside a block, all methods with an implied receiver will be invoked with respect to this local self. This means that all mutator methods defined on the receiver will modify state on the block's self rather than on the receiver's self. This is unlikely to be the desired behaviour; and so we can use the capture method to redirect method lookup to the actual receiver. All code captured by the capture block will be instance_eval'd against the actual receiver of the method.

class C
  class << self
    attr_reader :hello
    def self.capture_test
     
      # this code will be run against C
      capture { @hello = :captured }

      # this code will be run against the block context
      @goodbye = :goobye
    end
  end
end

C.local_eval { capture_test }

C.hello #=> :captured
@goodbye #=> :goodbye

How it works

Makes use of companion libraries: Remix and Object2module

Thread Safety

local_eval is not threadsafe.

Companion Libraries

LocalEval is one of a series of experimental libraries that mess with the internals of Ruby to bring new and interesting functionality to the language, see also:

  • Remix - Makes ancestor chains read/write
  • Object2module - Enables you to include/extend Object/Classes.
  • Include Complete - Brings in module singleton classes during an include. No more ugly ClassMethods and included() hook hacks.
  • Prepend - Prepends modules in front of a class; so method lookup starts with the module
  • GenEval - A strange new breed of instance_eval

Contact

Problems or questions contact me at github

FAQs

Package last updated on 17 Nov 2010

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