Ruby, Pass by value or pass by reference?

I wish I knew for sure…

The only reason I’m writing this is I got bitten while writing some code for Sentinel I was expecting a value based on the fact I thought I was passing by value so the original object would remain unaffected.

Needless to say it was actually passing by reference, although if you read some articles it suggests that it actually only passes by value and those values just so happen to be object ID’s which point to objects… It get’s very complicated.

In an earlier post I talked about my programming ability, and I have written not to long ago about starting to code in Ruby So to be clear, I am not a programmer, I never will be, this stuff that i’m writing about now is what I’d class as ground breaking for me, it’s probably a really simple concept, but for some reason it has been really difficult to get my head around I struggled in C with it, more so in Java, and now Ruby; the difference? this time I actually went off did some reading and got some friends involved in a rant (both programmers who understood it all instantly even though they’d never seen ruby before, Annoying)

My quest started as most do, on Google where I came across this I’d highly recommend reading this page and the following two links that appear on that page One, two a combination of those and more reading and more talking I think I finally got my head around it.

Where did it go wrong?

Laziness is probably the answer here, I didn’t bother to read anything about it I just found a tutorial and went with it, got my head around the basics and started to dive in; which is fine as long as you don’t mind spending your evenings like I did, reading lots and coding.

I started out on an assumption that unless I specified otherwise it would be pass by value, mistake 1. In fairness it wasn’t a big mistake but annoying none the less. The other mistake I made was just not doing any research into how the langage worked, I could have spent a little time looking. So just now I did that a 2 min google and i came across this which would have saved me a little time, Granted I should have followed this straight after following Steve Litt’s it would have saved me a lot of hassle.

There is but one more thing I would have done differently, I would have read about the class I was about to use… Instead I was following examples and basically shooting in the dark and hoping it would get better, I wouldn’t do it with Sysadmin, so why do it with programming?

Just those few things would have made a big difference, not only to my understanding of Ruby but also would have ultimately saved me coding a lot of return’s that were not needed, so now I have to re-write some Sentinel code and make better use of methods I didn’t know existed. Well, at least it keeps me busy and I’m still learning new things.

Summary

I realise in the middle of this I digressed a little, but The links above are all very useful, especial those for Sentinel! If you are looking to get into Ruby I’d suggest the tips and links in “Where did it go wrong?” It would have saved me time and effor so no doubt it will save you time and effort; likewise if you were looking for the really techy explanation of Ruby pass by value or reference re-read “I wish I knew for sure…” all the information is there. I could try my best to explain it but that would be morally wrong, who knows what tripe could be produced based on a beginners understanding of a core piece of functionality.