ruby語法_Ruby函式(方法)語法
ruby語法
The Rubylanguage makes it easy to create functions.
Ruby語言使建立函式變得容易。
Function Syntax
功能語法
def functionname(variable) return <value>end
def functionname(variable)return <值>結束
Examples
例子
Your function can compute values and store them in local variables that are specific to the function. Those values can then be returned with the return
您的函式可以計算值並將它們儲存在該函式特定的區域性變數中。 然後可以使用return語句返回這些值。
def say_hello(name) var =“Hello, ” + name return varend
def say_hello(name)var =“你好,” +名稱返回varend
The return statement also can be shortened for very simple functions into a single line
對於非常簡單的功能,還可以將return語句縮短為一行
def say_hello(name) return “Hello, ” + nameend
def say_hello(name)返回“ Hello,” + nameend
You can simplify the function further. The last expression that is evaluated is automatically returned by the method. For example:
您可以進一步簡化功能。 該方法自動返回最後一個求值表示式。 例如:
def say_hello(name) “Hello, ” + nameend
def say_hello(name)“你好,” + nameend
This would return the same value as the prior functions.
這將返回與先前功能相同的值。
To call a function
呼叫功能
function param1, param2
函式param1,param2
or
要麼
function(param1,param2)
函式(param1,param2)
Example
例
puts say_hello(“Geek”)
放置say_hello(“ Geek”)
翻譯自: https://www.howtogeek.com/howto/programming/ruby/ruby-function-method-syntax/
ruby語法