Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make a Ruby method to pass output parameters (change the value of referenced arguments)?
    primarykey
    data
    text
    <p>I'm trying to make a method with output arguments in ruby.</p> <p>I read differents posts <a href="https://stackoverflow.com/questions/6527034/when-passing-objects-as-parameters-are-they-passed-by-reference">here</a> and <a href="https://stackoverflow.com/questions/1872110/is-ruby-pass-by-reference-or-by-value">here</a> about the discussion of wether ruby pass its arguments by-value or by-reference and</p> <p>I undersand that on a strict sens, Ruby always pass-by-value, but the value passed is actually a reference. Reason why there is so much debate on this.</p> <p>I find out that there are several ways to change the value of the referenced variable. For instance with the <strong>replace</strong> method when its an Array, a Hash or a String, or <strong>merge!</strong> when it's a hash.</p> <p>I found out that with <strong>integer</strong>, I can change and pass the value outside my method without any special method use.</p> <p><strong>My question is about other objects</strong>. For instance I want to retrieve the 'id' attribute of an object, and the object reference itself :</p> <pre><code> class RestaurantController &lt; ApplicationController def pizza_to_deliver(pizza_name, id_of_the_order, pizza) # pizza to eat pizza = Pizza.where(:name =&gt; pizza_name).first # unknown pizza return false if pizza.nil? # first customer order about this pizza id_of_the_order = Orders.where(:pizza_id =&gt; pizza.id).first true end end my_pizza_name = 'margerita' My_order_id = nil my_pizza = nil my_restaurant = RestaurantController.new if my_restauant.pizza_to_deliver(my_pizza_name, My_order_id, my_pizza) then puts "Pizza to deliver : #{my_order_id}" rex_dog.eat(my_pizza) end </code></pre> <p>How to make this works ? (order_id and my_pizza remains with nil)</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. 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