Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 914529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:44:02+00:00 2026-05-15T17:44:02+00:00

a rubygem I’m writing and that is useful for counting word occurrences in a

  • 0

a rubygem I’m writing and that is useful for counting word occurrences in a text, I choose to put 3 parameters in class constructor.

The code is working, but I want to refactor it for niceness.
In your experience, it’s easier to read/mantain/use as API a class with a constructor with no params and a lot of setters/getters method or a code like this one, with all the params in the constructor?

TIA

Paolo

def initialize(filename, words, hide_list)

  if ! filename.nil?
    @filename = filename
    @occurrences = read
  else
    @filename = STDIN
    @occurrences = feed
  end

  @hide_list = hide_list
  @sorted = Array(occurrences).sort { |one, two| -(one[1] <=> two[1]) }
  @words = words

end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:44:03+00:00Added an answer on May 15, 2026 at 5:44 pm

    You could do it the rails way, where options are given in a hash:

    def initialize(filename = nil, options = {})
      @hide_list = options[:hide_list]
      @words = options[:words]
    
      if filename
        @filename = filename
        @occurrences = read
      else
        @filename = STDIN
        @occurrences = feed
      end
    
      @sorted = Array(occurrences).sort { |one, two| -(one[1] <=> two[1]) }
    
    end
    

    Then you can call it like this:

    WC.new "file.txt", :hide_list => %w(a the to), :words => %w(some words here)
    

    or this:

    wc = WC.new
    wc.hide_list = %w(a the is)
    wc.words = %w(some words here)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty new to the world of RSpec. I'm writing a RubyGem that deals
Is there a Rails plugin or a rubygem that gives you a starting point
I find easy_install extremely useful for programming with Python, and the same as rubygem
I have understood that I could use the RubyGem Curb to fulfill my needs
Is there a good ruby gem for a WYSIWYG editor that will easily work
The following code gives a segmentation fault on the last line require 'rubygems' gem
How can you determine which rubygem is being used in response to a require
I'm having problems installing the RMagick rubygem on Centos 5. I've followed the steps
I have not found a tab-completion script for RubyGem in Bash/Zsh. Where can you
I having a problem: $ rake db:create (in /Users/devinross14/myapp) rake aborted! RubyGem version error:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.