From: Josh Cheek Date: 2012-05-10T13:20:45+09:00 Subject: Re: passing via instance variable or regular () --e89a8f13ec6650536e04bfa6f0d5 Content-Type: text/plain; charset=ISO-8859-1 On Wed, May 9, 2012 at 9:37 PM, sam jam wrote: > def first > name = "sam" > last(name) > end > > def last(first) > name = first + " " + "jam" > end > > versus > > def first > @name = "sam" > last > end > > def last > name = @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) --e89a8f13ec6650536e04bfa6f0d5 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Wed, May 9, 2012 at 9:37 PM, sam jam <lists@ruby-forum.com> wrote:
def first
=A0 =A0name =3D "sam"
=A0 =A0last(name)
=A0end

=A0 def last(first)
=A0 =A0 name =3D first + " " + "jam"
=A0 end

versus

=A0def first
=A0 =A0@name =3D "sam"
=A0 =A0last
=A0end

=A0 def last
=A0 =A0 name =3D @name + " " + "jam"
=A0 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 questi= on :)

Unfortunately the answer is the ever-nebulou= s "it depends" :P

Within an object, the = second way is generally better, because objects should generally be small a= nd 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= 9;t to interact with your system so much as to give you an environment to w= ire your stuff together and bootstrap your app in), the former is usually b= etter.

I'd like to go further into this, but it's a bi= t difficult as the names of the methods don't accurately reflect what t= hey do (these both return full names, last is just a helper that first call= s in order to concatenate the names)
--e89a8f13ec6650536e04bfa6f0d5--