# Check to see if a num is Negative

Hey Gents,

Is there anything that will allow me to determine if a variable is a
positive or negative number??

I have this line:
difference = list_stripes.planned.to_i - list_stripes.spent.to_i

And then I want to check difference to see if it is positive or negative
value.

On Jun 7, 11:19 am, Phillip B. [email protected]
wrote:

Hey Gents,

Is there anything that will allow me to determine if a variable is a
positive or negative number??

I have this line:
difference = list_stripes.planned.to_i - list_stripes.spent.to_i

And then I want to check difference to see if it is positive or negative
value.

if difference < 0

# …

end

You should seriously consider reading a Ruby (not Rails) tutorial.
Many things should become much more clear to you as a result.

On Jun 7, 2007, at 12:42 PM, jdl wrote:

And then I want to check difference to see if it is positive or
negative
value.

if difference < 0

# …

end

You should seriously consider reading a Ruby (not Rails) tutorial.
Many things should become much more clear to you as a result.

I’ll second jdl, but also point out the comparison method <=> which
returns -1, 0, or 1 depending on whether the left-hand-side is less-
than, equal-to, or greater-than the right-hand-side.

There’s also .nonzero? which is false (nil, actually) when the
receiver is 0, or the value of the receiver otherwise.

irb> 5 <=> 3
=> 1
irb> 5 <=> 9
=> -1
irb> 5 <=> 5
=> 0
irb> 4.nonzero?
=> 4
irb> -5.nonzero?
=> -5
irb> 0.nonzero?
=> nil

-Rob

On 6/7/07, jdl [email protected] wrote:

difference = list_stripes.planned.to_i - list_stripes.spent.to_i

Well, I think the OP needs basic programming lessons, THEN Ruby lessons,
THEN Rails.

Just use one of the following:

difference > 0
difference >= 0
difference < 0
difference <= 0

Chris

On Jun 7, 5:19 pm, Phillip B. [email protected]

Jason R. wrote:

On 6/7/07, jdl [email protected] wrote:

difference = list_stripes.planned.to_i - list_stripes.spent.to_i

Well, I think the OP needs basic programming lessons, THEN Ruby lessons,
THEN Rails.

class Integer
def negative
self != 0 && (self != (self * self) / self.abs)
end
end

This always works for me.

Enjoy!

Guest wrote:

Jason R. wrote:

On 6/7/07, jdl [email protected] wrote:

difference = list_stripes.planned.to_i - list_stripes.spent.to_i

Well, I think the OP needs basic programming lessons, THEN Ruby lessons,
THEN Rails.

class Integer
def negative
self != 0 && (self != (self * self) / self.abs)
end
end

This always works for me.

Enjoy!

Your implementation lacks the question mark that is customary on methods
that return boolean values in Ruby. I find this method to be
satisfactory.

class Numeric
def negative?
if self.to_s =~ /^-(\d+).?(\d+)?\$/
true
elsif self.to_s =~ /^(\d+).?(\d+)?\$/
false
end
end
end

This also handles floating point values appropriately

• HTH HAND

Mathematica wrote:

Guest wrote:

Jason R. wrote:

On 6/7/07, jdl [email protected] wrote:

difference = list_stripes.planned.to_i - list_stripes.spent.to_i

Well, I think the OP needs basic programming lessons, THEN Ruby lessons,
THEN Rails.

class Integer
def negative
self != 0 && (self != (self * self) / self.abs)
end
end

This always works for me.

Enjoy!

Your implementation lacks the question mark that is customary on methods
that return boolean values in Ruby. I find this method to be
satisfactory.

class Numeric
def negative?
if self.to_s =~ /^-(\d+).?(\d+)?\$/
true
elsif self.to_s =~ /^(\d+).?(\d+)?\$/
false
end
end
end

This also handles floating point values appropriately

• HTH HAND

class Numeric
def negative?
self.to_s =~ /^-/
end
end

Oh, oh, right in the face! I refactored your code!

GuestCorrector wrote:

I think you mean:

class Numeric
def negative?
(self.to_s =~ /^-/) == 0
end
end

Yeah, my bad.

On Jun 7, 7:21 pm, Guest [email protected] wrote:

def negative
satisfactory.

Oh, oh, right in the face! I refactored your code!
Are you guys serious? A string conversion and a regexp just to see
whether a number is less than zero or not?

Chris

Chris M. wrote:

On Jun 7, 7:21 pm, Guest [email protected] wrote:

def negative
satisfactory.

Oh, oh, right in the face! I refactored your code!
Are you guys serious? A string conversion and a regexp just to see
whether a number is less than zero or not?

Chris

Maybe it was our way of asking if the OP was serious?

It’s a mystery.

Guest

On 6/7/07, Chris M. [email protected] wrote:

lessons,

Enjoy!
elsif self.to_s =~ /^(\d+).?(\d+)?\$/
def negative?
self.to_s =~ /^-/
end
end

Oh, oh, right in the face! I refactored your code!

Are you guys serious? A string conversion and a regexp just to see
whether a number is less than zero or not?

Chris

I think someone needs to turn on his sarcasm detector.

Jason

Oh, come on. Give ''em a break.

Class Numeric
def negative?
self < 0
end
end

Guest wrote:

Mathematica wrote:

Your implementation lacks the question mark that is customary on methods
that return boolean values in Ruby. I find this method to be
satisfactory.

class Numeric
def negative?
if self.to_s =~ /^-(\d+).?(\d+)?\$/
true
elsif self.to_s =~ /^(\d+).?(\d+)?\$/
false
end
end
end

This also handles floating point values appropriately

• HTH HAND

class Numeric
def negative?
self.to_s =~ /^-/
end
end

Oh, oh, right in the face! I refactored your code!

I think you mean:

class Numeric
def negative?
(self.to_s =~ /^-/) == 0
end
end