2

Is there a simple way to initialize a class variable through a class method in Ruby? I'm trying this:

class MyClass
    @@product_families = MyClass.load_pgrollups(File.join(File.dirname(__FILE__), ASSETS_FOLDER_NAME, PGROLLUP_CSV_FILENAME))

    def self.load_pgrollups(csv_file)                                                                                                                                        
       ....

       return product_families
    end

I'm getting an exception: undefined method `load_pgrollups' for ModuleName::myClass:Class

I don't necessarily want to initialize a class variable. I also tried to initialized a constant in a module through a module function

module ModuleName
    PRODUCT_FAMILIES = load_pgrollups(File.join(File.dirname(__FILE__), ASSETS_FOLDER_NAME, PGROLLUP_CSV_FILENAME))

    def load_pgrollups(csv_file)                                                                                                                                        
       ....

       return product_families
    end

but I got undefined method `load_pgrollups' for MyModule:Module

3
  • 2
    class names in ruby don't start with lowercase, secondly where are you getting DataHandler from?? Commented Mar 19, 2014 at 0:25
  • DataHandler should have been turned into myClass as I tried to replace the most of the names with generic ones. I was editing an existing Ruby code. I didn't have any experience in Ruby before and had no understanding that a class name can not start with a lowercase letter. Thanks for the comment. Commented Mar 19, 2014 at 17:06
  • @bjhaid, edited the question with your comments Commented Mar 19, 2014 at 17:30

2 Answers 2

5

Call it after it is defined:

class myClass
  def self.load_pgrollups(csv_file)                                                                                                                                        
    ....
    return product_families
  end
  @@product_families = load_pgrollups(File.join(__dir__, ASSETS_FOLDER_NAME, PGROLLUP_CSV_FILENAME))
end
Sign up to request clarification or add additional context in comments.

Comments

2

You define the method in line 4, but you are already calling it in line 2, where it hasn't been defined yet. So, yes, the method is undefined at the point you are calling it.

4 Comments

I think this is wrong cause every thing is compiled before it runs. So it doesn't matter
@artmees: Code in Ruby, like in pretty much every other programming language on the planet gets run from top to bottom. So, line 2 which calls the method is run before line 4, which defines the method. Ergo, since at the time that you call the method, it hasn't been defined yet, you get a NoMethodError.
It turns out it does - see here and here. Quote from the first one - "The standard 1.8.7 implementation is written in C, as a single-pass interpreted language." It was a surprise, because the most of modern programming languages don't care about whether the method is defined before the first call or after, i.e. Python is always compared to Ruby, but Python doesn't care about the order.
JörgWMittag, your answer also works, thank you. @sawa gave a code example, so I marked his answer as accepted.

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.