Need help on creating a LetterHistogram


#1

I’m in desperate need of help in creating a Letter Histogram in ruby, here is what I need the end product to produce.

irb(main):001:0> load ‘LetterHistogram.rb’
=> true
irb(main):002:0> h = LetterHistogram.new
=> #<LetterHistogram:0x00000000034fb020 @text=“Hello World!”>
irb(main):003:0> h.text => “Hello World!”
irb(main):004:0> h.text = “What’s it going to be then, eh?”
=> “What’s it going to be then, eh?”
irb(main):005:0> h.calculateFrequencies
NoMethodError: private method calculateFrequencies' called for #<LetterHistogram:0x00000000034fb020> from (irb):5 from C:/Program Files/Ruby24-x64/bin/irb.cmd:19:in
irb(main):006:0> h.display
A:*
B:*
C:
D:
E:***
F:
G:**
H:***
I:**
J:
K:
L:
M:
N:**
O:**
P:
Q:
R:
S:*
T:****
U:
V:
W:*
X:
Y:
Z:

any help on the matter wil be greatly apreciated

here is the code that i have so far

class LetterHistogram
attr_accessor :text
attr_reader :letters

def initialize(text)
    h = LetterHistogram.new
    {
        if 
    }
    @text = gets
    @letters = Hash.new(0)
end
def frequency
    @text = text.downcase.chars.each do |c|
        next if c =~ /\s/
        @letters[c] += 1
    end
    puts(@letters)
end
def histogram
  #@letters.each{|key,value| puts "#{key} + ':' + value.to_s + '*' * #{value}" }
end

runme = LetterHistogram.new("World")
runme.frequency

end


#2

Please show us the source code of LetterHistogram.rb


#3

that’s the issue i don’t have it all i know is that, i need to get to that point, ill add the slight bit of code i have


#4

ive added the code that i have, i need to know how to get to the point of where my output looks like the one above


#5
class A
  def initialize()   @h=Hash.new(0) end
  def append(str)    str.upcase.each_char {|c|  @h[c]+=1  if c !~ /\s/  } end
  def print()        @h.keys.sort.each {|k| puts "#{k}:#{'*' * @h[k]}" }  end
end

ARGV.each_with_object(A.new) { | str , a | a.append(str) }.print


#6

how would i go about testing this in terminal on mac