English 中文(简体)
添加了反对的栏目。 将其送往所谓的方法?
原标题:Does adding a block to Object.send pass it to the called method?
  • 时间:2012-01-12 06:29:09
  •  标签:
  • ruby

我刚刚完成了Ruby Koans,也没有一个单位负责使用目标的方法。 发送和Ruby关于该方法的文件都提供了关于使用发送方法的区块的任何信息。 寄送方法所附的一块块是否会被移至它所要求的方法之中,或者将失去封块?

例:

foo.send(:a_method) { bar.another_method }
最佳回答
问题回答

是的。 考虑如下:

class A
  def explicit(&b); b; end
  def implicit; yield "hello"; end
end

>> A.new.send(:explicit) { }
=> #<Proc:0x0000000000000000@(irb):19>
>> A.new.send(:implicit) { |greeting| puts greeting }
hello
=> nil

Hope this helps!

是的,它将。 在这种方法中,你可以用<代码>block_given?将其带上<代码>yield。

Code

class Foo
  def bar
    puts "before yield"
    yield if block_given?
    puts "after yield"
  end
end

f = Foo.new
f.send(:bar)
puts ""
f.send(:bar) { puts "yield"}

Output

before yield
after yield

before yield
yield
after yield




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...