Defining nested Method in single class

Hi All,

Today morning i exploring the ruby 1.8.7, i found myself some what
clear so
that i worked some basic code concepts (class and methods) and i
satisfied, Again I build a
code to confuse myself. Initially it show some error, finally i made
some
modification on the code,and it works fine, but i thought it needs to
show error, can anyone please explain how it works ?

Note:

I created a class with in class it showing Error like “class definition
in
method body”, If its is means, why not in methods ?

Code:

class AdavanceConfuse
def AdavanceConfuse.Class_method1
puts “this is class_method1”
def AdavanceConfuse.class_method1_attribute_class_method
puts “this is class_method1_attribute_class_method”
end
class_method2
end

def AdavanceConfuse.class_method2
#~ class TestClass
puts “this is class_method2”
def Instance_method1
puts “this is instance_method1”
end
#~ end
end
end

AdavanceConfuse.Class_method1
AdavanceConfuse.class_method1_attribute_class_method
AdavanceConfuse.class_method2
AdavanceConfuse.new.Instance_method1

Output:

this is class_method1
this is class_method2
this is class_method1_attribute_class_method
this is class_method2
this is instance_method1

Forgive me if my question is stupid ?..

Thanks in Advance,
Arun.

On Thu, Oct 28, 2010 at 8:57 AM, Arun K. [email protected]
wrote:

puts “this is class_method1”
puts “this is instance_method1”
Output:

this is class_method1
this is class_method2
this is class_method1_attribute_class_method
this is class_method2
this is instance_method1

What error did you expect to see from this - and why?

Kind regards

robert

Hi robert,

Thanks for the quick reply…

I created a class with in class it showing Error like “class definition
in method body”,
“”""""""""“If its is means, why not in methods ?”""""""""""

  1. May be my question is wrong ? can u please tell me how to call method
    defined within method of class ?
    “”"""
    def AdavanceConfuse.Class_method1
    puts “this is class_method1”
    def AdavanceConfuse.class_method1_attribute_class_method
    puts “this is class_method1_attribute_class_method”
    end
    end
    “”""""

How to call the class_method1_attribute_class_method in the above code
and how it internally ,the request will be handled in ruby ?

  1. How i can access a class’ method defined with in class ?

Example:

class AdavanceConfuse
class AnotherClass
def AnotherClass.method1
puts “Class with in class’s class method”
end
end
end

How to call the method “method1”,how it internally ,the request will be
handled in ruby ?

Thanks
Jak.

On Thu, Oct 28, 2010 at 9:28 AM, Arun K. [email protected]
wrote:

I created a class with in class it showing Error like “class definition
in method body”,
“”“”“”“”“”“If its is means, why not in methods ?”“”“”“”“”“”

That’s just the way it is: you can define nested methods in a method.
The feature isn’t too useful though.

If you want to define a class in a method (beware, it happens every
time the method is called!) you can do

def foo
Class.new do
def bar; 123; end
end
end

irb(main):006:0> foo.new
=> #<#Class:0x10154094:0x10153d14>
irb(main):007:0> foo.tap {|cl| p cl; cl.new.bar}
#Class:0x1011f140
=> #Class:0x1011f140
irb(main):008:0> foo.tap {|cl| p cl; cl.new.bar}
#Class:0x10021d3c
=> #Class:0x10021d3c

  1. May be my question is wrong ? can u please tell me how to call method
    defined within method of class ?

You know it already, don’t you? Please look at your code.

and how it internally ,the request will be handled in ruby ?
It’s just defined and called like any other class method. The only
difference with nested definitions is the point in time when the
method is created (i.e. not when the interpreter executes the class
definition but when someone calls the method containing the
definition).

  1. How i can access a class’ method defined with in class ?

See above.

Cheers

robert

Hi robert,

Thanks for spending your valuable time, now am clear with this
concept…

“Is there any best book for understanding the ruby in depth ?”