0

Good day.

Now I'm creating questions for Ruby education course. I divide them in 3 stages "simple","normal", "advanced". I know if define one method within another in Ruby it will acts as like as I defined 2 method. But I do not know how to explain this behavior.

def one
    p "one"
    def two
        p "two"
    end
end

one # "one"
two # "two"

Please help me with explanation what's going on? I going to put this question in "advanced" part.

Thank you.

UPD: educations course - list of questions with variants of answers I had during my way in Ruby. I'm relatively new in Ruby, and I preserve list of questions I been confused, and list of questions I been surprised.

Dmitry Dmitriev
  • 929
  • 8
  • 23
  • 1
    @mudasobwa perfect is the enemy to done. If I do not know something, I ask. – Dmitry Dmitriev May 01 '18 at 09:10
  • 1
    @mudasobwa it is not bad, it's answers on questions that rises for newcomer in Ruby, while I remember all there difficulties I met. Now I remember all my path, and queue of questions I had when I came from PHP/JS. – Dmitry Dmitriev May 01 '18 at 09:19
  • @mudasobwa to avoid potential harm of misconception in these questions (aprox 20) let me send them to you for checking, when I finish. The more articles exists, the more people use Ruby. And I like this language a lot. – Dmitry Dmitriev May 01 '18 at 09:24
  • 3
    This is an interesting question. I do not see the problem in not knowing something, then researching it to understand it before submitting it onto a course syllabus. Good for you and best of luck. – Sagar Pandya May 01 '18 at 11:12
  • 2
    @SagarPandya thank you for a kind words. – Dmitry Dmitriev May 01 '18 at 11:21
  • 1
    The answers to the 'duplicate' question don't answer one of your main questions: Why does ruby behave this way? I voted to re-open this question, but for now a quick summary: All methods are defined on an object in ruby. If you are not explicitly enclosing your method def in a class, then the object is `main`, an instance of `Class`. Methods dynamically defined within other methods are attached to whatever object scope you are in when the containing method is executed. Everything is an object in ruby, and methods are ways for those objects to receive messages. They can't stand alone. – Carl Zulauf May 01 '18 at 11:45
  • @CarlZulauf thank you for the clear and comprehensive explanation. – Dmitry Dmitriev May 01 '18 at 11:54

0 Answers0