标签: proc-object

Ruby块,procs和instance_eval

我最近尝试做类似于此的事情:

a = "some string"
b = Proc.new{ upcase }
a.instance_eval b
Run Code Online (Sandbox Code Playgroud)

这给出了错误:

TypeError:无法将Proc转换为String

但这有效:

def b(&block)
  "some string".instance_eval &block
end

b{ upcase }
Run Code Online (Sandbox Code Playgroud)

进一步了解这种方法:

def b(&block)
  "some string".instance_eval block
end
Run Code Online (Sandbox Code Playgroud)

产生相同的Proc to String错误.

所以...我对块的理解是它们只是触发器.但是这个&&符号显然有一些特别之处......

谁可以给我解释一下这个?是否有可能将正常的proc转换成&block对象的特殊之处?

编辑

刚刚想出了我的第二个问题,在前面加了一个&...这很容易,但这是真的在做什么?

ruby lambda block proc-object

13
推荐指数
2
解决办法
9169
查看次数

如何将变量用作TCL proc参数的默认值

我有一个变量,我想用作参数的默认值:

proc log {message {output $::output}} {
   ....
}
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点或需要我评估我的proc中的变量?

arguments default tcl proc-object

9
推荐指数
2
解决办法
3496
查看次数

如何通过方法调用参数传递给proc?(红宝石)

proc = Proc.new do |name|
  puts "Thank you #{name}!"
end
def thank
  yield
end
Run Code Online (Sandbox Code Playgroud)
proc.call # output nothing, just fine
proc.call('God') # => Thank you God!

thank &proc # output nothing, too. Fine;
thank &proc('God') # Error!
thank &proc.call('God') # Error!
thank proc.call('God') # Error!
# So, what should I do if I have to pass the 'God' to the proc and use the 'thank' method at the same time ?
Run Code Online (Sandbox Code Playgroud)

谢谢 :)

ruby parameters block call proc-object

8
推荐指数
2
解决办法
1万
查看次数

当使用Proc.new v.&符号时,为什么ruby中的break语句表现不同?

块的break语句(根据Ruby Programming Language)定义如下:

它导致块返回到它的迭代器,迭代器返回到调用它的方法.

因此,当运行以下代码时,它会导致LocalJumpError.

def test
    puts "entering test method"
    proc = Proc.new { puts "entering proc"; break }
    proc.call # LocalJumpError: iterator has already returned
    puts "exiting test method"
end
test
Run Code Online (Sandbox Code Playgroud)

虽然以下代码不会抛出LocalJumpError.&符号有什么特别之处?&符号是否隐含使用Proc.new?

def iterator(&proc)
    puts "entering iterator"
    proc.call # invoke the proc
    puts "exiting iterator" # Never executed if the proc breaks
end

def test
    iterator { puts "entering proc"; break }
end
test
Run Code Online (Sandbox Code Playgroud)

换句话说,我读了&符号作为进入Proc.new呼叫的手段.此时行为应该与第一个代码段相同.

def iterator (p = Proc.new { puts "entering proc"; …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails proc-object

8
推荐指数
1
解决办法
2603
查看次数

TCL - 如何知道函数运行了多长时间?

假设我有一个proc,proc包含几个语句和函数调用.我怎么知道这个函数到目前为止花了多少时间?

time timestamp tcl proc-object

7
推荐指数
2
解决办法
2万
查看次数

我们如何在不同的Ruby类之间复制单例方法?

我试图用一个方法定义一个类,一个缺少这些方法的类,然后允许后一个类的对象从前一个类的实例中"学习"这些方法.

这是我的尝试(Ruby 1.9.2) - 当我尝试在lambda绑定中更改'self'的值时,它会中断(在行评论"BREAKS!").

如果你能弄清楚如何解决这个问题 - 我会很高兴找到答案.

class Skill

  attr_accessor :name
  attr_accessor :technique

  def initialize(name, &technique_proc)
    @name = name
    @technique = lambda(&proc)
  end

end

class Person

  attr_accessor :name

  def initialize(name)
    @name = name
  end

  def method_missing(m, *args)
    "#{@name} the #{self.class}: I don't know how to #{m}"
  end

  def learn_skill(skill)
    puts "#{@name} the #{self.class} is learning skill: #{skill.name}"
    actual_self = self
    eval "self = #{actual_self}", skill.technique.binding ####### BREAKS!
    define_singleton_method skill.name.to_sym, skill.technique
  end

  def teach_skill(skill_name)
    skill = nil
    if self.respond_to?(skill_name) 
      puts …
Run Code Online (Sandbox Code Playgroud)

ruby lambda eval metaprogramming proc-object

6
推荐指数
1
解决办法
664
查看次数

为什么在ruby 1.9中可以使用===调用proc?

本文提到了4种在ruby 1.9中调用proc的方法,而===就是其中之一.我不明白为什么会这样做.它是否与===的正常含义有任何关系(询问这两个对象是否是同一个对象)?

irb(main):010:0> f =-> n {[:hello, n]}
=> #
irb(main):011:0> f.call(:hello)
=> [:hello, :hello]
irb(main):012:0> f === :hello
=> [:hello, :hello]
irb(main):013:0> Object.new === Object.new
=> false
irb(main):014:0> f === f
=> [:hello, #]

ruby ruby-1.9 proc-object

6
推荐指数
2
解决办法
136
查看次数

5
推荐指数
1
解决办法
180
查看次数

如何打印出tcl proc?

给出一个简单的tcl proc

proc foo {a b} {puts "$a $b"}
Run Code Online (Sandbox Code Playgroud)

我可以使用什么tcl命令打印出程序foo...这就是我想要proc 的文本回来...

例如:

% proc foo {a b} {puts "$a $b"}
% foo a b
  a b

% puts $foo
  can't read "foo": no such variable
Run Code Online (Sandbox Code Playgroud)

我怎么foo {a b} {puts "$a $b"}回来?

tcl redefinition proc-object

4
推荐指数
2
解决办法
5401
查看次数

tcl:包装一个同名的proc

我想用一个具有相同名称和调用约定的proc替换"proc N"的定义,但是需要一些额外的错误检测代码.

在python中,我可以按照下面的方式执行我想要的操作,但是我没有掌握命名空间和函数句柄如何在tcl中工作.

__orig_N = N
def N(arg1, arg2):
    if arg1 != 'GOOD VALUE':
        exit('arg1 is bad')
    return __orig_N(arg1, arg2)
Run Code Online (Sandbox Code Playgroud)

tcl decorator wrapper proc-object

4
推荐指数
2
解决办法
952
查看次数