So I’m taking a class on Ruby and it seems as though the teacher has
thrown something over my head.
List how many times each number from 0 to 9 was mentioned.
Example input:
ruby most-common.rb 4 2 1 1 1 2
Example output:
1 was mentioned 3 time(s).
2 was mentioned 2 time(s).
4 was mentioned 1 time(s).
The numbers in the command-line arguments must always be between 0 and
9, inclusive.
Do I loop over each element of an array of ARGV’s and test to see if
each new element of the array is equal to any of the others. And if so,
do I set up a count?
numbers = ARGV
for number in numbers
if number > 0 && < 9
???
end
puts "#{number} was mentioned #{count} times?
AHHHGH!!!
Since this is classwork, I won’t give you the answer, but. Here’s some
things I
would do (and trust me there are much smarter people on this list),
but I
would do probably use the case statement here. Seems to be a logical
holding
point for things.
def sort_numbers(argv_input)
case = argv_input
when “1” one =+1
etc…
end
numbers = ARGV.split(" ")
numbers.each do |num|
sort_numbers(num)
put your print routine here
end
Anyhow, that’s how I would tackle it. But I’m sure there are better ways
to code
this type of thing up.
Wayne
----- Original Message ----
From: Zebulon B. [email protected]
To: ruby-talk ML [email protected]
Sent: Tue, January 29, 2013 2:41:28 PM
Subject: newbie question…
So I’m taking a class on Ruby and it seems as though the teacher has
thrown something over my head.
List how many times each number from 0 to 9 was mentioned.
Example input:
ruby most-common.rb 4 2 1 1 1 2
Example output:
1 was mentioned 3 time(s).
2 was mentioned 2 time(s).
4 was mentioned 1 time(s).
The numbers in the command-line arguments must always be between 0 and
9, inclusive.
Do I loop over each element of an array of ARGV’s and test to see if
each new element of the array is equal to any of the others. And if so,
do I set up a count?
numbers = ARGV
for number in numbers
if number > 0 && < 9
???
end
puts "#{number} was mentioned #{count} times?
AHHHGH!!!
There are tons of examples on the web (and this forum) as to how to
count the frequency of an enumerable, string, etc…
Here’s a hint - look at the Hash type. Understand key-value pairs and
how to setup default values.
For the same reason, I won’t give you any answers, but your problem
could probably be solved using about two lines with the Array.inject
method and a Hash…think about it and ask questions. We are all here to
help, but we wouldn’t want it to be too easy for you 
-Ryan V.
And the minute I saw hash, a new answer dawned on me…
That’s why I
enjoy
reading this list, I’m always reminded of easier ways to do things.
----- Original Message ----
From: Scott RubyGuy [email protected]
There are tons of examples on the web (and this forum) as to how to
count the frequency of an enumerable, string, etc…
Here’s a hint - look at the Hash type. Understand key-value pairs and
how to setup default values.
I guess it’s time for me to jump ahead of our current skill-set!
On Tue, Jan 29, 2013 at 2:52 PM, Ryan V. [email protected]
wrote:
For the same reason, I won’t give you any answers, but your problem
could probably be solved using about two lines with the Array.inject
method and a Hash…think about it and ask questions. We are all here to
help, but we wouldn’t want it to be too easy for you 
Ok, I admit I’m stumped – I don’t get how to use a Hash inside an
Array.inject – I’m thinking it must be able to be done that way, but
I just cannot grok it.
#inject/#reduce/#fold are accumulators, but how does the Hash get
passed from round to round?
On Tue, Jan 29, 2013 at 8:45 PM, tamouse mailing lists
[email protected] wrote:
#inject/#reduce/#fold are accumulators, but how does the Hash get
passed from round to round?
Never mind, I got it…
Out of curiosity, what did you do to get it working? I played around in
irb after sending that (it was a quick guess, not anything I tried
first) and ended up using two lines, both with
Enumerable#each…couldn’t get the hash to be used as the accumulator.
-Ryan V.
u can try this,
string = ‘hello’
string.split(’’).each.inject(Hash.new(0)) { |h, char| h[char] += 1; h }
Prasad C
On Tue, Jan 29, 2013 at 10:08 PM, Ryan V. [email protected]
wrote:
Out of curiosity, what did you do to get it working? I played around in
irb after sending that (it was a quick guess, not anything I tried
first) and ended up using two lines, both with
Enumerable#each…couldn’t get the hash to be used as the accumulator.
I forgot one of ruby’s basic things. It always returns the last value
in the block.
There’s always the option of handy methods like split, uniq, scan, and
length. I did it one line using a combination of those (and sort for
neatness).