title: Ruby add variables to an existing object?
body: <p>How can I add variables to an existing obejct?
  I have a list of chat rooms and I want to add a new variable for each chat to use at my view:</p>
  
  <p>Example I want to add total users of chat</p>
  
  <pre><code>def index
    chats_all = ChatRoom.all
    @chats = Array.new
    chats_all.each |chat|
      chat.total_users = 10
      @chats &lt;&lt; chat
    end
    @chats
  end
  </code></pre>
  
  <p>total_users is not an attribute of ChatRoom class.</p>
  
  <p><strong>[EDIT - explaim better after @jvillian great awnser]</strong></p>
  
  <p>I don't want total_users as an attribute of User class. <br>
  I just want to add as a variable to use at this one single page. For json rails already let my add new attributes to objects. Just need to use as_json().map and a merge()<br>
  Example:<br></p>
  
  <pre><code>def index
    chats = chats.as_json().map {
      |chat| 
      chat.merge(
        total_users: 10
      }
    response = { chats: chats }
    render json: response
  end
  </code></pre>
  
  <p>Now I got an json with chats and each chat has total_users attribute.</p>
  
  <p>I want to know if I can do something like this with objects, just add a temporary variable to use at index page.</p>