我刚刚完成了Ruby Koans,也没有一个单位负责使用目标的方法。 发送和Ruby关于该方法的文件都提供了关于使用发送方法的区块的任何信息。 寄送方法所附的一块块是否会被移至它所要求的方法之中,或者将失去封块?
例:
foo.send(:a_method) { bar.another_method }
我刚刚完成了Ruby Koans,也没有一个单位负责使用目标的方法。 发送和Ruby关于该方法的文件都提供了关于使用发送方法的区块的任何信息。 寄送方法所附的一块块是否会被移至它所要求的方法之中,或者将失去封块?
例:
foo.send(:a_method) { bar.another_method }
send (symbol [, args...] ——
引用
symbol确定的方法,通过任何具体论点。
但注意到任何具体论点<>部分。 你给一种方法的 block脚石实际上是一种暗含的争辩,以便你能够做以下事情:
def m(&b)
@a.each(&b)
end
m { |e| puts e }
作为证据通过环绕。 然而,你也可以这样做:
def m
yield
end
m { puts pancakes }
因此,就论点清单而言,这一障碍是特别的,但即使有时是暗含的,这些障碍仍然是争辩。
鉴于以上“24是某种论点”的斜体,以及鲁比拉的区块的重要性,通过区块的<代码>send是合理的。 你们也可以尝试,但你必须谨慎处理意外和无证行为,采取“行业”做法:
class C
def m
yield
end
end
o = C.new
o.send(:m) { puts pancakes }
# "pancakes" comes out
是的。 考虑如下:
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。
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"}
before yield
after yield
before yield
yield
after yield
TL;DR: Yes, the send
method will pass the closure to the method it calls.
在主角下,一种叫作的封闭方法被暗中用作另一种理由(尽管是一种特殊理由)。 您可使用<条码>查询<>。
> def do_it(&block)
> block.call
> end
=> :do_it
> do_it { puts apples }
apples
=> nil
......或隐含在<代码>yield上。
> def do_it_with_yield
> yield if block_given?
> end
=> :do_it_with_yield
> do_it_with_yield { puts bananas }
bananas
=> nil
您可对任何方法,包括<代码>:send,:public_send
, 等,但以要求采用的方法为限。
就<代码>send等人而言,他们按照我们希望和期望谨慎地通过封锁。
> self.public_send(:do_it) { puts cherries }
cherries
=> nil
> self.send(:do_it_with_yield) { puts dates }
dates
=> nil
This is important when implementing the method_missing
method, for example.
def method_missing(method_name, *args, &block)
puts "Delegating #{method_name} to api handler"
my_api_handler.send(method_name, *args, &block)
end
参考资料:
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 ...
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 ...
All of the following API do the same thing: open a file and call a block for each line. Is there any preference we should use one than another? File.open("file").each_line {|line| puts line} open("...
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?
Here s the string: 04046955104021109 I need it to be formatted like so: 040469551-0402-1109 What s the shortest/most efficient way to do that with ruby?
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
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 ...
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 ...