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 %>
<%=item%>
<% else %>
<%=item%>
<% 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
···
On Mon, 17 Feb 2003, kwatch wrote:
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 %>
<%=item%>
<% else %>
<%=item%>
<% end %>
<% end %>
…--------------------
I hope these methods are to be built-in methods.
Give your opinion, please.
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 end
del odd?() self. == 1 end
end
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
···
----- Original Message -----
From: “kwatch” kwatch@lycos.jp
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, February 17, 2003 4:04 AM
Subject: even? and odd? methods in Integer class
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.
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.
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):