10 May 2012 06:20
Re: passing via instance variable or regular ()
Josh Cheek <josh.cheek <at> gmail.com>
2012-05-10 04:20:45 GMT
2012-05-10 04:20:45 GMT
On Wed, May 9, 2012 at 9:37 PM, sam jam <lists <at> ruby-forum.com> wrote:
def first
name = "sam"
last(name)
end
def last(first)
name = first + " " + "jam"
end
versus
def first
<at> name = "sam"
last
end
def last
name = <at> name + " " + "jam"
end
I am new to ruby and programming. I see that both of these ways work.
When I was first starting I tended toward the first way, but lately I
have
been tending toward the second way. Does it make much difference?
edit: saying brand n-3-w means my post contains spam? lol...okay first
starting then.
Hi, Sam, good question :)
Unfortunately the answer is the ever-nebulous "it depends" :P
Within an object, the second way is generally better, because objects should generally be small and operate on a shared set of instance variables.
Outside an object (e.g., in this case these methods are declared on main -- which is technically an object, but its purpose isn't to interact with your system so much as to give you an environment to wire your stuff together and bootstrap your app in), the former is usually better.
I'd like to go further into this, but it's a bit difficult as the names of the methods don't accurately reflect what they do (these both return full names, last is just a helper that first calls in order to concatenate the names)
RSS Feed