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 6142539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:24:03+00:00 2026-05-23T18:24:03+00:00

Maybe this is a stupid idea… I am new to Ruby (and to OOP,

  • 0

Maybe this is a stupid idea… I am new to Ruby (and to OOP, so I still dont really know what I am doing most of the time), and I thought of a small, fun project to build, and I am struggling with some concepts.

What I am building is basically a string manipulator. I am building a module with extra methods, and then including that on the String class.

My module has several methods that manipulate the strings in different ways, mostly replacing words, and then return the modified string.

What I want to do in order to make the string manipulation more ‘spontaneous’ and natural, is to create a “main” method (the one I will be calling from the strings), that randomly selects one of the string manipulation methods, and then returns the string (and then can be called again to apply several manipulations in one go)

How can I do this, or something similar? Hope I explained myself

Thanks

O.

  • 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-23T18:24:04+00:00Added an answer on May 23, 2026 at 6:24 pm

    Here’s the random manipulation module, as you described it. something_random is the main method:

    module RandomStringManipulation
    
      def something_random
        methods = RandomStringManipulation.instance_methods
        methods -= [:something_random]
        send methods.sample    # Ruby >= 1.9 required.  See below for Ruby 1.8.
      end
    
      def foo
        self + "foo"
      end
    
      def bar
        self + "bar"
      end
    
    end
    

    Mix it into String:

    class String
      include RandomStringManipulation
    end
    

    Now we can create an empty string, and then do something random to it a few times, printing it out each time:

    s = ""
    4.times do
      s = s.something_random
      p s
    end
    
    # => "foo"
    # => "foobar"
    # => "foobarbar"
    # => "foobarbarfoo"
    

    There are two bits that are interesting. The first is this:

    methods -= [:something_random]
    

    That removes :something_random from the array methods, preventing the *something_random* method from calling itself. The second interesting bit is this:

    send methods.sample
    

    Array.sample (Ruby >= 1.9) selects a random method. send then dispatches that method. In Ruby 1.8, do this instead:

    send methods[rand(methods.size)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe this is a stupid question, but I dont know how to use Membership
I know maybe this is a stupid question, But i suck at sql But
Maybe this is just a stupid idea, that's why I toss it in here:
I'm new to zend framework so maybe this question is stupid.. I've got a
I know, maybe this question is stupid but I am stuck and I need
I'am new to jquery and maybe this is a stupid question but I have
This maybe a stupid question, but as I can not easily undo my change
This question is for a web application. And maybe it's a stupid question but
Well, maybe it is a stupid question, but I cannot resolve this problem. In
I'm new to ASP.NET MVC so this may be a stupid question. I have

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.