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

  • SEARCH
  • Home
  • 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 8095723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:13:40+00:00 2026-06-05T21:13:40+00:00

So I have a question of best practice in terms of extending functionality of

  • 0

So I have a question of best practice in terms of extending functionality of common Ruby datatypes.

So Rails of course has a great method pluralize which takes a string and pluralizes it. What I’m particularly interested in is that you can call

"square".pluralize

and receive

=> "squares"

in return. What I want to do is a similar sort of thing — except for a Hash. For the sake of this question, let’s say I have a method pluralize_each which pluralizes each key and value of a hash.

def pluralize_each hash
  hash.each {|key,value| puts key.pluralize + "=>" + value.pluralize }
end

How can I manipulate this method so that I can call it on just a hash?
For example, I want to be able to call

{"cat" => "dog"}.pluralize_each
#=> {"cats" => "dogs"}

(Let’s forget that this particular method isn’t fully functional right now, with nested hashes and such.)

Is there any way to do this except for extending the Hash class? I would think that there would be a way.

  • 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-06-05T21:13:42+00:00Added an answer on June 5, 2026 at 9:13 pm

    With a module:

    module HashPluralize
      def pluralize
        each { |key,value| puts "#{key.pluralize} => #{value.pluralize}" }
      end
    end
    
    Hash.send(:include, HashPluralize)
    
    # Usage example:
    {'user' => 'product'}.pluralize
    users => products
    

    EDIT: WHY?

    @KL-7 legitimately asked a class edit vs module extension. This is my opinion:

    If you are writing something organic, modules can be useful in order to optimize refactoring / code organization. Example:

    module Pluralization
    
      def self.included(klass)
        klass.send :include, Pluralization.const_get(klass.to_s)
      rescue TypeError
        raise NotImplementedError.new("A pluralization method for the #{klass}" +
                " class is not supported at the moment")
      end
    
      module Array
        def pluralize
          'array pluralization implementation'
        end
      end
    
      module Hash
        def pluralize
          'hash pluralization implementation'
        end
      end
    
    end
    
    Array.send :include, Pluralization
    Hash.send :include, Pluralization
    
    puts Array.new.pluralize
    puts Hash.new.pluralize
    

    This module permits you to define a clean way to define pluralizations for various classes; you could add a configuration, separate the pluralization methods in various files, etc.

    Anyway, if you just want to add a pluralize method to Hash class, there is no problem editing the Hash class (just ensure you are not overriding another method, with a method_defined? check f.e.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question concerning the performance, reliability, and best practice method of using
I have a question regarding best-practice when including headers. Obviously include guards protect us
Simple question on best practice. Say I have: public class Product { public string
I have a question about jQuery load best practice. Imagine the following situation: $(#div).on('click',
I have a best practice question. I'm developing a OO MVC framework in PHP,
I have a design/best practice question relating to MVC3. I have a front page
Quick question on best practice. I have an AJAX application where there are 4
I have a question about what would be the best practice for creating a
I have a question on best practice for relational databases and hierarchies. My question
I guess this is a best-practice question: We have a test server, a git

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.