even? and odd? methods in Integer class 
Author Message
 even? and odd? methods in Integer class

Why Integer class doesn't have 'even?' nor 'odd?' method?
Of course, it is easy to define by myself.
But I wonder why these methods are not included in Ruby
by default?

.--------------------
 class Integer
   def even?
     return self % 2 == 0
   end
   def odd?
     return self % 1 == 0
   end
 end
.--------------------

I believe that these methods are very useful and friendly,
especially for Web Designers.
They're familiar with a concept about modulo, but not familiar
with an operator of modulo (= '%').

.--------------------
<% count = 0 %>
<% for item in list do %>
  <% count += 1 %>
  <% if count % 2 == 0 then %>   <!-- count.even? is more hopeful -->
    <tr class="even">
      <td><%=item%></td>
    </tr>
  <% else %>
    <tr class="odd">
      <td><%=item%></td>
    </tr>
  <% end %>
<% end %>
.--------------------

I hope these methods are to be built-in methods.
Give your opinion, please.

--
regards
kwatch



Fri, 05 Aug 2005 17:47:48 GMT  
 even? and odd? methods in Integer class

Quote:

> Why Integer class doesn't have 'even?' nor 'odd?' method?
> Of course, it is easy to define by myself.
> But I wonder why these methods are not included in Ruby
> by default?

> ..--------------------
>  class Integer
>    def even?
>      return self % 2 == 0
>    end
>    def odd?
>      return self % 1 == 0
>    end
>  end
> ..--------------------

> I believe that these methods are very useful and friendly,
> especially for Web Designers.
> They're familiar with a concept about modulo, but not familiar
> with an operator of modulo (= '%').

> ..--------------------
> <% count = 0 %>
> <% for item in list do %>
>   <% count += 1 %>
>   <% if count % 2 == 0 then %>   <!-- count.even? is more hopeful -->
>     <tr class="even">
>       <td><%=item%></td>
>     </tr>
>   <% else %>
>     <tr class="odd">
>       <td><%=item%></td>
>     </tr>
>   <% end %>
> <% end %>
> ..--------------------

> I hope these methods are to be built-in methods.
> Give your opinion, please.

Your odd? definition returns true for all ints.
All integers are 0 modulo 1.

You want self % 2 == 1:

irb(main):001:0> class Integer
irb(main):002:1> def odd?
irb(main):003:2> self % 1 == 0
irb(main):004:2> end
irb(main):005:1> end
nil
irb(main):006:0> 3.odd?
true
irb(main):007:0> 4.odd?
true
irb(main):008:0> class Integer
irb(main):009:1> def odd?
irb(main):010:2> self % 2 == 1
irb(main):011:2> end
irb(main):012:1> end
nil
irb(main):013:0> 3.odd?
true
irb(main):014:0> 4.odd?
false

Cheers,
Mike
--

    "I want to die peacefully in my sleep like my Grandfather...
  ... not screaming and yelling like the passengers in his car"



Fri, 05 Aug 2005 21:47:00 GMT  
 even? and odd? methods in Integer class
kwatch,

Quote:
> Why Integer class doesn't have 'even?' nor 'odd?' method?
> Of course, it is easy to define by myself.
> But I wonder why these methods are not included in Ruby
> by default?

> ..--------------------
>  class Integer
>    def even?
>      return self % 2 == 0
>    end
>    def odd?
>      return self % 1 == 0
>    end
>  end
> ..--------------------

    I don't know why these methods are not predefined, but there is a
simpler (and presumably faster) check for even and odd.  I use "mynumber[0]
== 0" to check for even and "mynumber[0] == 1" to check for odd.  It is
trivial to add these to the Integer class (which may be why these are not
predefined):

class Integer
  def even?() self.[](0) == 0 end
  del odd?() self.[](0) == 1 end
end

    So:

1.even? => false
1.odd?  => true

    Hope this helps!

    - Warren Brown



Sat, 06 Aug 2005 00:30:54 GMT  
 even? and odd? methods in Integer class
Quote:
----- Original Message -----

Newsgroups: comp.lang.ruby

Sent: Monday, February 17, 2003 4:04 AM
Subject: even? and odd? methods in Integer class

> Why Integer class doesn't have 'even?' nor 'odd?' method?
> Of course, it is easy to define by myself.
> But I wonder why these methods are not included in Ruby
> by default?

Frankly, I would like to see this (at least odd?
since even? is just the opposite -- Pascal went
this route -- but I am showing my age when I
mention that).

It's been discussed before, but I think Matz said
it was not needed often enough and/or was very
simple to implement anyway.

Of course, there are other more general choices
you could make also.

  count.mult?(n)  # is count a multiple of n?
  count.mod?(5,2) # is count mod 5 equal to 2?

The latter mod? method could have the second param
default to zero, so that it included the function
of mult? also.

Cheers,
Hal



Sat, 06 Aug 2005 05:43:08 GMT  
 even? and odd? methods in Integer class

Quote:
> Your odd? definition returns true for all ints.
> All integers are 0 modulo 1.

> You want self % 2 == 1:

Oh, sorry, you're right.
It's my mistake.

kwatch



Sat, 06 Aug 2005 11:01:54 GMT  
 even? and odd? methods in Integer class

Quote:

> Frankly, I would like to see this (at least odd?
> since even? is just the opposite -- Pascal went
> this route -- but I am showing my age when I
> mention that).

I know pascal, not delphi.
I'm showing my age, too.

Quote:

> It's been discussed before, but I think Matz said
> it was not needed often enough and/or was very
> simple to implement anyway.

> Of course, there are other more general choices
> you could make also.

>   count.mult?(n)  # is count a multiple of n?
>   count.mod?(5,2) # is count mod 5 equal to 2?

> The latter mod? method could have the second param
> default to zero, so that it included the function
> of mult? also.

> Cheers,
> Hal

Oh, it is right.
I must define if I want, and I can do.
Thanks Hal.

--
regards,
kwatch



Sun, 07 Aug 2005 03:37:20 GMT  
 even? and odd? methods in Integer class

Quote:

>    I don't know why these methods are not predefined, but there is a
> simpler (and presumably faster) check for even and odd.  I use "mynumber[0]
> == 0" to check for even and "mynumber[0] == 1" to check for odd.  It is
> trivial to add these to the Integer class (which may be why these are not
> predefined):

Neat! Never knew about Integer#[]

martin



Thu, 11 Aug 2005 04:22:12 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. J: Even and Odd

2. Odd and Even conjunctions

3. Odd and Even functions missing in RB

4. Even/odd week number

5. Printing Odd or Even page only

6. Odd or Even

7. determine whether a number is odd or even

8. Even/Odd rounding

9. Even/Odd rounding

10. odd /even addressing?

11. even vs. odd numbers

12. number representations (was: even vs. odd numbers)

 

 
Powered by phpBB® Forum Software