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???"

end

http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html

Check out the ‘Class and Module Definitions’ section.

-Jingjing

On Nov 10, 2011, at 12:39 PM, Nik Z. wrote:

def initialize
end

puts “How does this get printed without Foo.new???”
end

class definitions are expressions where the returned value is the last
statement evaluated. See this irb session for example:

result = class TestClass
“This is a test”
end
=> “This is a test”

puts result
This is a test

As the last statement was “This is a test”, it becomes the return of the
class definition expression.

  • Chris (twitter: @cwgem)

Thanks, DuanJingJing and Chris for the speedy reply.

Evidently I was expecting a return value during class definition time.

My bad :slight_smile:

Now making it worse :slight_smile:

I meant to say: I WASN’t expecting a return value during class
definition time.

and thanks Dave !!!

On Thu, Nov 10, 2011 at 15:39, Nik Z. [email protected] wrote:

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.

-Dave

-----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 :slight_smile:

I meant to say: I WASN’t expecting a return value during class
definition
time.

and thanks Dave !!!


Posted via http://www.ruby-forum.com/.


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???"

end


Posted via http://www.ruby-forum.com/.


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

Sponsor:
Conto Arancio al 4,20%. Soldi sempre disponibili, zero spese, aprilo in
due minuti!
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid920&d)-12