PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Defining a function inside a function. Whats this feature ? How touse inside a class ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Defining a function inside a function. Whats this feature ? How touse inside a class ?

Réponse
 
LinkBack Outils de la discussion
Vieux 08/01/2008, 12h15   #1
Sur
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Defining a function inside a function. Whats this feature ? How touse inside a class ?

[Note: parts of this message were removed to make it a legal post.]

Hi Everyone,

I am not sure if I have seen it being used earlier or probably I don't know
how to use it.

when I define function(s) inside a function within the main object, it
works...

def person
def author
p "I am author"
end
def reader
p "I am reader"
end
end

Now, if I call

person.author # => I am author
person.reader # => I am reader

And it works fine with any hierarchy.
The same thing doesn't seem to work inside a class.

Any explanation ?

Thanks!

--
sur
"is a String object" is a String object
hacking over objects
http://expressica.com

  Réponse avec citation
Vieux 08/01/2008, 12h39   #2
Andrew Timberlake
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Defining a function inside a function. Whats this feature ? Howto use inside a class ?

Sur

Two things come to mind, are you wanting class methods or instance methods?
With the first one, class methods, you don't have to create an instance of
Person (I don't think this is what you want) and you simply call the methods
directly from the class
With the second, instance methods, you need to create an instance of the
Person using Person.new and then call the methods on the object.

#Class methods:
class Person
def Person.author
p "I am author"
end
def Person.reader
p "I am reader"
end
end
Person.author # -> I am author
Person.reader # -> I am reader


#Instance methods:
class Person
def author
p "I am author"
end
def reader
p "I am reader"
end
end
person = Person.new
person.author # -> I am author
person.reader # -> I am reader

Andrew Timberlake
andrew@andrewtimberlake.com
082 415 8283
skype: andrewtimberlake

"I have never let my schooling interfere with my education."
--Mark Twain


-----Original Message-----
From: Sur [mailto:sur.max@gmail.com]
Sent: 08 January 2008 02:16 PM
To: ruby-talk ML
Subject: Defining a function inside a function. Whats this feature ? How to
use inside a class ?

Hi Everyone,

I am not sure if I have seen it being used earlier or probably I don't know
how to use it.

when I define function(s) inside a function within the main object, it
works...

def person
def author
p "I am author"
end
def reader
p "I am reader"
end
end

Now, if I call

person.author # => I am author
person.reader # => I am reader

And it works fine with any hierarchy.
The same thing doesn't seem to work inside a class.

Any explanation ?

Thanks!

--
sur
"is a String object" is a String object
hacking over objects
http://expressica.com


!DSPAM:3,47836aab145829463747748!



  Réponse avec citation
Vieux 08/01/2008, 12h58   #3
botp
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Defining a function inside a function. Whats this feature ? Howto use inside a class ?

On Jan 8, 2008 8:15 PM, Sur <sur.max@gmail.com> wrote:
> person.author # => I am author
> person.reader # => I am reader
>
> And it works fine with any hierarchy.



try it further, like

botp@jedi-hopeful:~$ irb
irb(main):001:0> def person
irb(main):002:1> def author
irb(main):003:2> p "I am author"
irb(main):004:2> end
irb(main):005:1> def reader
irb(main):006:2> p "I am reader"
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> person
=> nil
irb(main):010:0> author
"I am author"
=> nil
irb(main):011:0> nil.reader
"I am reader"
=> nil
...
irb(main):020:0> nil.methods.grep /reader/
=> ["reader"]
irb(main):021:0> 1.methods.grep /reader/
=> ["reader"]
irb(main):022:0> "string".methods.grep /author/
=> ["author"]

>
> The same thing doesn't seem to work inside a class.
>


it does, just try it...

sometimes, i play with this behavior to create a second initializer or
a eureka/latemethod definer

kind regards -botp

  Réponse avec citation
Vieux 08/01/2008, 14h06   #4
Sur
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Defining a function inside a function. Whats this feature ? Howto use inside a class ?

[Note: parts of this message were removed to make it a legal post.]

@Andrew
Hi,

No, I was not actually saying that... In the class I meant this...
class Person
def man
def author
p "I am author"
end
end
end

and if I call Person.new.man.author, it gives NoMethodError.


@Matz
Hello Matz,

>>Don't use def inside of def, in general. The behavior might be
>>changed in the future (2.0 or later).


So, is it not the expected behavior of the "class" and we should not use it
or its a bug that its allowing the def inside a def(specifically only in
main object) and will be fixed in the future release. Although I haven't
read anywhere about Namespaced functions like classes and modules but I was
just curious as it was an unexpected behavior to me.

Thanks!

On Jan 8, 2008 6:14 PM, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:

> Hi,
>
> In message "Re: Defining a function inside a function. Whats this feature
> ? How to use inside a class ?"
> on Tue, 8 Jan 2008 21:15:40 +0900, Sur <sur.max@gmail.com> writes:
>
> |I am not sure if I have seen it being used earlier or probably I don't
> know
> |how to use it.
> |
> |when I define function(s) inside a function within the main object, it
> |works...
>
> Don't use def inside of def, in general. The behavior might be
> changed in the future (2.0 or later).
>
> matz.
>
>



--
sur
"is a String object" is a String object
hacking over objects...

  Réponse avec citation
Vieux 08/01/2008, 14h50   #5
botp
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Defining a function inside a function. Whats this feature ? Howto use inside a class ?

On Jan 8, 2008 10:06 PM, Sur <sur.max@gmail.com> wrote:
> No, I was not actually saying that... In the class I meant this...
> class Person
> def man
> def author
> p "I am author"
> end
> end
> end
>
> and if I call Person.new.man.author, it gives NoMethodError.


you assume that it's a hierarchy, but it's not.

irb(main):009:0> Person.new.man
=> nil
irb(main):010:0> Person.new.man.author
NoMethodError: undefined method `author' for nil:NilClass
from (irb):10
from :0
irb(main):011:0> Person.new.author
"I am author"
=> nil

the chain seems to work on plain defs since by default if you do not
specify a class, ruby creates it in Object class. Thus the methods
defined are Object methods, ergo they become "public" so to speak.
Thus even nil class acquired the reader/author method; thus exhibiting
the seemingly hierachical behaviour (see my prev post). There is no
function as function per se in ruby. There are only methods wc of
course belong to a class.

kind regards -botp

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h39.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14985 seconds with 13 queries