【文件属性】:
文件名称:nil_or:执行除非 Nil
文件大小:5KB
文件格式:ZIP
更新时间:2021-06-23 10:25:24
Ruby
零或
将方法委托给目标,除非为零。
安装
将此行添加到应用程序的 Gemfile 中:
gem 'nil_or'
然后执行:
$ bundle
或者自己安装:
$ gem install nil_or
用法
我什么时候需要它? 而不是写:
date_of_birth = params [ :date_of_birth ] ? params [ :date_of_birth ] . to_date : nil
写:
date_of_birth = params [ :date_of_birth ] . nil_or . to_date
当 x 不为零时:
x = 5
x . nil_or . to_s
=> "5"
x . nil_or + 2
=> 7
x . nil_or . times { | i | puts i }
...
但是当它为零时:
x
【文件预览】:
nil_or-master
----.gitignore(185B)
----nil_or.gemspec(693B)
----UNLICENSE(1KB)
----spec()
--------spec_helper.rb(94B)
--------lib()
----README.md(1KB)
----lib()
--------nil_or.rb(236B)
----Gemfile(102B)
----Rakefile(28B)
----Gemfile.lock(502B)