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

S

Sur

[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!
 
A

Andrew Timberlake

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
(e-mail address removed)
082 415 8283
skype: andrewtimberlake

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


-----Original Message-----
From: Sur [mailto:[email protected]]
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!
 
B

botp

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
 
S

Sur

[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,

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!
 
B

botp

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top