Ruby 可以什么都不返回吗? [英] Can Ruby return nothing?

查看:35
本文介绍了Ruby 可以什么都不返回吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 ruby​​ 中什么都不返回吗?

Can I return nothing in ruby?

仅用于教育目的

例如:

myarray = [1,2,3]
myarray << some_method

def some_method
  if Date.today.day > 15
    return "Trololo"
  else
    return __NOTHING__
  end
end

因此,如果今天是 3 月 11 日,myarray 将不会添加新项目.我不想要 nil - 因为 nil 不是什么 :)

So if today is 11'th March myarray won't add new item. I don't want nil - because nil is not nothing :)

我明白,我可以使用 if |除非 语句像 myarray <<some_method if some_method 等等.我想了解我可以返回nothing 还是每次在ruby中返回something(至少我能得到的是Nil Object)

And I understand, that I can use if | unless statement like myarray << some_method if some_method etc. I want to understand can I return nothing or every time in ruby I am returning something (least I can get is Nil Object)

推荐答案

基本上,您要寻找的是声明.但是Ruby 没有语句,只有表达式.Everything 是一个表达式,即一切都返回一个值.

Basically, what you are looking for is a statement. But Ruby doesn't have statements, only expressions. Everything is an expression, i.e. everything returns a value.

这篇关于Ruby 可以什么都不返回吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆