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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jun 7, 11:19 am, Phillip Baker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jun 7, 2007, at 12:42 PM, jdl wrote:> On Jun 7, 11:19 am, Phillip Baker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > 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.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 Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 6/7/07, jdl <jamesludlow-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Jun 7, 11:19 am, Phillip Baker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > 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. > >Well, I think the OP needs basic programming lessons, THEN Ruby lessons, THEN Rails. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Just use one of the following: difference > 0 difference >= 0 difference < 0 difference <= 0 Chris On Jun 7, 5:19 pm, Phillip Baker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jason Roelofs wrote:> On 6/7/07, jdl <jamesludlow-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Guest wrote:> Jason Roelofs wrote: >> On 6/7/07, jdl <jamesludlow-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mathematica wrote:> Guest wrote: >> Jason Roelofs wrote: >>> On 6/7/07, jdl <jamesludlow-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 HANDclass Numeric def negative? self.to_s =~ /^-/ end end Oh, oh, right in the face! I refactored your code! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
GuestCorrector wrote:> > I think you mean: > > class Numeric > def negative? > (self.to_s =~ /^-/) == 0 > end > endYeah, my bad. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jun 7, 7:21 pm, Guest <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Mathematica wrote: > > Guest wrote: > >> Jason Roelofs wrote: > >>> On 6/7/07, jdl <jameslud...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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!Are you guys serious? A string conversion and a regexp just to see whether a number is less than zero or not? Chris --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Chris Mear wrote:> On Jun 7, 7:21 pm, Guest <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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? > > ChrisMaybe it was our way of asking if the OP was serious? It''s a mystery. Guest -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 6/7/07, Chris Mear <chris-OIzkuoyqg0kAvxtiuMwx3w@public.gmane.org> wrote:> > > On Jun 7, 7:21 pm, Guest <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > Mathematica wrote: > > > Guest wrote: > > >> Jason Roelofs wrote: > > >>> On 6/7/07, jdl <jameslud...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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! > > Are you guys serious? A string conversion and a regexp just to see > whether a number is less than zero or not? > > ChrisI think someone needs to turn on his sarcasm detector. Jason --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Oh, come on. Give ''''em a break. Class Numeric def negative? self < 0 end end On Jun 7, 1:25 pm, Guest <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> GuestCorrector wrote: > > > I think you mean: > > > class Numeric > > def negative? > > (self.to_s =~ /^-/) == 0 > > end > > end > > Yeah, my bad. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---