I have this code :
我有这个代码:
def self.add_contact(name, phone, adress, note)
if name.empty? || phone.empty? || adress.empty?
puts 'some text'
else
Zoznam.new(name, phone, adress, note)
end
end
But I want another method, no the empty?
method. because the empty?
method can be usable only for strings not integers. What is the correct method?
但我想要另一种方法,不是空的吗?方法。因为空?方法只能用于非整数的字符串。什么是正确的方法?
Sorry for my bad English...
对不起,我的英语不好...
3 个解决方案
#1
2
You can use activesupport
gem which implements blank?
method. This is probably exactly what you want.
你可以使用实现空白的activesupport gem吗?方法。这可能正是你想要的。
#2
1
Using .to_s.empty?
should cover all your cases, which seem to be String, Integer and nil.
使用.to_s.empty?应该涵盖你的所有情况,似乎是String,Integer和nil。
"".to_s.empty? # => true
nil.to_s.empty? # => true
"foo".to_s.empty? # => false
1.to_s.empty? # => false
#3
0
But I want another method, not the
empty?
method, because the empty method can be used only for strings and not integers. What is the correct method?但是我想要另一种方法,而不是空的?方法,因为empty方法只能用于字符串而不能用于整数。什么是正确的方法?
Well depends really on what you consider an integer empty. There's no such thing as empty?
in the Integer
class because it doesn't make sense. If your variable is of type Integer
or Fixnum
there's no way it is empty somehow: it will always contain a value, a number.
那取决于你认为整数是空的。没有空的东西吗?在Integer类中,因为它没有意义。如果你的变量是Integer或Fixnum类型,那么它不可能以某种方式为空:它将始终包含一个值,一个数字。
What I guess you are looking for is to test the variable phone
for emptiness in the sense that nil
might be assigned to it. In that case you can simply use:
我猜你正在寻找的是测试可变电话的空虚,因为nil可能被分配给它。在这种情况下,您可以简单地使用:
if name.empty? || phone || adress.empty?
or
要么
if name.empty? || phone.is_a? Fixnum || adress.empty?
if you want to be absolutely sure that phone
is Fixnum
(or any other class for that matter).
如果你想绝对确定手机是Fixnum(或任何其他课程)。
#1
2
You can use activesupport
gem which implements blank?
method. This is probably exactly what you want.
你可以使用实现空白的activesupport gem吗?方法。这可能正是你想要的。
#2
1
Using .to_s.empty?
should cover all your cases, which seem to be String, Integer and nil.
使用.to_s.empty?应该涵盖你的所有情况,似乎是String,Integer和nil。
"".to_s.empty? # => true
nil.to_s.empty? # => true
"foo".to_s.empty? # => false
1.to_s.empty? # => false
#3
0
But I want another method, not the
empty?
method, because the empty method can be used only for strings and not integers. What is the correct method?但是我想要另一种方法,而不是空的?方法,因为empty方法只能用于字符串而不能用于整数。什么是正确的方法?
Well depends really on what you consider an integer empty. There's no such thing as empty?
in the Integer
class because it doesn't make sense. If your variable is of type Integer
or Fixnum
there's no way it is empty somehow: it will always contain a value, a number.
那取决于你认为整数是空的。没有空的东西吗?在Integer类中,因为它没有意义。如果你的变量是Integer或Fixnum类型,那么它不可能以某种方式为空:它将始终包含一个值,一个数字。
What I guess you are looking for is to test the variable phone
for emptiness in the sense that nil
might be assigned to it. In that case you can simply use:
我猜你正在寻找的是测试可变电话的空虚,因为nil可能被分配给它。在这种情况下,您可以简单地使用:
if name.empty? || phone || adress.empty?
or
要么
if name.empty? || phone.is_a? Fixnum || adress.empty?
if you want to be absolutely sure that phone
is Fixnum
(or any other class for that matter).
如果你想绝对确定手机是Fixnum(或任何其他课程)。