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 3320548
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:57:23+00:00 2026-05-17T22:57:23+00:00

I was reading an article on meta-programming and it showed that you can define

  • 0

I was reading an article on meta-programming and it showed that you can define a method within another method. This is something that I had known for a while, but it made me ask myself the question: does this have any practical application? Is there any real life uses of defining a method within a method?

Ex:

def outer_method
  def inner_method
     # ...
  end
  # ...
 end
  • 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-17T22:57:24+00:00Added an answer on May 17, 2026 at 10:57 pm

    My favorite metaprogramming example like this is dynamically building a method that you’re then going to use in a loop. For example, I have a query-engine I wrote in Ruby, and one of its operations is filtering. There are a bunch of different forms of filters (substring, equals, <=, >=, intersections, etc.). The naive approach is like this:

    def process_filter(working_set,filter_type,filter_value)
      working_set.select do |item|
        case filter_spec
          when "substring"
            item.include?(filter_value)
          when "equals"
            item == filter_value
          when "<="
            item <= filter_value
          ...
        end
      end
    end
    

    But if your working sets can get large, you’re doing this big case statement 1000s or 1000000s of times for each operation even though it’s going to take the same branch on every iteration. In my case the logic is much more involved than just a case statement, so the overhead is even worse. Instead, you can do it like this:

    def process_filter(working_set,filter_type,filter_value)
      case filter_spec
        when "substring"
          def do_filter(item,filter_value)
            item.include?(filter_value)
          end
        when "equals"
          def do_filter(item,filter_value)
            item == filter_value
          end
        when "<="
          def do_filter(item,filter_value)
            item <= filter_value
          end
        ...
      end
      working_set.select {|item| do_filter(item,filter_value)}
    end
    

    Now the one-time branching is done once, up front, and the resulting single-purpose function is the one used in the inner loop.

    In fact, my real example does three levels of this, as there are variations in the interpretation of both the working set and the filter value, not just the form of the actual test. So I build an item-prep function and a filter-value-prep function, and then build a do_filter function that uses those.

    (And I actually use lambdas, not defs.)

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

Sidebar

Related Questions

Reading this article http://support.microsoft.com/kb/813878 I have a question: Where can I get ipseccmd.exe for
After reading this article on thedailywtf.com, I'm not sure that I really got the
Reading this article I found several ways to call a method. Method to call:
While reading this article, I got a doubt. I understood that while trasferring small
I'm reading this article that compares XML to JSON, and in the comments section,
After reading this article I made a point that int () yields 0 because
I was reading this article about relative time calculation The problem is that the
After reading this article, it makes sense to rebase to gather changes from the
After reading this article I don't have a clear answer: http://palizine.plynt.com/issues/2010Oct/bypass-xss-filters/ Will browsers interpret
After reading this article http://lukast.mediablog.sk/log/?p=155 I decided to use mingw on linux to compile

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.