Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating new instance from class // Syntax irritates me

42 views
Skip to first unread message

Ugur

unread,
May 20, 2013, 5:57:01 PM5/20/13
to
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 'mi...@test.lindsaar.net'
from 'a...@test.lindsaar.net'
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

Simon Krahnke

unread,
May 24, 2013, 12:45:05 AM5/24/13
to
* Ugur <ugur....@gmail.com> (2013-05-20) schrieb:

>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 'mi...@test.lindsaar.net'
> from 'a...@test.lindsaar.net'
> 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 'mi...@test.lindsaar.net'" is a method call on self called "to",
with the argument 'mi...@test.lindsaar.net'.

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

do...@nodally.com

unread,
May 28, 2013, 5:11:30 PM5/28/13
to
As Simon says these are method calls not parameters. This syntax might be easier to follow:

m = Mail.new

m.to('mi...@test.lindsaar.net')
m.from('a...@test.lindsaar.net')
m.subject('testing sendmail')
m.body('testing sendmail')

m.deliver

Cheers,
Doug
0 new messages