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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:30:16+00:00 2026-05-25T09:30:16+00:00

Given how expressive Ruby is, I’m wondering if anyone has ever tried to create

  • 0

Given how expressive Ruby is, I’m wondering if anyone has ever tried to create a class or module that will mimick JS object syntax. For instance, in JS I can of course do this:

[1]  var obj = {a: 'b'};
[2]  obj.c = 'd';
[3]  obj.a = 123
[4]  obj['e'] = 'f';
[5]  obj.e = obj['a']

In Ruby I can, as of now, have code that will permit me to do something like this:

[1]  obj = {'a' => 'b'}.to_js
[2]  # obj.c = 'd' << This is what I can't solve, 'c' is first defined here.
[3]  obj.a = 123;
[4]  obj['e'] = 'f'
[5]  obj.e = obj['a']

As long as I get a symbol through square brackets or on initialization, I can easily store the K/V pair and create instance methods for the setters and getters.

However, I haven’t been able to figure out how to create an object that will respond to ‘c’ if it’s not defined, then do some magic. For instance,

  • I can iterate over the hash coming in, and set up accessors
  • I can use []=(k, v) for the bracket notation and do the same thing.

My progress in the second style up to this point.

There’s a very specific Object#respond_to_missing? which can take a specific symbol in it; but can’t respond in the general case.

There’s three possible ways that I can think of solving the commented out, second invocation style:

  • Make the object not an instance of a class but a Method at all times. For instance, the following works:
class A
  def b; puts 'c'; end
end
def always
  A.new
end

c = always
c.b

Given this, there may be a possible way to do some kind of error catching and then hook the object accordingly.

  • Utilize some fantastic ruby feature that I haven’t heard of. Ruby is such a rich language, this may already be quite easy.

  • Override the error handling mechanics somehow. I don’t have any idea how someone would do this in the large general case, but basically you’d be telling ruby that if any instance of some class failed to respond to a method, you’d have some generalized handler to deal with things.

Anyway, if anyone has any idea here, it would be a pretty fun exercise I think. Thanks!

  • 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-25T09:30:16+00:00Added an answer on May 25, 2026 at 9:30 am

    Probably better ways of doing this:

    class Hash
      def method_missing(symbol, opts = nil)
        string = symbol.to_s
        self[string[0..-2].to_sym] = opts if string[-1..-1] == '=' and opts
        self[symbol]
      end
    end
    

    If you want to subclass, than you can do this:

    class JSHash < Hash
      def method_missing(symbol, opts = nil)
        string = symbol.to_s
        self[string[0..-2].to_sym] = opts if string[-1..-1] == '=' and opts
        self[symbol]
      end
    end
    
    class Hash
      def to_js
        JSHash.new.merge! self
      end
    end
    

    I’ll keep this open for a while in case someone else has an idea of a better way here.

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

Sidebar

Related Questions

A while back I came across a ruby gem that, given a regular expression,
Given a string in Ruby 1.8.7 (without the awesome Oniguruma regular expression engine that
In the Sequel ORM for Ruby, the Dataset class has an all method which
Given the following Expression<Func<T,bool>> matchExpression; How can i create another expression that is a
Given a regular expression, I'm looking for a package which will dynamically generate the
What regular expression can I use (if any) to validate that a given string
I need to reduce this boolean expression to its simplest form. Its given that
Given an Expression<Func<T, object>> (e.g. x => x.Prop1.SubProp), I want to create a string
Given an expression that points to a property in an object graph, I want
Given a C++ template class (or function) definition: template<typename T> struct Foo { T

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.