pulling random numbers from an array or hash

Napoleon

Veteran
Veteran
Joined
Dec 29, 2012
Messages
869
Reaction score
97
First Language
Dutch
Primarily Uses
b = a does not copy anything other than a reference if a is an array or hash. You do not end up with 2 arrays! What b = a does is simply giving you 2 variables that are EXACTLY the same object/class instance. See it as 2 roads that both lead to Rome. If you destroy Rome via the left road then Rome will also be destroyed when you reach it using the right road. But if you use clone() instead then when you take the right road Rome would still be intact. Because it's a copy and the copy was not destroyed.

# Rome is some class with a reference to a neighborhood x.#------------------------------------------------------------------------a = Rome.new()b = aa.destroytravel_to(a) # omg Rome is gone!travel_to( # omg Rome is gone!travel_to(a.neighborhood_x) # omg the neighborhood is also gone!#------------------------------------------------------------------------a = Rome.new()b = a.clonea.destroytravel_to(a) # omg Rome is gone!travel_to( # Welcome to Rome!travel_to(b.neighborhood_x) # omg but the neighborhood is gone! # (not deep cloned)But yeah you still get your references in b after using b = a but keep in mind that this is not a 'copy' nor a 'clone' so even if you alter a reference from b, the reference in a will change as well because they are all the same.

Deep cloning (yes there are several cloning methods) however would also copy all references. But I don't think this functionality is build into Ruby by default but I'm not sure.

You might want to google "pass by reference" and "pass by value" and "cloning". You will run into problems sooner or later when scripting a lot if you don't, and finding the error is near impossible if you don't know this then. In Ruby most variables however are passed by reference.
 
Last edited by a moderator:

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
I really wonder why they actually did pass by reference as default opposed to pass by value...
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
Last edited by a moderator:

Napoleon

Veteran
Veteran
Joined
Dec 29, 2012
Messages
869
Reaction score
97
First Language
Dutch
Primarily Uses
Performance and 'hiding' the fact that Ruby uses pointers indeed would be the first 2 that would come to my mind as well. I remember in XNA (which prefers pass by value most of the time) passing so many of my variables as a pointer manually. The way Ruby handles this is so much better for games. But it all depends in on your platform. For generic Windows applications "pass by value" by default has my personal preference and it has usually 0 performance impact as well in these cases.
 
Last edited by a moderator:

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
I see... Maybe I just didn't realized that it's actually the default for most languages I've written on... coz now that I think of it, yeah they pass by reference too... 
 

vindaca

CEO of Tales of the Valiant
Veteran
Joined
Dec 11, 2012
Messages
534
Reaction score
75
First Language
English
Primarily Uses
Thank you very much for clarifying that for me Napoleon.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

People3_5 and People3_8 added!

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.

Forum statistics

Threads
105,868
Messages
1,017,083
Members
137,583
Latest member
write2dgray
Top