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

  • Home
  • SEARCH
  • 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 982195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:39:31+00:00 2026-05-16T04:39:31+00:00

Sometimes you can see: do_this do available_method1 arg1 available_method2 arg1 end When I use

  • 0

Sometimes you can see:

do_this do
   available_method1 "arg1"
   available_method2 "arg1"
end

When I use the block from do_this method then I get some methods I could use inside that block.

I wonder how this is accomplished? How does the code look like behind the scenes?

I want to be able to provide some methods through a block.

  • 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-16T04:39:31+00:00Added an answer on May 16, 2026 at 4:39 am

    It’s called a Domain-Specific Language (DSL). Here’s (Last archived version) some great info on various forms of Ruby DSL blocks.

    There are really two ways to go about doing this, with different syntaxes:

    do_thing do |thing| # with a block parameter
      thing.foo :bar
      thing.baz :wibble
    end
    
    # versus
    
    do_thing do # with block-specific methods
      foo :bar
      baz :wibble
    end
    

    The first is more explicit and less likely to fail, while the second is more concise.

    The first can be implemented like so, by simply passing a new instance as the block parameter with yield:

    class MyThing
      def self.create
        yield new
      end
    
      def foo(stuff)
        puts "doing foo with #{stuff}"
      end
    end
    
    MyThing.create do |thing|
      thing.foo :bar
    end
    

    And the second, which runs the block in the context of the new object, giving it access to self, instance variables, and methods:

    class MyThing
      def self.create(&block)
        new.instance_eval &block
      end
    
      def foo(stuff)
        puts "doing foo with #{stuff}"
      end
    end
    
    MyThing.create do
      foo :bar
    end
    

    And if you really want to do it without calling MyThing.create, just:

    def create_thing(&block)
      MyThing.create &block
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm plotting on top of an image in Matlab. Sometimes, I can't see what's
I have some problem with MySQL Workbench in that I sometimes can't set foreign
Our mysql processes can sometimes get backlogged and processes begin queuing up. I'd like
Actually my problem is sometimes i can get the location and sometimes i can't
Sometimes, I'll end up having to catch an exception that I know can never
Sometimes I can't understand the simplest things, i'm sure it's in my face, i
I've got an ArrayList that can sometimes contain the values Time (GMT), None, and
I have several large CSS files and making a change can sometimes take a
Feel free to edit the title, engrish can sometimes confuse instead of help. I
Can anybody tell me why my Async task fails sometimes like this? I tested

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.