Creating new instance from class // Syntax irritates me

U

Ugur

Hello there,

I am totally new to Ruby and saw this syntax which is creating a new instance from a class?

(quote)
mail = Mail.new do
to '(e-mail address removed)'
from '(e-mail address removed)'
subject 'testing sendmail'
body 'testing sendmail'
end

mail.deliver
(/quote)
This example is taken from [1]

The tokens "to, from, subject, body" are whitespace seperated from the values on the right hand side. But no comma or arrow in between.

Can anyone explain this syntax to me? I checked [2] and couldn't find anything similir.

Thanks for any help,
Ugur

[1] http://lindsaar.net/2010/3/15/how_to_use_mail_and_actionmailer_3_with_gmail_smtp
[2] http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html
 
S

Simon Krahnke

* Ugur said:
Hello there,

I am totally new to Ruby and saw this syntax which is creating a new instance from a class?

(quote)
mail = Mail.new do
to '(e-mail address removed)'
from '(e-mail address removed)'
subject 'testing sendmail'
body 'testing sendmail'
end

Well, the mail is simply created by Mail.new, the do ... end part is a
block given to the constructor that it uses to intialize some fields in
the mail.

"to '(e-mail address removed)'" is a method call on self called "to",
with the argument '(e-mail address removed)'.

The trick to achieve self to be set to something other than the value it
had during definition seems to be done using #instance_eval.

mfg, simon .... l
 
D

dougs

Hello there,



I am totally new to Ruby and saw this syntax which is creating a new instance from a class?



(quote)

mail = Mail.new do

to '(e-mail address removed)'

from '(e-mail address removed)'

subject 'testing sendmail'

body 'testing sendmail'

end



mail.deliver

(/quote)

This example is taken from [1]



The tokens "to, from, subject, body" are whitespace seperated from the values on the right hand side. But no comma or arrow in between.



Can anyone explain this syntax to me? I checked [2] and couldn't find anything similir.



Thanks for any help,

Ugur



[1] http://lindsaar.net/2010/3/15/how_to_use_mail_and_actionmailer_3_with_gmail_smtp

[2] http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html

As Simon says these are method calls not parameters. This syntax might be easier to follow:

m = Mail.new

m.to('(e-mail address removed)')
m.from('(e-mail address removed)')
m.subject('testing sendmail')
m.body('testing sendmail')

m.deliver

Cheers,
Doug
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top