Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Defining a function inside a function. Whats this feature ? How touse inside a class ?

Reply
Thread Tools

Defining a function inside a function. Whats this feature ? How touse inside a class ?

 
 
Sur
Guest
Posts: n/a
 
      01-08-2008
[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

 
Reply With Quote
 
 
 
 
Andrew Timberlake
Guest
Posts: n/a
 
      01-08-2008
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
082 415 8283
skype: andrewtimberlake

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


-----Original Message-----
From: Sur [(E-Mail Removed)]
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!



 
Reply With Quote
 
 
 
 
botp
Guest
Posts: n/a
 
      01-08-2008
On Jan 8, 2008 8:15 PM, Sur <(E-Mail Removed)> 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

 
Reply With Quote
 
Sur
Guest
Posts: n/a
 
      01-08-2008
[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 <(E-Mail Removed)> 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 <(E-Mail Removed)> 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...

 
Reply With Quote
 
botp
Guest
Posts: n/a
 
      01-08-2008
On Jan 8, 2008 10:06 PM, Sur <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Class variable inside block of defining instance method Mikkel Kroman Ruby 4 02-08-2010 01:38 PM
Class.class and class.getClass, whats the difference seyal.v.p@gmail.com Java 2 05-17-2006 08:34 AM
What're the benift and the drawback of defining a class embedded inside another class. Xiangliang Meng C++ 5 04-12-2005 09:07 AM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM



Advertisments