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

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 T.
[email protected]
082 415 8283
skype: andrewtimberlake

“I have never let my schooling interfere with my education.”
–Mark Twain

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 [email protected] 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.

On Jan 8, 2008 8:15 PM, Sur [email protected] wrote:

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

And it works fine with any hierarchy.

try it further, like

[email protected]:~$ 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 :slight_smile:

kind regards -botp

@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 10:06 PM, Sur [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs