[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

issue using method 'odd?'

Mmcolli00 Mom

1/20/2009 4:26:00 PM

I keep getting a no method error when trying to find out if a count is
an odd number. I had tested the code with actual numbers and it works
fine. It seems like it is not liking how I am trying to use the method
'odd?' The count's class is of FixNum. I have tried converting to
interger but got same result. Here is the code that I am using. Do you
know if I need a gem or what I have not done here? Thanks. MC

searchText.rb:38: undefined method `odd?' for 8:Fixnum (NoMethodError)
from searchText.rb:30:in `each'
from searchText.rb:30
*****************************************
count1 = Hash.new(0)

File.open("NTemp.txt") do |src|
src.each_line do |line|
word1 = line.chomp.split(",")
count1[word1] += 1
#~ count2[word2] += 1
end
end

words = (count1.keys).uniq
words.each do |word|

#~ if count[word] == 1
#~ if count[word] == 2

if count[word].odd? == true
puts "Value does not contain a match: #{word}"
end
end
--
Posted via http://www.ruby-....

3 Answers

Jesús Gabriel y Galán

1/20/2009 4:35:00 PM

0

On Tue, Jan 20, 2009 at 5:26 PM, Mmcolli00 Mom <mmc_collins@yahoo.com> wrote:
> I keep getting a no method error when trying to find out if a count is
> an odd number. I had tested the code with actual numbers and it works
> fine. It seems like it is not liking how I am trying to use the method
> 'odd?' The count's class is of FixNum. I have tried converting to
> interger but got same result. Here is the code that I am using. Do you
> know if I need a gem or what I have not done here? Thanks. MC
>
> searchText.rb:38: undefined method `odd?' for 8:Fixnum (NoMethodError)
> from searchText.rb:30:in `each'
> from searchText.rb:30
> *****************************************
> count1 = Hash.new(0)
>
> File.open("NTemp.txt") do |src|
> src.each_line do |line|
> word1 = line.chomp.split(",")
> count1[word1] += 1
> #~ count2[word2] += 1
> end
> end
>
> words = (count1.keys).uniq
> words.each do |word|
>
> #~ if count[word] == 1
> #~ if count[word] == 2
>
> if count[word].odd? == true
> puts "Value does not contain a match: #{word}"
> end
> end
> --
> Posted via http://www.ruby-....
>
>



Ruby 1.8 doesn't have an odd? method per se. Rail's ActiveSupport adds
one to Integer. The code is quite simple, so I wouldn't require active
support only for this:

module EvenOdd
def multiple_of?(number)
self % number == 0
end

def even?
multiple_of? 2
end if RUBY_VERSION < '1.9'

def odd?
!even?
end if RUBY_VERSION < '1.9'
end

Ruby 1.9 has Fixnum#odd? though.

Jesus.

Michael Morin

1/20/2009 4:49:00 PM

0

Jesús Gabriel y Galán wrote:
> Ruby 1.8 doesn't have an odd? method per se. Rail's ActiveSupport adds
> one to Integer. The code is quite simple, so I wouldn't require active
> support only for this:

Ruby 1.8.7 has odd?

uzimonkey@WOPRJr:~$ ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
uzimonkey@WOPRJr:~$ ruby -e 'puts 1.odd?'
true


Jesús Gabriel y Galán

1/20/2009 5:02:00 PM

0

On Tue, Jan 20, 2009 at 5:48 PM, Michael Morin <uzimonkey@gmail.com> wrote:
> Jes=FAs Gabriel y Gal=E1n wrote:
>>
>> Ruby 1.8 doesn't have an odd? method per se. Rail's ActiveSupport adds
>> one to Integer. The code is quite simple, so I wouldn't require active
>> support only for this:
>
> Ruby 1.8.7 has odd?
>
> uzimonkey@WOPRJr:~$ ruby -v
> ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
> uzimonkey@WOPRJr:~$ ruby -e 'puts 1.odd?'
> true

Doh, you are right ! I was checking 1.8.6.

Jesus.