Forum: Ruby pass by reference

96bb15ec886c45a93e0f32bb0b2e69c8?d=identicon&s=25 Kevin Abel (bklabel1)
on 2016-03-30 16:33
I'm new to Ruby.
Is there a way to pass a value by reference to a function in Ruby?
Thanks,
Kevin
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2016-03-30 23:13
Technically everything is by reference. If you directly modify the
variable inside the function it affects the outer variable.

[1] pry(main)> str = 'a'
=> "a"
[2] pry(main)> def upcase( val )
[2] pry(main)*   val.upcase!
[2] pry(main)* end
=> :upcase
[3] pry(main)> upcase str
=> "A"
[4] pry(main)> str
=> "A"
[5] pry(main)>
96bb15ec886c45a93e0f32bb0b2e69c8?d=identicon&s=25 Kevin Abel (bklabel1)
on 2016-03-30 23:45
Joel,
I'm a bit confused. I watched the Learn Ruby in an Hour video.  It
showed a numeric forgetting the change outside of the def.
I'm not sure how famous that video is but the rest of it looks accurate.
Thanks,
Kevin
4a65f01f7ece0b720bdb0de3c3db089e?d=identicon&s=25 Dansei Yuuki (blutorange)
on 2016-03-31 07:36
In ruby, everything is an object, and objects are passed by reference.
For performance reasons, there are some exceptions for primitive types.

Small numbers use the machine's integer types. Also, it would be strange
if you could modify a number: you could make the object 5 represent the
number 6..

Still, the following code shows  that even numbers behave like objects
passed by reference in some respects:


def f(x)
 puts x.frozen?
end
x=5
y=6
x.freeze
f(x) # => true
f(y) # => false
4a65f01f7ece0b720bdb0de3c3db089e?d=identicon&s=25 Dansei Yuuki (blutorange)
on 2016-03-31 09:34
Also, note that operators such as += affect the variable, not the object
itself:

> x = "Hello"
> y = x
> x << "Hello"
> x += " World"
> puts x # => HelloHello World
> puts y # => HelloHello
96bb15ec886c45a93e0f32bb0b2e69c8?d=identicon&s=25 Kevin Abel (bklabel1)
on 2016-04-01 17:27
Dansei ,

I appreciate you assisting me.  I am new to Ruby.

> def f(x)
>  puts x.frozen?
> end
> x=5
> y=6
> x.freeze
> f(x) # => true
> f(y) # => false

I am not sure what the property and question mark are used for at the
end of the x variable.

What is x.frozen and z.freeze?

Thanks,

Kevin
4a65f01f7ece0b720bdb0de3c3db089e?d=identicon&s=25 Dansei Yuuki (blutorange)
on 2016-04-02 00:57
It freezes an object, preventing further modification, see
http://ruby-doc.org/core-2.3.0/Object.html#method-i-freeze

But all the example is supposed to illustrate is that freeze is a method
that does something to the object and freeze? checks whether it has been
done to the object.

You get the object 5 outside the method and freeze it, then you call the
method, passing the object and check if it has been frozen. In this
respect, even numbers behave like passed-by-reference.
96bb15ec886c45a93e0f32bb0b2e69c8?d=identicon&s=25 Kevin Abel (bklabel1)
on 2016-09-01 17:38
Dansei,
I was looking back on my questions.  I forgot to thank you for your time
to explain this.
I have been away from Ruby for a while.  I think that I need to take the
intro classes again.  I forgot too much of the basics.
Thanks,
Kevin
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2016-09-03 03:48
If you write code on your own in a given language, you will be less
likely to forget.
96bb15ec886c45a93e0f32bb0b2e69c8?d=identicon&s=25 Kevin Abel (bklabel1)
on 2016-09-17 00:21
Robert Heiler,
Yes I agree.  I need to write some of my own code in Ruby.
I go back to looking at it and I forget what I just learned.
Thanks,
Kevin
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.