1

I'm struggling with a simple question how to instantiate a class with arguments. For example i have a class with an initializer that takes two arguments and a class method:

class MyClass
  attr_accessor :string_1, :string_2

  def initialize(string_1, string_2)
    @string_1 = string_1    
    @string_2 = string_2
  end

  def self.some_method
    # do something
  end

end

If some_method were an instance method i could instantiate a new object of MyClass and call the instance method like:

MyClass.new("foo", "bar").some_method

But how can i achieve that for the MyClass itself and the class method instead of an instance?

Something like MyClass.self("foo", "bar").some_method does not work.

3
  • 2
    Classes aren't really meant to hold state. That's the instance's job. So if you want the class method some_method to act on some data, the normal thing to do would be to pass that data as arguments: def self.some_method(string_1, string_2). What are you trying to achieve? If you are in control of the class, I'd advice making some_method an instance method. Commented May 1, 2016 at 18:50
  • thanks for elaboration, i was just curious whether it's possible to have a counterpart of MyClass.new("foo", "bar").some_method on class level. Commented May 1, 2016 at 18:58
  • 1
    I'd like to emphasize @MartinSvalin's point. In my experience, putting state in classes (as opposed to instances of classes) is usually a very bad idea. You're making an assumption that every single instance will conform to that state, and even when it is reasonable to think so at the time, things can change over time. If the initialized data is dependent on the environment (e.g. OS) then it's fine, but otherwise I'd recommend against it. Commented May 2, 2016 at 1:50

2 Answers 2

2

You could do this.

class MyClass
  singleton_class.send(:attr_accessor, :string_3)
end

MyClass.string_3 = "It's a fine day."
MyClass.string_3 #=> "It's a fine day."

@string_3 is a class instance variable.

Sign up to request clarification or add additional context in comments.

Comments

1

I believe the conventional way to initiate a class and then run a method on those values would be like:

class MyClass
  def initialize(string_1, string_2)
    @string_1 = string_1
    @string_2 = string_2
  end

  def some_method
    "#{@string_1} #{@string_2}"
  end
end

a = MyClass.new("foo", "bar")
puts a.some_method #=> "foo bar"

If you want to use attr_accessor then you can bypass the some_method to return those values:

class MyClass 
  attr_accessor :string_1, :string_2
  def initialize(string_1, string_2)
    @string_1 = string_1
    @string_2 = string_2
  end
end

a = MyClass.new("foo", "bar")
puts a.string_1 + a.string_2 #=> "foobar"

1 Comment

This answer describes the way to create an instance of a class. The OP stated that this is not what was desired.

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.