Variable Issue

Discussion in 'Ruby' started by robzdc, Mar 15, 2013.

  1. #1
    Hi, I have an issue with a variable creating an array...
    First, I initialize the array.
    @data = []
    Code (markup):

    Then I add the data.
    @data << [...]
    Code (markup):

    Then I display the results
    <% @data.each do |data| %>
    Code (markup):

    It works great... but if I change the variable name to @x it fails and display an empty array...
    I dont understand why.
    Any help please?
     
    Solved! View solution.
    robzdc, Mar 15, 2013 IP
  2. robzdc

    robzdc Greenhorn

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    11
    #2
    There is something weird here... kind of cache or something...

    I change all variables to @x but in html I leave @data.each do |data| and it keeps sending me the same results...
     
    robzdc, Mar 15, 2013 IP
  3. robzdc

    robzdc Greenhorn

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    11
    #3
    Done.
    I had to restart the server..
    The weird thing is that Im in development environment...
     
    robzdc, Mar 15, 2013 IP
  4. southwind19

    southwind19 Active Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    73
    #4
    robzdc, this is not an error thats how variables are meant to function in Ruby.

    @variable stands for instance variable, by using that variable the data can be used in any function with in the class.

    When you did @data, u created such a variable and this variable can be accessed in the view.

    When you use "variable" a local variable is made it has validity only within that function you can't use that variable in the view.
     
    southwind19, Mar 28, 2013 IP
  5. robzdc

    robzdc Greenhorn

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    11
    #5
    The error was because I'm using the function in lib/ directory, so I have to restart the server to reload the data.
     
    robzdc, Apr 1, 2013 IP
  6. #6
    Ah I c, well the reason why you had to restart, to make use of those values was because Rails would load (Loading to memory) the contents of lib/ only during its initial start.

    Thanks for posting the question, learned something new today.. :)
     
    southwind19, Apr 2, 2013 IP