连接之前检查 nil 字符串

发布于 2024-08-20 07:53:46 字数 213 浏览 2 评论 0原文

这个问题与很多问题类似,但绝不是重复的。这个问题是关于字符串连接和编写比检查 nil/zero 更好的代码。

目前我有:

file.puts "cn: " + (var1.nil? ? "UNKNOWN" : var1)

效果很好,但看起来不太好。在 ruby​​ 中编写此代码的更好方法是什么,以便我检查 nil 而不是连接它

This question is similar to a LOT of questions, but in no such way is it anything of a duplicate. This question is about string concatenation and writing better code less than it is for checking nil/zero.

Currently I have:

file.puts "cn: " + (var1.nil? ? "UNKNOWN" : var1)

Which works fine, but doesn't look good. What is a better way to write this in ruby so that I am checking for nil and not concatenating it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

太傻旳人生 2024-08-27 07:53:46

您可以这样做:

file.puts "cn: " + (var1 || "UNKNOWN")

或者,如果您愿意,也可以这样做:

file.puts "cn: " + (var1 or "UNKNOWN")

或我最喜欢的,我认为这是最惯用的红宝石:

file.puts "cn: #{var1 or 'unknown'}"

You can do this:

file.puts "cn: " + (var1 || "UNKNOWN")

or, identically if you prefer:

file.puts "cn: " + (var1 or "UNKNOWN")

or my favourite, which I think is the most idiomatic ruby:

file.puts "cn: #{var1 or 'unknown'}"
以为你会在 2024-08-27 07:53:46

使用join添加可能为nil的字符串。

如果存在 niljoin 不会抱怨

例如:

["a","b",nil,"c"].join("") 
#=> abc

但是,如果您使用除空白字符串(例如下划线)之外的任何内容进行连接,您将得到一个 join String对于 nil 值:

["a","b",nil,"c"].join("_")
#=> a_b__c

要解决此问题,请在加入之前使用 .compactArray 中删除 nil 值:

["a","b",nil,"c"].compact.join("_")
#=> a_b_c

Use join to add the strings which may be nil.

The join will not complain if there is a nil

For example:

["a","b",nil,"c"].join("") 
#=> abc

However, if you are joining with anything but a blank string, like an underscore, you will get a join String for the nil value:

["a","b",nil,"c"].join("_")
#=> a_b__c

To fix this, use .compact to remove the nil values from the Array before joining:

["a","b",nil,"c"].compact.join("_")
#=> a_b_c
不…忘初心 2024-08-27 07:53:46

使用 ruby​​ 2.4.1,to_s 解析为 nil"Hello"。所以 var1.to_s 应该足够了。

2.4.1 :058 > nil.to_s
 => "" 
2.4.1 :059 > "hello".to_s
 => "hello" 

Using ruby 2.4.1, to_s resolves for both nil and "Hello". So var1.to_s should suffice.

2.4.1 :058 > nil.to_s
 => "" 
2.4.1 :059 > "hello".to_s
 => "hello" 
诺曦 2024-08-27 07:53:46

我会按照彼得的建议去做,假设 false 不是 var1 的有效值,并且 var1 保证为 nil 或字符串。您还可以将该逻辑提取到函数中:

def display_value(var)
  (var || "UNKNOWN").to_s # or (var.nil? ? "UNKNOWN" : var.to_s) if 'false' is a valid value
end

file.puts "cn: " + display_value(var1)

仅当 var1 不能保证为 nil 或字符串时才需要 to_s。或者,如果您这样做:

file.puts "cn: #{display_value(var1)}"

它将对 display_value 的结果执行隐式 to_s

I would do what Peter suggested, assuming that false wasn't a valid value for var1, and var1 was guaranteed to be nil or a string. You could also extract that logic into a function:

def display_value(var)
  (var || "UNKNOWN").to_s # or (var.nil? ? "UNKNOWN" : var.to_s) if 'false' is a valid value
end

file.puts "cn: " + display_value(var1)

to_s is only necessary if var1 isn't guaranteed to be nil or a string. Alternatively, if you do:

file.puts "cn: #{display_value(var1)}"

it will do an implicit to_s on the result of display_value

泪是无色的血 2024-08-27 07:53:46
file.puts( "cn:" + (var1 || "UNKNOWN" ))
file.puts( "cn:" + (var1 || "UNKNOWN" ))
阳光①夏 2024-08-27 07:53:46

由于 "cn: " 部分纯粹是为了美观,因此(更多?)可能会发生变化以满足未来的演示指南,我建议使用 join;

file.puts(["cn", (var1 || "UNKNOWN")].join(": ")

也许作为一个函数,如前所述 - 语义是相同的,只是方法名称/关键字发生了变化;

def value_or_unknown(value, attribute = nil)
  [attribute, (value or "UNKNOWN")] * ": "
end

Since the "cn: " part is purely aesthetical and therefore (more?) subject to change to meet future presentation guidelines, I would recommend using join;

file.puts(["cn", (var1 || "UNKNOWN")].join(": ")

Perhaps as a function, as mentioned earlier - semantics are the same, only method names/keywords have changed;

def value_or_unknown(value, attribute = nil)
  [attribute, (value or "UNKNOWN")] * ": "
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文