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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:46:34+00:00 2026-06-09T08:46:34+00:00

I’m trying to figure out how to create a sort of class-less DSL for

  • 0

I’m trying to figure out how to create a sort of “class-less DSL” for my Ruby project, similar to how step definitions are defined in a Cucumber step definition file or routes are defined in a Sinatra application.

For example, I want to have a file where all my DSL functions are being called:

#sample.rb

when_string_matches /hello (.+)/ do |name|
    call_another_method(name)
end

I assume it’s a bad practice to pollute the global (Kernel) namespace with a bunch of methods that are specific to my project. So the methods when_string_matches and call_another_method would be defined in my library and the sample.rb file would somehow be evaluated in the context of my DSL methods.

Update: Here’s an example of how these DSL methods are currently defined:

The DSL methods are defined in a class that is being subclassed (I’d like to find a way to reuse these methods between the simple DSL and the class instances):

module MyMod
  class Action
    def call_another_method(value)
      puts value
    end

    def handle(text)
      # a subclass would be expected to define
      # this method (as an alternative to the 
      # simple DSL approach)
    end
  end
end

Then at some point, during the initialization of my program, I want to parse the sample.rb file and store these actions to be executed later:

module MyMod
  class Parser

    # parse the file, saving the blocks and regular expressions to call later
    def parse_it
      file_contents = File.read('sample.rb')
      instance_eval file_contents
    end

    # doesnt seem like this belongs here, but it won't work if it's not
    def self.when_string_matches(regex, &block)
      MyMod.blocks_for_executing_later << { regex: regex, block: block }
    end
  end
end

# Later...

module MyMod
  class Runner

    def run
      string = 'hello Andrew'
      MyMod.blocks_for_executing_later.each do |action|
        if string =~ action[:regex]
          args = action[:regex].match(string).captures
          action[:block].call(args)
        end
      end
    end

  end
end

The problem with what I have so far (and the various things I’ve tried that I didn’t mention above) is when a block is defined in the file, the instance method is not available (I know that it is in a different class right now). But what I want to do is more like creating an instance and eval’ing in that context rather than eval’ing in the Parser class. But I don’t know how to do this.

I hope that makes sense. Any help, experience, or advice would be appreciated.

  • 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-06-09T08:46:35+00:00Added an answer on June 9, 2026 at 8:46 am

    It’s a bit challenging to give you a pat answer on how to do what you are asking to do. I’d recommend that you take a look at the book Eloquent Ruby because there are a couple chapters in there dealing with DSLs which would probably be valuable to you. You did ask for some info on how these other libraries do what they do, so I can briefly try to give you an overview.

    Sinatra

    If you look into the sinatra code sinatra/main.rb you’ll see that it extends Sinatra::Delegator into the main line of code. Delegator is pretty interesting..

    It sets up all the methods that it wants to delegate

    delegate :get, :patch, :put, :post, :delete, :head, :options, :template, :layout,
             :before, :after, :error, :not_found, :configure, :set, :mime_type,
             :enable, :disable, :use, :development?, :test?, :production?,
             :helpers, :settings
    

    and sets up the class to delegate to as a class variable so that it can be overridden if needed..

    self.target = Application
    

    And the delegate method nicely allows you to override these methods by using respond_to? or it calls out to the target class if the method is not defined..

    def self.delegate(*methods)
      methods.each do |method_name|
        define_method(method_name) do |*args, &block|
          return super(*args, &block) if respond_to? method_name
          Delegator.target.send(method_name, *args, &block)
        end
        private method_name
      end
    end
    

    Cucumber

    Cucumber uses the treetop language library. It’s a powerful (and complex—i.e. non-trivial to learn) tool for building DSLs. If you anticipate your DSL growing a lot then you might want to invest in learning to use this ‘big gun’. It’s far too much to describe here.

    HAML

    You didn’t ask about HAML, but it’s just another DSL that is implemented ‘manually’, i.e. it doesn’t use treetop. Basically (gross oversimplification here) it reads the haml file and processes each line with a case statement…

    def process_line(text, index)
      @index = index + 1
    
      case text[0]
      when DIV_CLASS; push div(text)
      when DIV_ID
        return push plain(text) if text[1] == ?{
        push div(text)
      when ELEMENT; push tag(text)
      when COMMENT; push comment(text[1..-1].strip)
      ...
    

    I think it used to call out to methods directly, but now it’s preprocessing the file and pushing the commands into a stack of sorts. e.g. the plain method

    FYI the definition of the constants looks like this..

    # Designates an XHTML/XML element.
    ELEMENT         = ?%
    # Designates a `<div>` element with the given class.
    DIV_CLASS       = ?.
    # Designates a `<div>` element with the given id.
    DIV_ID          = ?#
    # Designates an XHTML/XML comment.
    COMMENT         = ?/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.