Comprobar si una variable no es nil y no es cero en ruby


Estoy usando el siguiente código para comprobar si una variable no es nil y no cero

if(discount != nil && discount != 0) 
  ...
end

¿Hay una mejor manera de hacer esto?

 215
Author: Salvador Dali, 2008-10-31

16 answers

unless discount.nil? || discount == 0
  # ...
end
 357
Author: Dejan Simic,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-18 09:45:02
class Object
  def nil_zero?
    self.nil? || self == 0
  end
end

# which lets you do
nil.nil_zero? # returns true
0.nil_zero?   # returns true
1.nil_zero?   # returns false
"a".nil_zero? # returns false

unless discount.nil_zero?
  # do stuff...
end

Tenga cuidado con los descargos de responsabilidad habituales... gran poder / responsabilidad, parches de monos que conducen al lado oscuro, etc.

 39
Author: madlep,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-31 12:32:01

Ok, después de 5 años han pasado....

if discount.try :nonzero?
  ...
end
 26
Author: rewritten,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-15 23:09:37
unless [nil, 0].include?(discount) 
  # ...
end
 24
Author: Dejan Simic,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-31 00:47:13

Desde Ruby 2.3.0 en adelante, puede combinar el operador de navegación segura (&.) con Numeric#nonzero?. &. devuelve nil si la instancia fue nil y nonzero? - si el número era 0:

if discount&.nonzero?
  # ...
end

O postfijo:

do_something if discount&.nonzero?
 17
Author: ndn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-15 21:04:54
if (discount||0) != 0
  #...
end
 15
Author: Raimonds Simanovskis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-31 12:46:35

Usted podría hacer esto:

if (!discount.nil? && !discount.zero?)

El orden es importante aquí, porque si discount es nil, entonces no tendrá un método zero?. La evaluación de cortocircuito de Ruby debería evitar que intente evaluar discount.zero?, sin embargo, si discount es nil.

 13
Author: Jeremy Ruten,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-31 00:14:09

¿Puede convertir su fila vacía a un valor entero y marcar cero?.

"".to_i.zero? => true
nil.to_i.zero? => true
 9
Author: oivoodoo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-26 09:53:27
if discount and discount != 0
  ..
end

Actualización, será false para discount = false

 3
Author: rubyprince,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-16 05:16:46
def is_nil_and_zero(data)
     data.blank? || data == 0 
end  

Si pasamos "" devolverá false, mientras que en blanco? devuelve true. Lo mismo ocurre cuando data = false en blanco? devuelve true para nil, false, empty o una cadena de espacios en blanco. Así que es mejor usar en blanco? método para evitar la cadena vacía también.

 3
Author: Saroj,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-06 10:05:43

Puede aprovechar el método NilClass proporcionado #to_i, que devolverá cero para nil valores:

unless discount.to_i.zero?
  # Code here
end

Si discount puede ser números fraccionarios, puede usar #to_f en su lugar, para evitar que el número se redondee a cero.

 2
Author: Dave G-W,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-29 06:40:24

Cuando se trata de un registro de base de datos , me gusta inicializar todos los valores vacíos con 0, utilizando el ayudante de migración:

add_column :products, :price, :integer, default: 0
 1
Author: pastullo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-14 14:04:23

Puede inicializar el descuento a 0 siempre y cuando se garantice que su código no intentará usarlo antes de inicializarlo. Eso eliminaría un cheque supongo, no puedo pensar en nada más.

 0
Author: Ed S.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-31 00:10:38
if discount.nil? || discount == 0
  [do something]
end
 0
Author: Abhinay Reddy Keesara,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-20 19:17:22

La solución alternativa es usar Refinamientos, como así:

module Nothingness
  refine Numeric do
    alias_method :nothing?, :zero?
  end

  refine NilClass do
    alias_method :nothing?, :nil?
  end
end

using Nothingness

if discount.nothing?
  # do something
end
 0
Author: RichOrElse,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-19 10:18:48

Creo que lo siguiente es lo suficientemente bueno para el código ruby. No creo que pueda escribir una prueba unitaria que muestre alguna diferencia entre esto y el original.

if discount != 0
end
 -6
Author: Jeff Waltzer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-16 03:35:35