Noob question regarding custom method

Hello,

I am still learning ruby. My question is, often we see things like this:

def set_miles(miles)
@miles = miles
end

Here, we’re defining a custom method set_miles , then passing it an
argument (what’s the reason of doing so?), then we’re setting the
instance variable @miles to a local variable miles, why’s that for? I
know I can use attr_accessor but things like this occurs in various
place. What’s the point of doing this?

I know I sound funny but i can’t understand. Any help would be much much
much appreciated.

“setter” methods that have “set” in the name are far more idiomatic of
Java. Ruby has a different way of doing this:

def miles=(new_miles)
@miles = new_miles
end

Which I know wasn’t the question, I just felt like mentioning this first
:slight_smile:

You’re right, you could use attr_accessor (or reader or writer,
whichever suits the purpose best). And in this case, you probably
would. There’s no reason not to. But some setter methods require, for
example, some kind of validation.

Say if you didn’t want the @miles variable to be less than 0, you could
do this:

def miles=(new_miles)
if new_miles < 0
raise Exception, “Miles cannot be less than 0.”
else
@miles = new_miles
end
end

Is this making sense? :slight_smile:

On 14 November 2011 11:51, Junayeed Ahnaf N.

On Mon, Nov 14, 2011 at 1:29 PM, Sam R. [email protected] wrote:

whichever suits the purpose best). And in this case, you probably
end
end

Is this making sense? :slight_smile:

Just a stylistic remark: I would code it like this

def miles=(new_miles)
if new_miles < 0
raise Exception, “Miles cannot be less than 0.”
end

@miles = new_miles
end

or even

def miles=(new_miles)
raise Exception, “Miles cannot be less than 0.” if new_miles < 0

@miles = new_miles
end

or

def miles=(new_miles)
new_miles < 0 and raise Exception, “Miles cannot be less than 0.”

@miles = new_miles
end

Especially with the first variant it is immediately clear what the
main course of action is. The exception interrupts the regular flow
anyway so there is really no reason for the “else” branch.

Kind regards

robert

On 11/14/2011 06:57 PM, Robert K. wrote:

You’re right, you could use attr_accessor (or reader or writer,
@miles = new_miles

anyway so there is really no reason for the “else” branch.

Kind regards

robert

Thank you robert and sam for answering the question . It helped.

-----Messaggio originale-----
Da: Junayeed Ahnaf N. [mailto:[email protected]]
Inviato: luned 14 novembre 2011 14:17
A: ruby-talk ML
Oggetto: Re: Noob question regarding custom method

On 11/14/2011 06:57 PM, Robert K. wrote:

You’re right, you could use attr_accessor (or reader or writer,
whichever suits the purpose best). And in this case, you probably
would. There’s no reason not to. But some setter methods require, for
example, some kind of validation.

Say if you didn’t want the @miles variable to be less than 0, you could
do this:
Just a stylistic remark: I would code it like this

Thank you robert and sam for answering the question . It helped.


Junayeed Ahnaf N.
/Doing this and that/
http://twitter.com/nirjhor


Caselle da 1GB, trasmetti allegati fino a 3GB e in piu’ IMAP, POP3 e
SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Capodanno al parco Oltremare Riccione: Pacchetto hotel 3 stelle in
centro + ingresso al parco.

-----Messaggio originale-----
Da: Junayeed Ahnaf N. [mailto:[email protected]]
Inviato: luned 14 novembre 2011 12:52
A: ruby-talk ML
Oggetto: Noob question regarding custom method

Hello,

I am still learning ruby. My question is, often we see things like this:

def set_miles(miles)
@miles = miles
end

Here, we’re defining a custom method set_miles , then passing it an
argument
(what’s the reason of doing so?), then we’re setting the instance
variable
@miles to a local variable miles, why’s that for? I know I can use
attr_accessor but things like this occurs in various place. What’s the
point
of doing this?

I know I sound funny but i can’t understand. Any help would be much much
much appreciated.

Junayeed Ahnaf N.
/Doing this and that/
http://twitter.com/nirjhor


Caselle da 1GB, trasmetti allegati fino a 3GB e in piu’ IMAP, POP3 e
SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Capodanno a Riccione, Pacchetto Relax: Mezza Pensione + bagno turco +
solarium + massaggio. Wifi e parcheggio gratis. 2 giorni euro 199 a
persona
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid979&d)-12

Please unsubscribe from this list.

Regards,
Bharath