Newbie question about classes

Is there any special way to define a class in Ruby that will have just
one instance object? I do not need even a class name.

I mean something simple like that

obj = class { def foo; puts “Foo”; end }.new

You can use the singleton lib, look:

Your “new” method will become a private method and you`ll instantiate
object using “.instance”

I hope to be helpful.

Thank you Wender J. for your fast answer. I read the referenced
documentation but I do not think it is what I was looking for. In fact,
I am not trying to avoid multiple instances. I devise the use of an
object of a class as a mean to access users methods from within a class
I am writing. Something like that;

User’s code

class Anyname
def something

do something


def something_else

do something_else

end # Anyname

obj =
another_obj =

My class

class MyClass
def initialize(user_obj)
@user = user_obj

def anything
@user.something if @user.respond_to?(“something”)

def anything_else
@user.something_else if @user.respond_to?(“something_else”)
end # MyClass

In this case, it is pointless to create more than one object Anyname.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs