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 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.

--
Posted via http://www.ruby-forum.com/.

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

Kind regards

robert

···

On Thu, Oct 28, 2010 at 8:57 AM, Arun Kumar <jakheart001@gmail.com> wrote:

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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 ?

2. 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.

--
Posted via http://www.ruby-forum.com/.

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.

"""""
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 ?

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).

2. How i can access a class' method defined with in class ?

See above.

Cheers

robert

···

On Thu, Oct 28, 2010 at 9:28 AM, Arun Kumar <jakheart001@gmail.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Hi robert,

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

···

--
Posted via http://www.ruby-forum.com/.