3

In javascript, you can create blank objects that are not an instance of anything. Like so:

foo = {
    bar: 12,
    fooFunc: function() {
        return this.bar;
    }
}

Is this(or something similar) possible in ruby?

3
  • 1
    You can singleton an instance from Object and that would be the same. Commented Jun 17, 2013 at 2:04
  • Hope this article will be useful devblog.avdi.org/2010/05/12/quickie-objects-in-ruby Commented Jun 17, 2013 at 2:05
  • 2
    also take a look at OpenStruct which allow you to add attributes at later points Commented Jun 17, 2013 at 3:53

1 Answer 1

1

Ruby has an Object class from which all other objects inherit. From the Ruby docs:

Object is the parent class of all classes in Ruby. Its methods are therefore available to all objects unless explicitly overridden.

Object mixes in the Kernel module, making the built-in kernel functions globally accessible.

In Ruby 1.9:

Object.new.class #=> Object

Object.class #=> Class
Object.superclass #=> BasicObject

BasicObject.class #=> Class
BasicObject.superclass #=> nil
Sign up to request clarification or add additional context in comments.

2 Comments

Glad it helped. Would you consider accepting this answer as correct, then?
I will indeed, but I am required to wait another two minutes before it lets me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.