Module: Tins::Interpreter

### Home Classes Methods

### In Files

- lib/tins/dslkit.rb

### Methods

- #interpret
- #interpret_with_binding

### Class/Module Index

Quicksearch

- Tins
- Tins
- Tins
- Tins::Annotate
- Tins::AskAndSend
- Tins::Attempt
- Tins::Bijection
- Tins::Blank
- Tins::Blank::Array
- Tins::Blank::FalseClass
- Tins::Blank::Hash
- Tins::Blank::NilClass
- Tins::Blank::Numeric
- Tins::Blank::Object
- Tins::Blank::String
- Tins::Blank::TrueClass
- Tins::BlankSlate
- Tins::BlockSelf
- Tins::CasePredicate
- Tins::ClassMethod
- Tins::Concern
- Tins::Concern::ModuleMixin
- Tins::Constant
- Tins::ConstantMaker
- Tins::CountBy
- Tins::DSLAccessor
- Tins::DateDummy
- Tins::DateTimeDummy
- Tins::DeepConstGet
- Tins::DeepDup
- Tins::Deflect
- Tins::Deflect::DeflectError
- Tins::Deflect::Deflector
- Tins::Deflect::DeflectorCollection
- Tins::Delegate
- Tins::DynamicScope
- Tins::DynamicScope::Context
- Tins::Eigenclass
- Tins::ExtractLastArgumentOptions
- Tins::FileBinary
- Tins::FileBinary::ClassMethods
- Tins::FileBinary::Constants
- Tins::Find
- Tins::Find::Finder
- Tins::Find::Finder::PathExtension
- Tins::FromModule
- Tins::Full
- Tins::GO
- Tins::GO::EnumerableExtension
- Tins::Generator
- Tins::HashSymbolizeKeysRecursive
- Tins::HashUnion
- Tins::IfPredicate
- Tins::InstanceExec
- Tins::Interpreter
- Tins::Limited
- Tins::LinesFile
- Tins::LinesFile::LineExtension
- Tins::Memoize
- Tins::Memoize::CacheMethods
- Tins::MethodDescription
- Tins::MethodMissingDelegator
- Tins::MethodMissingDelegator::DelegatorClass
- Tins::MethodMissingDelegator::DelegatorModule
- Tins::Minimize
- Tins::ModuleGroup
- Tins::NamedSet
- Tins::Null
- Tins::Null::Kernel
- Tins::NullClass
- Tins::NullPlus
- Tins::Once
- Tins::P
- Tins::ParameterizedModule
- Tins::PartialApplication
- Tins::ProcCompose
- Tins::ProcPrelude
- Tins::RangePlus
- Tins::RequireMaybe
- Tins::Responding
- Tins::Rotate
- Tins::Scope
- Tins::SecureWrite
- Tins::SexySingleton
- Tins::SexySingleton::SingletonClassMethods
- Tins::Shuffle
- Tins::StringByteOrderMark
- Tins::StringCamelize
- Tins::StringUnderscore
- Tins::StringVersion
- Tins::StringVersion::Version
- Tins::Subhash
- Tins::SymbolMaker
- Tins::Terminal
- Tins::ThreadGlobal
- Tins::ThreadLocal
- Tins::TimeDummy
- Tins::TimeFreezer
- Tins::To
- Tins::ToProc
- Tins::Token
- Tins::UniqBy
- Tins::Write
- Object
- Object::DSLKit
- Object::Spruz
- Array
- Date
- DateTime
- Enumerable
- File
- Hash
- IO
- IRB
- Method
- Module
- Proc
- Range
- String
- Symbol
- Time
- UnboundMethod

No matching classes.

# Tins::Interpreter

### Public Instance Methods

interpret(source, *args) click to toggle source

Interpret the string source as a body of a block, while passing *args into the block.

A small example explains how the method is supposed to be used and how the *args can be fetched:

    class A
      include Tins::Interpreter
      def c
        3
      end
    end

    A.new.interpret('|a,b| a + b + c', 1, 2) # => 6

To use a specified binding see interpret_with_binding.

    # File lib/tins/dslkit.rb, line 113
    def interpret(source, *args)
      interpret_with_binding(source, binding, *args)
    end

interpret_with_binding(source, my_binding, *args) click to toggle source

Interpret the string source as a body of a block, while passing *args into the block and using my_binding for evaluation.

A small example:

    class A
      include Tins::Interpreter
      def c
        3
      end
      def foo
        b = 2
        interpret_with_binding('|a| a + b + c', binding, 1) # => 6
      end
    end
    A.new.foo # => 6

See also interpret.

    # File lib/tins/dslkit.rb, line 135
    def interpret_with_binding(source, my_binding, *args)
      path = '(interpret)'
      if source.respond_to? :to_io
        path = source.path if source.respond_to? :path
        source = source.to_io.read
      end
      block = lambda { |*a| eval("lambda { #{source} }", my_binding, path).call(*a) }
      instance_exec(*args, &block)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.