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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:37:13+00:00 2026-06-06T22:37:13+00:00

I recently found that dynamically creating object and methods in Ruby is quite a

  • 0

I recently found that dynamically creating object and methods in Ruby is quite a work, this might be because of my background experience in Javascript.

In Javascript you can dynamically create object and it’s methods as follow:

function somewhere_inside_my_code() {
  foo = {};
  foo.bar = function() { /** do something **/ };
};

How is the equivalent of accomplishing the above statements in Ruby (as simple as in Javascript)?

  • 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-06T22:37:14+00:00Added an answer on June 6, 2026 at 10:37 pm

    You can achieve this with singleton methods. Note that you can do this with all objects, for example:

    str = "I like cookies!"
    
    def str.piratize
      self + " Arrrr!"
    end
    
    puts str.piratize
    

    which will output:

    I like cookies! Arrrr!
    

    These methods are really only defined on this single object (hence the name), so this code (executed after the above code):

    str2 = "Cookies are great!"
    puts str2.piratize
    

    just throws an exception:

    undefined method `piratize' for "Cookies are great!":String (NoMethodError)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently found an article online that told me about this: RewriteRule ^mock-up/([^/]+)/([^/]+) /mock-up/index.php?page=$1&section=$2
I recently found this post . It basically says that Eclipse has a modified
The obvious answer is to use Charset.defaultCharset() but we recently found out that this
I recently found that a query would not work on some of my records
I recently found that if i said: object latestPosts = new MyApp.Models.LatestPosts(); TryUpdateModel(latestPosts); The
I recently found that this is impossible in SQLite: SELECT * FROM fruit WHERE
I recently found that one of my services was taking a large amount of
I am a windows dev, but I have recently found that I need to
I have recently found out that there exists a method called nth_element in the
I have recently found out that no argument constructor and multiple argument constructor cannnot

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.