Defining nested Method in single class

A

Arun Kumar

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

Robert Klemme

Hi All,

=A0Today 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:
=3D=3D=3D=3D=3D
I created a class in class it showing Error like "class definition in
method body", If its is means, why not in methods ?

Code:
=3D=3D=3D=3D=3D

class AdavanceConfuse
=A0def AdavanceConfuse.Class_method1
=A0 =A0puts "this is class_method1"
=A0 =A0def AdavanceConfuse.class_method1_attribute_class_method
=A0 =A0 =A0puts "this is class_method1_attribute_class_method"
=A0 =A0end
=A0 =A0class_method2
=A0end

=A0def AdavanceConfuse.class_method2
=A0 =A0#~ class TestClass
=A0 =A0 =A0puts "this is class_method2"
=A0 =A0 =A0def Instance_method1
=A0 =A0 =A0 =A0puts "this is instance_method1"
=A0 =A0 =A0end
=A0 =A0#~ end
=A0end
end

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

Output:
=3D=3D=3D=3D=3D=3D=3D
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

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

Arun Kumar

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

Robert Klemme

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
=3D> #<#<Class:0x10154094>:0x10153d14>
irb(main):007:0> foo.tap {|cl| p cl; cl.new.bar}
#<Class:0x1011f140>
=3D> #<Class:0x1011f140>
irb(main):008:0> foo.tap {|cl| p cl; cl.new.bar}
#<Class:0x10021d3c>
=3D> # said:
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.
=A0"""""
=A0 =A0 =A0 =A0def AdavanceConfuse.Class_method1
=A0 =A0puts "this is class_method1"
=A0 =A0def AdavanceConfuse.class_method1_attribute_class_method
=A0 =A0 =A0puts "this is class_method1_attribute_class_method"
=A0 =A0end
=A0end
""""""

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

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

Arun Kumar

Hi robert,

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

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top