The following code gets me wondering: how come the last “puts” gets
printed without initializing on the Foo class at all…
…
class Foo
def initialize
end
puts “How does this get printed without Foo.new???”
end
When you define a class, all the stuff at the top level of the
definition is executed. Methods and variables get defined and
attached to the class, and other stuff just gets executed. That
includes this puts call. If you only wanted it executed when Foo.new
gets called, it needs to be inside initialize.
-----Messaggio originale-----
Da: Nik Z. [mailto:[email protected]]
Inviato: gioved 10 novembre 2011 22:15
A: ruby-talk ML
Oggetto: Re: How Am I to Understand This Class Behavior?
Now making it worse
I meant to say: I WASN’t expecting a return value during class
definition
time.
–
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?mid978&d)-12
-----Messaggio originale-----
Da: Nik Z. [mailto:[email protected]]
Inviato: gioved 10 novembre 2011 21:40
A: ruby-talk ML
Oggetto: How Am I to Understand This Class Behavior?
Hi, Gurus,
The following code gets me wondering: how come the last “puts” gets
printed without initializing on the Foo class at all…
I would appreciate it if anyone can explain this to me. Thank you in
advance !!!
Nik
===== Code as Below =======
#/usr/bin/env ruby
puts “print as expected”
def bar
puts “Hello, World!”
end
class Foo
def initialize
end
puts "How does this get printed without Foo.new???"
–
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?mid977&d)-12
-----Messaggio originale-----
Da: Luca (Email) [mailto:[email protected]]
Inviato: gioved 29 dicembre 2011 07:58
A: ruby-talk ML
Oggetto: I: How Am I to Understand This Class Behavior?
–
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