红宝石 |块
块与方法相同,但不属于对象。块在其他编程语言中称为闭包。关于 Ruby 中的 Blocks 有一些要点:
- Block 可以接受参数并返回一个值。
- 块没有自己的名字。
- 块由代码块组成。
- 块总是用函数调用,或者可以说是传递给方法调用。
- 要在具有值的方法中调用块,请使用yield语句。
- 块可以像从它传递到的方法内部的方法一样被调用。
可以通过以下两种方式使用块代码:
- 在 do..end 语句中
句法:
block_name do #statement-1 #statement-2 . . end
例子:
# Ruby program to demonstrate the block # defined inside do..end statements # here 'each' is the method name # or block name # n is the variable ["Geeks", "GFG", 55].each do |n| puts n end
输出: 。
Geeks GFG 55
- 花括号 {} 之间的内联
句法:
block_name { #statements_to_be_executed }
例子:
# Ruby program to demonstrate the block # Inline between the curly braces {} # here 'each' is the method name # n is the variable ["Geeks", "GFG", 55].each {|i| puts i}
输出: 。
Geeks GFG 55
块参数:参数可以通过在管道或竖线( | | )之间封闭来传递给块。
例子:
# Ruby program to demonstrate the
# arguments passing to block
# here india_states is an array and
# it is the argument which is to
# be passed to block
india_states = ["Andra Pardesh", "Assam", "Bihar", "Chattisgarh",
"Goa", "Gujrat", "Haryana", "Arunachal Pardesh",
"Karnataka", "Manipur", "Punjab", "Uttar Pardesh",
"Uttarakhand"]
# passing argument to block
india_states.each do |india_states|
puts india_states
end
输出:
Andra Pardesh
Assam
Bihar
Chattisgarh
Goa
Gujrat
Haryana
Arunachal Pardesh
Karnataka
Manipur
Punjab
Uttar Pardesh
Uttarakhand
说明:在上面的示例中,india_states 是传递给块的参数。这里它类似于def method_name (india_states) 。唯一的区别是方法有名称但没有块,参数在方括号()之间传递给方法,但在块中,参数在管道之间传递|| .
块如何返回值:实际上块返回由调用它的方法返回的值。
例子:
# Ruby program to demonstrate how block returns the values
# here two methods called i.e 'select' and 'even?'
# even? method is called inside the block
puts [1, 2, 3, 4, 5].select { |num| num.even? }
输出:
12
14
说明:在上面的例子中,有两种方法,即select , even?和一个街区。首先, select方法将为数组中的每个数字调用块。首先,它会将11传递给 block,现在在 block 内部,还有一个名为even 的方法?它将把它作为 num 变量并为 11 返回false 。这个 false 值将被传递给 select 方法,该方法将丢弃它,然后它将 12 传递给块,同样在块内部,奇怪吗?方法被调用,它为 12 返回 true,并且这个 true 值将被传递给 select 方法。现在 select 方法将存储这个值。同样,对于数组中的剩余值, select方法会将值存储在数组中,最后最终数组将返回到puts方法,该方法在屏幕上打印返回的数组元素。
收益声明
yield 语句用于使用带有值的yield 关键字调用方法内部的块。
例子:
# Ruby program to demonstrate the yield statement
# method
def shivi
# statement of the method to be executed
puts "Inside Method!"
# using yield statement
yield
# statement of the method to be executed
puts "Again Inside Method!"
# using yield statement
yield
end
# block
shivi{puts "Inside Block!"}
输出:
Inside Method!
Inside Block!
Again Inside Method!
Inside Block!
说明:在上面的程序中,方法名是shivi 。首先调用显示Inside Method的方法语句。但是一旦 yield 语句执行,控制就进入块,块将执行它的语句。一旦块执行,它就会将控制权交还给方法,并且方法将从调用 yield 语句的位置继续执行。
注意:参数可以传递给 yield 语句。
例子:
# Ruby program to demonstrate the yield statement
# method
def shivi
# statement of the method to be executed
puts "Inside Method!"
# using yield statement
# p1 is the parameter
yield "p1"
# statement of the method to be executed
puts "Again Inside Method!"
# using yield statement
# p2 is the parameter
yield "p2"
end
# block
shivi{ |para| puts "Inside Block #{para}"}
输出:
Inside Method!
Inside Block p1
Again Inside Method!
Inside Block p2
BEGIN 和 END 块: Ruby 源文件具有声明可以在加载文件时运行的代码块的功能,即BEGIN块。程序执行完毕后将执行END块。一个程序可以包含多个 BEGIN 和 END 块。 BEGIN 块将始终按顺序执行,而END块将按相反顺序执行。
例子:
# Ruby program to demonstrate the BEGIN and END block
#!/usr/bin/ruby
# BEGIN block
BEGIN {
# BEGIN block code
puts "This is BEGIN block Code"
}
# END block
END {
# END block code
puts "This is END block code"
}
# Code will execute before END block
puts "Before END block"
输出:
This is BEGIN block Code
Before END block
This is END block code
注意:我们可以在块的外部和内部使用相同的变量。
例子:
# Ruby program to demonstrate the use of
# same variable outside and inside a block
#!/usr/bin/ruby
# variable 'x' outside the block
x = "Outside the block"
# here x is inside the block
4.times do |x|
puts "Value Inside the block: #{x}"
end
puts "Value Outside the block: #{x}"
输出:
Value Inside the block: 0
Value Inside the block: 1
Value Inside the block: 2
Value Inside the block: 3
Value Outside the block: Outside the block