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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:03:10+00:00 2026-05-24T17:03:10+00:00

There is a common idiom of using substitutions like: def with clazz, &block yield

  • 0

There is a common idiom of using substitutions like:

def with clazz, &block
  yield clazz
  clazz
end

with Hash.new |hash|
  hash.merge!{:a => 1}
end

Is there a way to go further and define #with to have a possibility of doing:

with Hash.new |hash|
  merge!{:a => 1} 
end

or even:

with Hash.new do
  merge!{:a => 1}
end

?


UPDATE

Later accidentally I found exactly what I was looking for (solution similar to the accepted one):
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/19153

UPDATE 2

It was added to sugar-high/dsl in https://github.com/kristianmandrup/sugar-high

UPDATE 3

docille project on Github exploits this idea very nicely.

  • 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-24T17:03:12+00:00Added an answer on May 24, 2026 at 5:03 pm

    If you are referring to the way in which Rails does routing then I think you need to do something like this

    def with(instance, &block)
      instance.instance_eval(&block)
      instance
    end
    
    with(Hash.new) do
      merge!({:a => 1})
      merge!({:b => 1})
    end
    

    This is how I can see it being done in the Rails source anyway start by looking at the draw method in action_pack/lib/action_dispatch/routing/route_set

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

Sidebar

Related Questions

I am pretty sure there is a common idiom, but I couldn't find it
in Java the following is a common idiom: if( null != obj && obj.getSomeNumber()
In Rails, the common idiom for page-specific Javascript is: (layout) <head> <% yield :javascript
Is there a common Javascript/Coffeescript-specific idiom I can use to accomplish this? Mainly out
The iPhone platform has a number of common gesture idioms. For example, there are
Is there a common conversion function to convert a integer type object to a
Are there any Common Lisp implementations for .Net?
Is there a common way to get the current time in or with milliseconds?
Is there a common way to express the usage of arguments in C++? I
Is there a common standard for reporting errors in your class functions? I've seen

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.