Sha256: b69093bf57b2922432a3ca1bbbe82ba8ca3f8b29a5a3f631b4a3de2faa3f3660

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

class Ruby_process
  #Calls a block by its block-ID with given arguments.
  def cmd_block_call(obj)
    raise "Invalid block-ID: '#{obj}'." if obj[:block_id].to_i <= 0
    block_ele = @objects[obj[:block_id]]
    raise "No block by that ID: '#{obj[:block_id]}'." if !block_ele
    raise "Not a block? '#{block_ele.class.name}'." if !block_ele.respond_to?(:call)
    debug "Calling block #{obj[:block_id]}: #{obj}\n" if @debug
    block_ele.call(*read_args(obj[:args]))
    return nil
  end
  
  #Spawns a block and returns its ID.
  def cmd_spawn_proxy_block(obj)
    block = proc{
      send(:cmd => :block_call, :block_id => obj[:id])
    }
    
    id = block.__id__
    raise "ID already exists: '#{id}'." if @objects.key?(id)
    @objects[id] = block
    
    return {:id => id}
  end
  
  #Dynamically creates a block with a certain arity. If sub-methods measure arity, they will get the correct one from the other process.
  def block_with_arity(args, &block)
    eval_str = "proc{"
    eval_argsarr = "\t\tblock.call("
    
    if args[:arity] > 0
      eval_str << "|"
      1.upto(args[:arity]) do |i|
        if i > 1
          eval_str << ","
          eval_argsarr << ","
        end
        
        eval_str << "arg#{i}"
        eval_argsarr << "arg#{i}"
      end
      
      eval_str << "|\n"
      eval_argsarr << ")\n"
    end
    
    eval_full = eval_str + eval_argsarr
    eval_full << "}"
    
    debug "Block eval: #{eval_full}\n" if @debug
    dynamic_proc = eval(eval_full)
    
    return dynamic_proc
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby_process-0.0.7 cmds/blocks.rb
ruby_process-0.0.5 cmds/blocks.rb
ruby_process-0.0.4 cmds/blocks.rb