0

I have a class like so:

Railsapp/lib/five9_providers/record_provider.rb:

class Five9Providers::RecordProvider < Five9Providers::BaseProvider

  def add_record_to_list
    variable = 'test'
  end

end

Then, in a controller I have this:

Railsapp/app/controllers/five9_controller.rb:

class Five9Controller < ApplicationController

    def import
      record_provider = Five9Providers::RecordProvider.new()
      record_provider.add_record_to_list
      puts Five9Providers::RecordProvider::variable
    end

end

However, calling my controller method import just returns:

NoMethodError (undefined method 'variable' for Five9Providers::RecordProvider:Class)

How can I access variable from the recover_provider.rb class in my five9_controller.rb class?

EDIT:

Even when using @@variable in both my record_provider and my five9_controller, I still can't access that variable. I am calling it like so: puts @@variable.

3
  • 1
    This is extremely basic Ruby stuff. I would try at least learning Ruby before building a self-guided Rails app. Commented Dec 31, 2013 at 21:23
  • Sanity check: "recover_provider.rb" and "five9_controller.rb" are not classes. They're files, which happen to contain class definitions. Be careful how you refer to things as using the wrong terms will really obstruct your ability to be understood by others. Commented Dec 31, 2013 at 21:40
  • Thanks @theTinMan. Soaking up as much knowledge as I can. Commented Dec 31, 2013 at 21:48

2 Answers 2

1

As written, you cannot. variable is local to the instance method and can't be accessed by any Ruby expression from outside the method.

On a related point, the term "class variable" is typically used to refer to variables of the form @@variable.

Update: In response to your "Edit" statement, if you change variable to @@variable in your class, then there are techniques available to access that variable from outside the class, but a naked reference to @@variable isn't one of them. Carefully read the answers to the question you cited in your comment for more information.

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

10 Comments

Could you suggest an alternative? I was following the structure listed here: stackoverflow.com/questions/12590723/… What can I do to access variable?
@Luigi, you need to study how Ruby handles variable conventions and scope. techotopia.com/index.php/Ruby_Variable_Scope. A class variable would start with @@. An instance variable would start with @.
Thanks for the tip @mbratch. What is this talk about using constants instead of class variables though? Class variables seem to cause more harm than good. I will definitely check the link and continue my studying though, thanks for the help.
@Luigi, whether a class variable causes harm or not depends upon how well you design the code. They are available if and when needed and when used properly. :) You do need to determine if you need an instance variable versus a class variable (hard to tell with your brief example). Constants can be defined as well and accessed from the class. A discussion of the pros and cons of using class versus instance versus private variables and pros/cons of how Ruby handles them is way outside the scope of your originally posted question.
@mbratch Agreed on all fronts, I obviously have a lot to learn. I edited my original post to contain more information.
|
0

Best way is to set and get the value using methods. Below is a sample code

class Planet
  @@planets_count = 0

  def initialize(name)
    @name = name
    @@planets_count += 1
  end

  def self.planets_count
    @@planets_count
  end  

  def self.add_planet
    @@planets_count += 1
  end

  def add_planet_from_obj
    @@planets_count += 1
  end
end


Planet.new("uranus")
Plant.add_planet
obj = Planet.new("earth")
obj.add_planet_from_obj

Comments

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.