Gdohfg
Gdohfg

Reputation: 77

Ruby, run from command line

I have my code:

class String
  def freq
    str = String "a string!"
    self.upcase.each_char { |c| puts c + "!" }
    str = text.split("  ")
    wordFrequencies = Hash.new(0)
    str.each { |str| wordFrequencies[str] += 1 }
    wordFrequencies = wordFrequencies.sort_by {|a, b| b }
    wordFrequencies.reverse!
    wordFrequencies.each { |str, wordFrequencies | puts str + " " + wordFrequencies.to_s }
  end
end

When I go to run it from the command line, I do:

ruby filename.rb 

and then nothing happens - can anyone explain why..?

Upvotes: 2

Views: 150

Answers (1)

Andrey Deineko
Andrey Deineko

Reputation: 52347

Nothing happens because you do nothing in the file.

To have some output you'd want to add

String.new.freq

as the last line of the file.

Alternatively, you can add the following line before the last end:

new.freq # create an instance of the class and call `freq` method on it

Both options would result in freq method being actually called and produce output.

Upvotes: 2

Related Questions