[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

statement outside of any method in a class

Paul Smith

1/11/2007 4:01:00 AM

Newbie question:

In the following code:

class Person < ActiveRecord::Base
validates_presence_of :first_name
end

validates_presence_of is a method call that is
specified outside of any method in class Person.

Could someone explain what the invocation model is
for that statement that is not bound to any method
within a class (i.e. when is that statement executed?)

Thanks.

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

13 Answers

dblack

1/11/2007 4:22:00 AM

0

hemant

1/11/2007 4:31:00 AM

0

On Thu, 2007-01-11 at 13:01 +0900, Paul Smith wrote:
> Newbie question:
>
> In the following code:
>
> class Person < ActiveRecord::Base
> validates_presence_of :first_name
> end
>
> validates_presence_of is a method call that is
> specified outside of any method in class Person.
>

Hmm validates_presence_of method is not "outside any method" in class
Person actually. Most probably (i.e taking into account rails meta
magic), validates_presence_of is a class method, which is defined in
class ActiveRecord::Base.

So, when Person class inherits ActiveRecord::Base class it also inherits
method validates_presence_of and above mentioned line invokes the method
with argument :first_name.

class Foobar
def self.sayfoo arg
puts "You said #{arg}"
end
end

class Baz < Foobar
sayfoo :rubyrocks
end

Output #=> "You said rubyrocks"


Unlike C++, where you can have class or instance methods of a class(i.e
static or normal methods of class) defined outside class definition, in
Ruby its always inside class.





Paul Smith

1/11/2007 4:36:00 AM

0

Thanks for the explanation David.

However, it is still not clear to me when these statements are invoked.

In your example, what triggers the execution of the 2 statements?

> class C < SomeClass
> puts "self right now is #{self}." # self right now is C.
> do_something # Hi!
> end

When the class is loaded, when an instance of that class is created?
Is this similar to a static initialization block in Java?

P.

>> Could someone explain what the invocation model is
>> for that statement that is not bound to any method
>> within a class (i.e. when is that statement executed?)
>
> It's the same, in the abstract, as the invocation model generally: a
> "bareword"-style method is automatically invoked on the current
> default object, self. At the outer level of a class definition, self
> is the class object itself. So what you're seeing is a call to a
> class method.
>
> Here's a little X-ray of what's going on:
>
> class SomeClass
> def self.do_something
> puts "Hi!"
> end
> end
>
> class C < SomeClass
> puts "self right now is #{self}." # self right now is C.
> do_something # Hi!
> end
>
> There are more nuances but that's the basic scenario.
>
>
> David


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

Paul Smith

1/11/2007 4:43:00 AM

0

Thanks Hemant. However, as I mentioned in my last reply, it is not clear
to me what triggers the execution of a statement such as "sayfoo
:rubyrocks" in your example. When the class is loaded, when an instance
is created, ???

Thanks.

P.

Hemant Kumar wrote:
> On Thu, 2007-01-11 at 13:01 +0900, Paul Smith wrote:
>>
> Hmm validates_presence_of method is not "outside any method" in class
> Person actually. Most probably (i.e taking into account rails meta
> magic), validates_presence_of is a class method, which is defined in
> class ActiveRecord::Base.
>
> So, when Person class inherits ActiveRecord::Base class it also inherits
> method validates_presence_of and above mentioned line invokes the method
> with argument :first_name.
>
> class Foobar
> def self.sayfoo arg
> puts "You said #{arg}"
> end
> end
>
> class Baz < Foobar
> sayfoo :rubyrocks
> end
>
> Output #=> "You said rubyrocks"
>
>
> Unlike C++, where you can have class or instance methods of a class(i.e
> static or normal methods of class) defined outside class definition, in
> Ruby its always inside class.


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

hemant

1/11/2007 5:35:00 AM

0

On Thu, 2007-01-11 at 13:42 +0900, Paul Smith wrote:
> Thanks Hemant. However, as I mentioned in my last reply, it is not clear
> to me what triggers the execution of a statement such as "sayfoo
> :rubyrocks" in your example. When the class is loaded, when an instance
> is created, ???
>
> Thanks.

On a broader perspective, we can say when the class is loaded, when you
define a class like this in your code:

class Baz < Foobar
sayfoo :rubyrocks
end

An object called Baz starts to exist in current Runtime, which is an
instance of class Class.

So, when method sayfoo gets invoked when object Baz is created or in
other words, we can say when class is loaded.





Gavin Kistner

1/11/2007 6:13:00 AM

0

Paul Smith wrote:
> Thanks for the explanation David.
>
> However, it is still not clear to me when these statements are invoked.
>
> In your example, what triggers the execution of the 2 statements?
[snip]
> When the class is loaded, when an instance of that class is created?
> Is this similar to a static initialization block in Java?

Instead of asking and waiting for an answer, perhaps just try running
the following code:

puts "Line 1"

class Foo
puts "Inside Foo on line 4"
def bar
puts "Running #bar method (line 6)"
end
puts "All done with #{self} (line 8)"
end

puts "Line 11"

f = Foo.new
f.bar

puts "Last line (16)"

Gavin Kistner

1/11/2007 6:16:00 AM

0

Phrogz wrote:
> class Foo
> puts "Inside Foo on line 4"
> def bar
> puts "Running #bar method (line 6)"
> end
> puts "All done with #{self} (line 8)"
> end

Even more enlightening, add two lines:

class Foo
puts "Inside Foo on line 4"
p self.instance_methods(false)
def bar
puts "Running #bar method (line 7)"
end
p self.instance_methods(false)
puts "All done with #{self} (line 10)"
end

Alex Gutteridge

1/11/2007 6:17:00 AM

0

On 11 Jan 2007, at 13:36, Paul Smith wrote:

> Thanks for the explanation David.
>
> However, it is still not clear to me when these statements are
> invoked.
>
> In your example, what triggers the execution of the 2 statements?
>
>> class C < SomeClass
>> puts "self right now is #{self}." # self right now is C.
>> do_something # Hi!
>> end
>
> When the class is loaded, when an instance of that class is created?
> Is this similar to a static initialization block in Java?
>
> P.

Hemant may have already answered your question, but as an aside the
easiest way to check these things in Ruby is to use irb to test:

irb(main):001:0> class A
irb(main):002:1> def self.doit
irb(main):003:2> puts "hi"
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> class B < A
irb(main):007:1> puts "self: #{self}"
irb(main):008:1> doit
irb(main):009:1> end
self: B
hi
=> nil
irb(main):010:0> A.new
=> #<A:0x340fe4>
irb(main):011:0> B.new
=> #<B:0x33aab8>

Does that make it clearer? doit only gets run when the B class is
defined, not when an instance is created.

Alex Gutteridge

Bioinformatics Center
Kyoto University



The Peeler

10/28/2012 9:02:00 PM

0

On Sun, 28 Oct 2012 13:50:41 -0700 (PDT), A Moose in Love with Nazi Scum
wrote:

>>>from such sources as Iranian, Turkic, Caucasian, Slavic, Latin, and
>>>German.
>>>}
>>
>> When does it intend to give them back?
>
> i don't know. we have also borrowed words from Hungarian. i.e.
> marmalade = leckvar, watermelon = deenya

Doesn't matter! His point really was whether you would like to engage with
him in another Nazi circle jerk, now that Dumb Heini has gone to bed! <G>

Tony Beltram

10/28/2012 9:41:00 PM

0

On Sun, 28 Oct 2012 13:50:41 -0700 (PDT), A Moose in Love
<parkstreetbooboo@gmail.com> wrote:

>On Oct 28, 4:36?pm, The Revd <peel...@degenerate.Grik> wrote:
>> On Sun, 28 Oct 2012 13:25:46 -0700 (PDT), A Moose in Love
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> <parkstreetboo...@gmail.com> wrote:
>> >i've read that it has ties with a few different siberian languages.
>> >http://www.progenealogists.com/hungary/la...
>> >{
>> >The Hungarian language is truly a language unto itself, and like no
>> >other. Contrary to initial expectations, the language is not Slavic-
>> >based, nor is it related to that of the Croats or Serbs. It has no
>> >connection with German. The closest connection to Hungarian is
>> >probably a language that is least expected?Finnish. Hungarian actually
>> >belongs to a category referred to as Finno-Ugrian, which includes
>> >Estonian and a handful of smaller, rarely-used languages. It also has
>> >distant ties to Turkic, Mongolian and some minor Asian languages.
>> >Despite this categorization, Hungarian fails to resemble any of these
>> >languages to a measurable degree.
>>
>> >Hungarian is a logical language, utilizing diacritics to assist the
>> >pronunciation and even the emphasis of different syllables in words.
>> >Nothing is left to wonder or confusion. The word is written exactly as
>> >it is pronounced, and vice versa. The diacritics even control voice
>> >inflection. The diacritic symbols follow their respective standard
>> >vowels in the alphabetical lineup.
>> >}
>>
>> >{
>> >Hungarian language, Hungarian Magyar, ?member of the Finno-Ugric group
>> >of the Uralic language family, spoken primarily in Hungary but also in
>> >Slovakia, Romania, and Yugoslavia, as well as in scattered groups
>> >elsewhere in the world. Hungarian belongs to the Ugric branch of Finno-
>> >Ugric, along with the Ob-Ugric languages, Mansi and Khanty, spoken in
>> >western Siberia.
>>
>> >The language has been written in a modified Latin alphabet since the
>> >13th century ad, and its orthography was stabilized from the 16th
>> >century with the introduction of printing. Characteristic of Hungarian
>> >orthography are the acute accent (? ) marking long vowels?doubled in
>> >the case of long front rounded vowels (? )?and special representations
>> >for sibilant sounds (e.g., sz corresponds to English s, but s
>> >corresponds to English sh).
>>
>> >Surrounded by non-Uralic languages, Hungarian has borrowed many words
>> >from such sources as Iranian, Turkic, Caucasian, Slavic, Latin, and
>> >German.
>> >}
>>
>> When does it intend to give them back?
>
>i don't know. we have also borrowed words from Hungarian. i.e.
>marmalade = leckvar, watermelon = deenya

Neither leckvar or deenya are in the English language, though.