• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ruby语法_Ruby函数(方法)语法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

ruby语法

The Ruby language 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 statement.

您的函数可以计算值并将它们存储在该函数特定的局部变量中。 然后可以使用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语法


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Ruby 2.4.0前瞻发布时间:2022-07-14
下一篇:
《Effective Ruby:改善Ruby程序的48条建议》一第9条:提防Ruby最棘手的解析 ...发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap