Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have Methods inside Methods?

Tags:

methods

ruby

I have a method inside of a method. The interior method depends on a variable loop that is being run. Is that a bad idea?

like image 354
Trip Avatar asked Feb 01 '11 15:02

Trip


People also ask

Can I call a method inside another method?

Similarly another method which is Method2() is being defined with 'public' access specifier and 'void' as return type and inside that Method2() the Method1() is called. Hence, this program shows that a method can be called within another method as both of them belong to the same class.

Can there be nested methods?

JAVA does not support “directly” nested methods, but you can still achieve nested method functionality in Java 7 or older version by defining local classes, class within method so that it will compile. In java 8 and newer version you achieve it by lambda expression.

How do you call a method inside a method in Java?

Call a Method Inside main , call the myMethod() method: public class Main { static void myMethod() { System.out.println("I just got executed!"); } public static void main(String[] args) { myMethod(); } } // Outputs "I just got executed!"


2 Answers

UPDATE: Since this answer seems to have gotten some interest lately, I wanted to point out that there is discussion on the Ruby issue tracker to remove the feature discussed here, namely to forbid having method definitions inside a method body.


No, Ruby doesn't have nested methods.

You can do something like this:

class Test1   def meth1     def meth2       puts "Yay"     end     meth2   end end  Test1.new.meth1 

But that is not a nested method. I repeat: Ruby does not have nested methods.

What this is, is a dynamic method definition. When you run meth1, the body of meth1 will be executed. The body just happens to define a method named meth2, which is why after running meth1 once, you can call meth2.

But where is meth2 defined? Well, it's obviously not defined as a nested method, since there are no nested methods in Ruby. It's defined as an instance method of Test1:

Test1.new.meth2 # Yay 

Also, it will obviously be redefined every time you run meth1:

Test1.new.meth1 # Yay  Test1.new.meth1 # test1.rb:3: warning: method redefined; discarding old meth2 # test1.rb:3: warning: previous definition of meth2 was here # Yay 

In short: no, Ruby does not support nested methods.

Note also that in Ruby, method bodies cannot be closures, only block bodies can. This pretty much eliminates the major use case for nested methods, since even if Ruby supported nested methods, you couldn't use the outer method's variables in the nested method.


UPDATE CONTINUED: at a later stage, then, this syntax might be re-used for adding nested methods to Ruby, which would behave the way I described: they would be scoped to their containing method, i.e. invisible and inaccessible outside of their containing method body. And possibly, they would have access to their containing method's lexical scope. However, if you read the discussion I linked above, you can observe that matz is heavily against nested methods (but still for removing nested method definitions).

like image 85
Jörg W Mittag Avatar answered Oct 11 '22 15:10

Jörg W Mittag


Actually it's possible. You can use procs/lambda for this.

def test(value)   inner = ->() {     value * value   }   inner.call() end 
like image 29
SuperManEver Avatar answered Oct 11 '22 16:10

SuperManEver