如何在Ruby中的一行中定义一个方法?

时间:2023-02-02 19:14:38

Is def greet; puts "hello"; end the only way to define a method on one line in Ruby?

def问候;把“你好”;结束在Ruby中的一行上定义方法的惟一方法?

5 个解决方案

#1


83  

You can avoid the need to use semicolons if you use parentheses:

如果使用括号,可以避免使用分号:

def hello() :hello end

#2


55  

Just give the full fresh answer:

只要给出一个全新的答案:

In general avoid single-line methods. Although they are somewhat popular in the wild, there are a few peculiarities about their definition syntax that make their use undesirable. At any rate - there should be no more than one expression in a single-line method.

一般避免使用单行方法。虽然它们在野外很受欢迎,但是它们的定义语法有一些特殊之处,使它们的使用不受欢迎。无论如何——单行方法中不应该有多个表达式。

# bad
def too_much; something; something_else; end

# okish - notice that the first ; is required
def no_braces_method; body end

# okish - notice that the second ; is optional
def no_braces_method; body; end

# okish - valid syntax, but no ; make it kind of hard to read
def some_method() body end

# good
def some_method
  body
end

One exception to the rule are empty-body methods.

该规则的一个例外是空体方法。

# good
def no_op; end

From bbatsov/ruby-style-guide.

从bbatsov / ruby-style-guide。

#3


39  

def add a,b; a+b end

The semicolon is the inline statement terminator for Ruby

分号是Ruby的内联语句结束符

Or you can use the define_method method. (Edit: This one's deprecated in ruby 1.9)

或者你可以使用define_method方法。(编辑:这个在ruby 1.9中被弃用了)

define_method(:add) {|a,b| a+b }

#4


9  

Another way:

另一种方法:

define_method(:greet) { puts 'hello' }

May be used if you don't want to enter new scope for method while defining it.

如果在定义方法时不希望为方法输入新的范围,可以使用。

#5


5  

Yet another way:

另一种方法:

def greet() return 'Hello' end

#1


83  

You can avoid the need to use semicolons if you use parentheses:

如果使用括号,可以避免使用分号:

def hello() :hello end

#2


55  

Just give the full fresh answer:

只要给出一个全新的答案:

In general avoid single-line methods. Although they are somewhat popular in the wild, there are a few peculiarities about their definition syntax that make their use undesirable. At any rate - there should be no more than one expression in a single-line method.

一般避免使用单行方法。虽然它们在野外很受欢迎,但是它们的定义语法有一些特殊之处,使它们的使用不受欢迎。无论如何——单行方法中不应该有多个表达式。

# bad
def too_much; something; something_else; end

# okish - notice that the first ; is required
def no_braces_method; body end

# okish - notice that the second ; is optional
def no_braces_method; body; end

# okish - valid syntax, but no ; make it kind of hard to read
def some_method() body end

# good
def some_method
  body
end

One exception to the rule are empty-body methods.

该规则的一个例外是空体方法。

# good
def no_op; end

From bbatsov/ruby-style-guide.

从bbatsov / ruby-style-guide。

#3


39  

def add a,b; a+b end

The semicolon is the inline statement terminator for Ruby

分号是Ruby的内联语句结束符

Or you can use the define_method method. (Edit: This one's deprecated in ruby 1.9)

或者你可以使用define_method方法。(编辑:这个在ruby 1.9中被弃用了)

define_method(:add) {|a,b| a+b }

#4


9  

Another way:

另一种方法:

define_method(:greet) { puts 'hello' }

May be used if you don't want to enter new scope for method while defining it.

如果在定义方法时不希望为方法输入新的范围,可以使用。

#5


5  

Yet another way:

另一种方法:

def greet() return 'Hello' end

相关文章