在一个街区内收益是什么意思?

时间:2009-09-14 05:14:21

标签: ruby

  def any?
    if block_given?
      method_missing(:any?) { |*block_args| yield(*block_args) }
    else
      !empty?
    end
  end

在ActiveRecord的代码中,块中存在的yield语句的目的是什么?

3 个答案:

答案 0 :(得分:10)

基本上,如果给当前方法一个代码块(由调用者调用它),yield执行传入指定参数的代码块。

[1,2,3,4,5].each { |x| puts x }

现在{ |x| puts x}是传递给x的每个方法的代码块(Array是一个参数)。 Array#each实现会迭代自己并使用x = each_element

多次调用您的块
pseudocode
def each
  #iterate over yourself
    yield( current_element )
end

因此结果

1
2
3
4
5

*block_args是一种Ruby方式,可以接受未知数量的参数作为数组。调用者可以传入具有不同数量参数的块。

最后让我们看看一个区块内的收益率。

class MyClass
  def print_table(array, &block)
    array.each{|x| yield x}
  end
end

MyClass.new.print_table( [1,2,3,4,5] ) { |array_element| 
    10.times{|i| puts "#{i} x #{array_element} = #{i*array_element}" }
    puts "-----END OF TABLE----"
  }

此处Array#each会将每个元素生成给MyClass#print_table ...

的块

答案 1 :(得分:6)

这并不意味着什么特别的。它只是一个收益率,就像任何其他收益率一样。

def test_method
  ["a", "b", "c"].map {|i| yield(i) }
end

p test_method {|i| i.upcase }
# => ["A", "B", "C"]

在活动记录的代码段中,目的是在每次调用any?块时生成。

答案 2 :(得分:0)

这有助于我理解:yield是一种将块插入到已编写的方法中的方法,这意味着“在此处执行”。例如,

没有参数

def original_method
   puts "a"
   puts "b"
end

如果您想在这两个p之间执行一些未知块怎么办?

def updated_method
   puts "a"
   yield
   puts "b"
end

# these two are the same:
updated_method { puts "execute something here" }
updated_method do puts "execute something here" end

结果将是

a
execute something here
b

您可以在方法中拥有尽可能多的yield个。

使用参数

def updated_method
   puts "a"
   yield
   puts "b"
end

如果要在字符串"Execute something on me!"上执行某个未知块怎么办?

def updated_method_with_argument
   puts "a"
   yield("Execute something on me!")
   puts "b"
end

updated_method_with_argument do |argument|
  puts "This argument gets put out: " << argument
end

结果将是

a
This argument gets put out: Execute something on me!
b
相关问题