I've seen formal arguments mentioned in ruby, is there anywhere explaining what they are exactly
Im looking for 开发者_如何学运维a good explanation of what this term means
In programming, formal arguments (also commonly known as 'formal parameters') are the arguments a function expects to receive and assigns names to. They are the arguments that generally act similar to local variables, and are explicitly listed in the function definition. For example, in this method:
def say(words, options)
options[:repetitions].times { puts words }
puts "that's all folks!" unless options[:no_footer]
end
The formal arguments are words
and options
. There are also some informal arguments, namely repetitions
and no_footer
. Semantically, we understand these to be arguments to the function, but they are not formal arguments.
There is nothing specific to ruby about formal arguments, but there is some specific significance. In ruby, and especially Rails, many many methods only have a few formal arguments (and a lot of informal arguments). For example, in this call:
redirect_to :action => 'show', :id => @entry.id
The receiving method only really has one formal argument, an options hash.
It's also worth noting here that 'formal arguments' are frequently contrasted with 'actual arguments'. Actual arguments are simply the values really being passed. So, for example, in this call,
say "I love ruby", :repetitions => 10, :no_footer => true
The actual arguments are "I love ruby"
and {:repetitions => 10, :no_footer => true}
and these map onto the formal arguments above, words
and options
.
var1, var2 = 123, 456
# arg1 and arg2 are formal arguments of some_method
# They are defined in the method signature and can be used to
# refer to the actual arguments used to call this method
def some_method(arg1, arg2)
puts arg1 + arg2
end
# var1 and var2 are the actual arguments used to call some_method
some_method var1, var2
You may see that this is not some speciality of Ruby, but is a common pattern in higher level programming languages.
精彩评论