Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7665647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:37:31+00:00 2026-05-31T14:37:31+00:00

class MyClass def method_missing(name, *args) name = name.to_s 10.times do number = rand(100) end

  • 0
class MyClass
  def method_missing(name, *args)
    name = name.to_s
    10.times do
      number = rand(100)
    end
    puts "#{number} and #{name}"
  end  
end

Hello, I am exercising ruby but in this nonrecursive function i am getting stack level too deep error when use this piece of code.

x = MyClass.New
x.try
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:37:34+00:00Added an answer on May 31, 2026 at 2:37 pm

    The problem with your code is the number variable defined inside times() falls out of method_missing() scope. Thus, when that line is executed Ruby interprets it as a method call on self.

    In normal cases you should be getting NoMethodError exception. However, since you have overriden method_missing() method for MyClass, you do not get this exception. Instead until the stack overflows method number() is called.

    To avoid such problems,try to specify the method names that are allowed. For instance, lets say you only need try, test, and my_method methods to be called on MyClass, then specify these method names on method_missing() to avoid such problems.

    As an example :

    class MyClass
      def method_missing(name, *args)
        name = name.to_s
        super unless ['try', 'test', 'my_method'].include? name
        number = 0
        10.times do
          number = rand(100)
        end
        puts "#{number} and #{name}"
      end  
    end
    

    If you do not really need method_missing(), avoid using it. There are some good alternatives here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

module MyModule def my_method; 'hello'; end end class MyClass class << self include MyModule
The following code works: class MyClass def method_a method_b end private def method_b puts
I have a __new__ method as follows: class MyClass(object): def __new__(cls, *args): new_args =
The following code: class MyClass(): def test(self): self.__x = 0 def __setattr__(self, name, value):
I have a ruby file as follows: module Example class Myclass def t_st Hello
I have a class: class MyClass(object): @property def myproperty(self): return 'hello' Using mox and
Consider this example: class MyClass: def func(self, name): self.name = name I know that
I have the following Ruby code: module MyModule class MyClass def self.my_method end end
consider the following code: class MyClass(object): def __init__(self): self.data_a = np.array(range(100)) self.data_b = np.array(range(100,200))
I have the following code: class MyClass def method foo = MyClass.all end end

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.