简体   繁体   English

方法参数在Ruby中如何工作?

[英]How do method arguments work in Ruby?

I can't seem to find an answer to this question, and the two books I've checked seem to be written for experienced programmers who are new to Ruby, not beginners to programming as well as Ruby. 我似乎无法找到一个回答这个问题,我已经检查了两本书似乎有经验的程序员谁是新的红宝石,而不是初学者编程以及 Ruby来写。 I'm struggling to understand how method arguments work and what their purpose is. 我正在努力了解方法参数如何工作以及它们的目的是什么。 For example, this I can understand: 例如,我可以理解:

def method_name
  # method behaviour
end

Then, whenever I want to re-use that particular batch of code, I simply re-enter method_name and Ruby will repeat the actions as specified in that method. 然后,每当我想重新使用该特定批代码时,我只需重新输入method_name,Ruby就会重复该方法中指定的操作。 However, most methods I've seen include arguments in parenthesis immediately following the name, ie 但是,我见过的大多数方法都在名称后面紧跟括号中的参数,即

def method_name (argument1, argument2)
  # method behaviour
end

I'm struggling to grasp what the arguments mean and how they factor into the functionality of the code. 我正在努力理解参数的含义以及它们如何影响代码的功能。 Also, why are arguments required for some methods but not others? 另外,为什么某些方法需要参数,而其他方法却不需要? Do arguments represent input from the user? 参数代表用户的输入吗? Are they similar to params? 它们类似于params吗?

Like you said, think of the arguments like parameters (the two words are used for slightly different meanings but it's not important for your understanding at this point.) 就像您说的那样,将参数当作参数来考虑(这两个词的含义略有不同,但这对您的理解而言并不重要。)

The arguments are input to be used in the method behavior. 输入参数以在方法行为中使用。

def add(arg1, arg2)
    sum = arg1 + arg2
    return sum
end

Then you can call that method with some arguments: 然后,您可以使用一些参数来调用该方法:

result = add(1, 2)
# result equals 3

Function arguments don't just apply to Ruby. 函数参数不仅适用于Ruby。

Think for example a mathematical function f(x) = x^2 , where x is the input and the function simply uses that input to return an output, in this case x squared. 例如,考虑一个数学函数f(x) = x^2 ,其中x是输入,并且该函数仅使用该输入返回输出,在这种情况下为x平方。

This concept applies to programming across many languages where arguments can be used to specify a contract between the function and the caller of the function to modify the behavior of the function. 此概念适用于多种语言的编程,在这些语言中,参数可用于指定函数和函数调用者之间的约定,以修改函数的行为。

In ruby the above mathematical function could be defined as: 在红宝石中,上述数学函数可以定义为:

def square(x)
  x ** 2
end 

square(3) #=> 9
square(4) #=> 16

Methods have the option of taking arguments and it's up to you the designer whether or not to include this option. 方法可以选择接受参数,设计者可以选择是否包含此选项。 Sometimes it just makes sense to include this option, sometimes not. 有时包括此选项很有意义,有时则没有。 You will see this as you code more. 您将在编写更多代码时看到它。

Example without argument 没有参数的例子

def say_hello
  puts "hello"
end

#calling the method
say_hello #=> hello

This method is simply designed to say hello when called, which works fine. 此方法只是设计为在调用时打个招呼,效果很好。

Example with argument 带参数的例子

def say_personal_hello(name)
  puts "hello #{name}"
end

#calling the method with an argument
say_personal_hello("mszub") #=> hello mszub

But this method takes an argument because I want it to be personalised by putting a particular name in it. 但是此方法带有一个参数,因为我希望通过在其中添加特定名称来对其进行个性化设置。 The name can be a user input if you like for example: 如果您愿意,例如,该名称可以是用户输入:

puts "What's your name?"
input_name = gets.chomp

def say_personal_hello(input_name)
  puts "hello #{input_name}"
end

say_personal_hello(input_name)

Hope this helps. 希望这可以帮助。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM