相当于 Ruby 中的“pass”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13452624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Equivalent of “pass” in Ruby
提问by user1251007
In python there is a passkeyword for defining an empty function, condition, loop, ...
Is there something similar for Ruby?
在 python 中有一个pass关键字用于定义空函数、条件、循环、...... Ruby 有类似的东西吗?
Python Example:
Python示例:
def some_function():
# do nothing
pass
采纳答案by user1251007
You always have endstatements, so passis not needed.
你总是有end陈述,所以pass不需要。
Ruby example:
红宝石示例:
def some_function()
# do nothing
end
回答by J?rg W Mittag
No, there is no such thing in Ruby. If you want an empty block, method, module, class etc., just write an empty block:
不,Ruby 中没有这样的东西。如果你想要一个空块、方法、模块、类等,只需写一个空块:
def some_method
end
That's it.
就是这样。
In Python, every block is requiredto contain at leastone statement, that's why you need a "fake" no-op statement. Ruby doesn't havestatements, it only has expressions, and it is perfectly legal for a block to contain zero expressions.
在 Python 中,每个块都需要包含至少一个语句,这就是为什么您需要“假”无操作语句的原因。Ruby没有有报表,它只有表情,这是完全合法的块包含零个表情。
回答by konsolebox
nilis probably the equivalent of it:
nil可能相当于它:
def some_function
nil
end
It's basically helpful when ignoring exceptions using a simple one-line statement:
使用简单的单行语句忽略异常时,它基本上很有帮助:
Process.kill('CONT', pid) rescue nil
Instead of using a block:
而不是使用块:
begin
Process.kill('CONT')
rescue
end
And dropping nilwould cause syntax error:
并且丢弃nil会导致语法错误:
> throw :x rescue
SyntaxError: (irb):19: syntax error, unexpected end-of-input
from /usr/bin/irb:11:in `<main>'
Notes:
笔记:
def some_function; end; some_functionreturns nil.
def some_function; end; some_function返回nil。
def a; :b; begin; throw :x; rescue; end; end; a;also returns nil.
def a; :b; begin; throw :x; rescue; end; end; a;也返回nil。
回答by Fady Faried
Single line functions and classes
单行函数和类
def name ; end
class Name ; end
works fine for pseudocode.
适用于伪代码。
As answered before everything in ruby is an expressionso it is fine to leave it blank.
正如之前回答的,ruby 中的所有内容都是一个表达式,因此可以将其留空。
def name
end
class Name
end
A ruby alternative for python programmers who love the pass keyword
喜欢 pass 关键字的 Python 程序员的 ruby 替代品
def pass
end
# OR
def pass; end
Note that it is useless to do this in Ruby since it allows empty methods but if you're that keen on pass, this is the simplest and cleanest alternative.
请注意,在 Ruby 中执行此操作是无用的,因为它允许使用空方法,但如果您如此热衷pass,这是最简单和最干净的选择。
and now you can use this function inside any blockand it will work the same.
现在你可以在任何块中使用这个函数,它的工作原理是一样的。
def name
pass
end
# OR
class Name
pass
end
Keep in mind that pass is a function that returns, so it is up to you how you can use it.
请记住, pass 是一个返回函数,因此如何使用它取决于您。
回答by gkats
As others have said, in Ruby you can just leave a method body empty. However, this could prove a bit different than what Python accomplishes with pass.
正如其他人所说,在 Ruby 中,您可以将方法体留空。但是,这可能与 Python 使用pass.
In Ruby, everything is an object. The absence of value, which some programming languages indicate with nullor nilis actually an object of NilClassin Ruby.
在 Ruby 中,一切都是对象。缺少值,某些编程语言用Ruby表示null或nil实际上是NilClassRuby 中的对象。
Consider the following example (in irb):
考虑以下示例(在 中irb):
class A
def no_op
end
end
A.new.no_op
# => nil
A.new.no_op.class
# => NilClass
A.new.no_op.nil?
# => true
Here's Ruby's NilClass documentationfor reference.
这是 Ruby 的NilClass 文档供参考。
I believe Python's passis used mainly to overcome the syntactic limitations of the language (indentation), although I'm not that experienced in Python.
我相信 Python 的pass主要用途是克服语言的语法限制(缩进),尽管我在 Python 方面没有那么丰富的经验。
回答by sawa
If you want to be able to use it freely with any number of arguments, you have to have a small trick on the arguments:
如果你想能够自由地使用任意数量的参数,你必须在参数上有一个小技巧:
def gobble *args, ≺ end

