Any reason why this code should not work - The Ruby Way, p270

V

Victor Reyes

[Note: parts of this message were removed to make it a legal post.]

I lifted the following code from The Ruby way and tried to use it.
It does not output anything.
Do anyone knows why?

Thank you

Victor

##############################
# This code adds a count method to the Array class
# This code was lifted from:
# The Ruby Way
# Second Edition
# By: Hal Fulton
# Page: 270
##############################
class Array
def count
k=Hash.new(0)
self.each{|x| k[x]+1 }
k
end
end



@aa = [

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789]
]

@ga = [
[0,0,0,0,1,9,0,4,0],
[0,0,4,8,0,0,6,0,0],
[7,5,0,0,0,0,0,0,2],
[0,9,0,1,0,2,0,0,4],
[0,0,0,0,0,3,0,0,0],
[5,0,0,4,0,6,0,3,0],
[8,0,0,0,0,0,0,7,3],
[0,0,6,0,0,8,4,0,0],
[0,1,0,2,9,0,0,0,0]
]


# Test the count method:

f = @ga[0].count
puts "f = #{f}" # The output here was: f =

f = @aa.count
puts "f = #{f}" # The output here was: f =
 
J

Jano Svitok

I lifted the following code from The Ruby way and tried to use it.
It does not output anything.
Do anyone knows why?

Thank you

Victor

##############################
# This code adds a count method to the Array class
# This code was lifted from:
# The Ruby Way
# Second Edition
# By: Hal Fulton
# Page: 270
##############################
class Array
def count
k=Hash.new(0)

- self.each{|x| k[x]+1 }
+ self.each{|x| k[x]+=1 }

# notice the = sign, you have not stored the result anywhere.
k
end
end



@aa = [

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789]
]

@ga = [
[0,0,0,0,1,9,0,4,0],
[0,0,4,8,0,0,6,0,0],
[7,5,0,0,0,0,0,0,2],
[0,9,0,1,0,2,0,0,4],
[0,0,0,0,0,3,0,0,0],
[5,0,0,4,0,6,0,3,0],
[8,0,0,0,0,0,0,7,3],
[0,0,6,0,0,8,4,0,0],
[0,1,0,2,9,0,0,0,0]
]


# Test the count method:

f = @ga[0].count
puts "f = #{f}" # The output here was: f =

f = @aa.count
puts "f = #{f}" # The output here was: f =
 
R

Rolando Abarca

I lifted the following code from The Ruby way and tried to use it.
It does not output anything.
Do anyone knows why?

Thank you

Victor

##############################
# This code adds a count method to the Array class
# This code was lifted from:
# The Ruby Way
# Second Edition
# By: Hal Fulton
# Page: 270
##############################
class Array
def count
k=Hash.new(0)
self.each{|x| k[x]+1 }
k
end
end

I think it should read:

def count
k = Hash.new(0)
self.each { |x| k[x] += 1 }
k
end

The problem is that inside the 'each' loop you're not asigning any
value.
regards,
 
J

Jesús Gabriel y Galán

I lifted the following code from The Ruby way and tried to use it.
It does not output anything.
Do anyone knows why?

Thank you

Victor

##############################
# This code adds a count method to the Array class
# This code was lifted from:
# The Ruby Way
# Second Edition
# By: Hal Fulton
# Page: 270
##############################
class Array
def count
k=Hash.new(0)
self.each{|x| k[x]+1 }

I think this line should be
self.each{|x| k[x] += 1}

(notice the = sign, so the value gets assigned to the key).
Otherwise you are never adding entries to the hash
k
end
end

Hope this helps,

Jesus.
 
V

Victor Reyes

[Note: parts of this message were removed to make it a legal post.]

Embarrassing!!!!!!!!! To say the least!
Even when I copy, I don't copy right!

Thank you Jano!

I lifted the following code from The Ruby way and tried to use it.
It does not output anything.
Do anyone knows why?

Thank you

Victor

##############################
# This code adds a count method to the Array class
# This code was lifted from:
# The Ruby Way
# Second Edition
# By: Hal Fulton
# Page: 270
##############################
class Array
def count
k=Hash.new(0)

- self.each{|x| k[x]+1 }
+ self.each{|x| k[x]+=1 }

# notice the = sign, you have not stored the result anywhere.
k
end
end



@aa = [

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789]
]

@ga = [
[0,0,0,0,1,9,0,4,0],
[0,0,4,8,0,0,6,0,0],
[7,5,0,0,0,0,0,0,2],
[0,9,0,1,0,2,0,0,4],
[0,0,0,0,0,3,0,0,0],
[5,0,0,4,0,6,0,3,0],
[8,0,0,0,0,0,0,7,3],
[0,0,6,0,0,8,4,0,0],
[0,1,0,2,9,0,0,0,0]
]


# Test the count method:

f = @ga[0].count
puts "f = #{f}" # The output here was: f =

f = @aa.count
puts "f = #{f}" # The output here was: f =
 
V

Victor Reyes

[Note: parts of this message were removed to make it a legal post.]

Yes, Jesus - Thank you!
It is, as I said earlier, embarrassing!

BTW Jesus, on what country are you located? I'm in Brooklyn New York, USA.

Embarrassing!!!!!!!!! To say the least!
Even when I copy, I don't copy right!

Thank you Jano!


I lifted the following code from The Ruby way and tried to use it.
It does not output anything.
Do anyone knows why?

Thank you

Victor

##############################
# This code adds a count method to the Array class
# This code was lifted from:
# The Ruby Way
# Second Edition
# By: Hal Fulton
# Page: 270
##############################
class Array
def count
k=Hash.new(0)

- self.each{|x| k[x]+1 }
+ self.each{|x| k[x]+=1 }

# notice the = sign, you have not stored the result anywhere.
k
end
end



@aa = [

[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],[123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789]
]

@ga = [
[0,0,0,0,1,9,0,4,0],
[0,0,4,8,0,0,6,0,0],
[7,5,0,0,0,0,0,0,2],
[0,9,0,1,0,2,0,0,4],
[0,0,0,0,0,3,0,0,0],
[5,0,0,4,0,6,0,3,0],
[8,0,0,0,0,0,0,7,3],
[0,0,6,0,0,8,4,0,0],
[0,1,0,2,9,0,0,0,0]
]


# Test the count method:

f = @ga[0].count
puts "f = #{f}" # The output here was: f =

f = @aa.count
puts "f = #{f}" # The output here was: f =
 
V

Victor Reyes

[Note: parts of this message were removed to make it a legal post.]

Thank you Rolando. It was my silly typo!

On May 8, 2008, at 8:09 AM, Victor Reyes wrote:

I lifted the following code from The Ruby way and tried to use it.
It does not output anything.
Do anyone knows why?

Thank you

Victor

##############################
# This code adds a count method to the Array class
# This code was lifted from:
# The Ruby Way
# Second Edition
# By: Hal Fulton
# Page: 270
##############################
class Array
def count
k=Hash.new(0)
self.each{|x| k[x]+1 }
k
end
end

I think it should read:

def count
k = Hash.new(0)
self.each { |x| k[x] += 1 }
k
end

The problem is that inside the 'each' loop you're not asigning any value.
regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top