I'm starting to learn Ruby. I read that arguments where passed by reference to a method, however I don't understand the difference between these two methods.
我开始学Ruby了。我阅读了引用一个方法传递的参数,但是我不理解这两个方法之间的区别。
def print(text)
puts text
end
and
和
def print(*text)
puts text
end
Using a *
means that we are passing a pointer like in C?
使用*表示我们正在传递一个指针,就像在C中那样?
4 个解决方案
#1
5
The *text is what's called the splat operator in Ruby. It basically means if you pass multiple arguments to the second print they will get slurped into the single text variable.
文本是Ruby中的splat操作符。它的基本意思是如果你将多个参数传递给第二个打印文件,它们将被合并到单个文本变量中。
See The Splat Operator in Ruby
参见Ruby中的Splat操作符
#2
4
The *
before a parameter name in a Ruby parameter list is used for variable length arguments, so they are similar to the ...
in C/C++ for varargs.
在Ruby参数列表中的参数名之前的*用于变量长度参数,因此它们与…在C / c++可变参数。
def vlaFunc(*args)
puts args
end
vlaFunc(1,2,3)
# output is [1,2,3]
#3
2
There are no pointers in Ruby, *
in this context is generally referred to as the "splat" operator:
Ruby中没有指针,*在这个上下文中通常被称为“splat”操作符:
- http://4loc.wordpress.com/2009/01/16/the-splat-operator-in-ruby/
- http://4loc.wordpress.com/2009/01/16/the-splat-operator-in-ruby/
- http://theplana.wordpress.com/2007/03/03/ruby-idioms-the-splat-operator/
- http://theplana.wordpress.com/2007/03/03/ruby-idioms-the-splat-operator/
In this case the method can take an arbitrary number of arguments, which will be available in the array text
.
在这种情况下,该方法可以取任意数量的参数,这些参数将在数组文本中可用。
#4
0
First you have two nice methods started there. But I would say try to avoid using puts inside them. You don't need it anyway. A method will always yield the last statement evaluated. something = text would get the job done. And I don't need to answer now about the differences. Your first two replies are very good there. But you may want to try something like this j = *[] #=> nil in 1.8 but [] in 1.9 It's been the new kid on the block for a time now. Guess what it does?
首先你有两个很好的方法。但我想说的是尽量避免使用放在里面的东西。反正你也不需要它。方法总是会产生最后一个被求值的语句。有东西=文字就能完成工作。我现在不需要回答这些差异。你的前两个回答很好。但是你可能想试试这个j = *[] #=> nil值1。8,但在1。9中,它已经有一段时间了。猜出这是个什么东西?
#1
5
The *text is what's called the splat operator in Ruby. It basically means if you pass multiple arguments to the second print they will get slurped into the single text variable.
文本是Ruby中的splat操作符。它的基本意思是如果你将多个参数传递给第二个打印文件,它们将被合并到单个文本变量中。
See The Splat Operator in Ruby
参见Ruby中的Splat操作符
#2
4
The *
before a parameter name in a Ruby parameter list is used for variable length arguments, so they are similar to the ...
in C/C++ for varargs.
在Ruby参数列表中的参数名之前的*用于变量长度参数,因此它们与…在C / c++可变参数。
def vlaFunc(*args)
puts args
end
vlaFunc(1,2,3)
# output is [1,2,3]
#3
2
There are no pointers in Ruby, *
in this context is generally referred to as the "splat" operator:
Ruby中没有指针,*在这个上下文中通常被称为“splat”操作符:
- http://4loc.wordpress.com/2009/01/16/the-splat-operator-in-ruby/
- http://4loc.wordpress.com/2009/01/16/the-splat-operator-in-ruby/
- http://theplana.wordpress.com/2007/03/03/ruby-idioms-the-splat-operator/
- http://theplana.wordpress.com/2007/03/03/ruby-idioms-the-splat-operator/
In this case the method can take an arbitrary number of arguments, which will be available in the array text
.
在这种情况下,该方法可以取任意数量的参数,这些参数将在数组文本中可用。
#4
0
First you have two nice methods started there. But I would say try to avoid using puts inside them. You don't need it anyway. A method will always yield the last statement evaluated. something = text would get the job done. And I don't need to answer now about the differences. Your first two replies are very good there. But you may want to try something like this j = *[] #=> nil in 1.8 but [] in 1.9 It's been the new kid on the block for a time now. Guess what it does?
首先你有两个很好的方法。但我想说的是尽量避免使用放在里面的东西。反正你也不需要它。方法总是会产生最后一个被求值的语句。有东西=文字就能完成工作。我现在不需要回答这些差异。你的前两个回答很好。但是你可能想试试这个j = *[] #=> nil值1。8,但在1。9中,它已经有一段时间了。猜出这是个什么东西?