Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's say <code>column_array[i] = "foo"</code>, for an example.</p> <p>If you want to call the <em>method</em> <code>r.foo</code>, use <code>Object#send</code>:</p> <pre><code> r.send(column_array[i], arg1, arg2, arg3, ...) </code></pre> <p>If you want to access <code>r</code>'s instance variable <code>@foo</code>, use <code>Object#instance_variable_get</code> and <code>Object#instance_variable_set</code>:</p> <pre><code> r.instance_variable_get('@'+column_array[i]) r.instance_variable_set('@'+column_array[i], new_value) </code></pre> <p>In this case we have to prepend the given name with an <code>@</code> sigil, since that is required at the start of all instance variable names.</p> <p>Since this is rails, and there's a whole lot of <code>ActiveRecord</code> magic going on with your models (and I'm guessing <code>Student</code> is a subclass of <code>ActiveRecord::Base</code>) you probably want to use the former, since <code>ActiveRecord</code> creates methods to access the database, and the values stored in instance variables may not be what you want or expect.</p> <p>I'll use an example from some test data I've got lying around:</p> <pre><code>% script/console Loading development environment (Rails 2.3.2) irb&gt; Customer #=&gt; Customer(id: integer, date_subscribed: datetime, rental_plan_id: integer, name: string, address: string, phone_number: string, credit_limit: decimal, last_bill_end_date: datetime, balance: decimal) irb&gt; example_customer = Customer.find(:all)[0] #=&gt; #&lt;Customer id: 6, date_subscribed: "2007-12-24 05:00:00", rental_plan_id: 3, name: "Evagation Governessy", address: "803 Asbestous St, Uneradicated Stannous MP 37441", phone_number: "(433) 462-3416", credit_limit: #&lt;BigDecimal:191edc0,'0.732E3',4(12)&gt;, last_bill_end_date: "2009-05-15 04:00:00", balance: #&lt;BigDecimal:191e870,'0.743E3',4(12)&gt;&gt; irb&gt; example_customer.name #=&gt; "Evagation Governessy" irb&gt; field = 'name' #=&gt; "name" irb&gt; example_customer.instance_variable_get(field) NameError: `name` is not allowed as an instance variable name from (irb):8:in `instance_variable_get` from (irb):8 irb&gt; example_customer.instance_variable_get('@'+field) #=&gt; nil irb&gt; example_customer.send(field) #=&gt; "Evagation Governessy" irb&gt; example_customer.send(field+'=', "Evagation Governessy Jr.") #=&gt; "Evagation Governessy Jr." irb&gt; example_customer.send(field) #=&gt; "Evagation Governessy Jr." irb&gt; example_customer.name #=&gt; "Evagation Governessy Jr." </code></pre> <p>So you can see how <code>#send(field)</code> accesses the record information, and trying to access the attributes doesn't. Also, we can use <code>#send(field+'=')</code> to change record information.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload