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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:00:10+00:00 2026-05-23T10:00:10+00:00

I recently came across the Ruby EOB / -EOB construct within this context (from

  • 0

I recently came across the Ruby EOB / -EOB construct within this context (from the Ruby id3 library) :

def initialize(...)
  # ...

  instance_eval <<-EOB
    class << self

      def parse
        # ...
        # Method code
        # ...
      end   
  EOB 
  self.parse # now we're using the just defined parsing routine

  # ...
end

I understand that the code is used to generate a method on the fly, yet I would like to know if it would be possible to use the EOB snippet within a method. I would like to write a method which generates some other method code , which is to be included in yet another class. This sounds a bit confusing, I’ll try to illustrate my intention with some simplified code samples :

# This class reads the code of another 
# Ruby class and injects some methods
class ReadAndInject

  # The method which defines another method
  def get_code_to_be_injected
    "\tdef self.foo\n"+
    "\t\tputs 'bar'\n"+
    "\tend\n"
  end

  # Main entry point, reads a generated Ruby Class
  # and injects specific methods within it
  def read_and_inject

    # Assume placeholder for currently read line,
    # add the generated code within
    current_line += "\n#{get_code_to_be_injected}"
  end

end # class ReadAndInject

This would work, since the method to be injected is added correctly. Yet I was wondering if using the EOB construct would yield some advantages (e.g. better visibility of the code, since no cumbersome tabs or string concatenations would have to be added.

To conclude, is this a good use case for EOB ?
It seems like a shady yet powerful construct, I’ve ducked it, googled and stackoverflow’d it yet no significant code samples other than one from RubyCocoa were returned. I’ve only recently started to use meta constructs in Ruby, so please be gentle 🙂

Thanks in advance!

  • 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-23T10:00:11+00:00Added an answer on May 23, 2026 at 10:00 am

    These are called “here documents”, which are supported by several languages, and allow you to make a multi-line string. You can actually use any delimiter, not just EOB. Ruby has some extra features for heredocs: for example, the - in <<-EOB allows you to indent the delimiter.

    You might use it like this:

    def code_to_be_injected
      <<-EOS
        def self.foo
          puts 'bar'
        end
      EOS
    end
    

    Some additional features in Ruby:

    myvar = 42
    <<EOS
    variable: #{myvar}
    EOS #=> "variable: 42"
    
    <<'EOS'
    variable: #{myvar}
    EOS #=> "variable: #{myvar}"
    
    print <<A, <<B
    This will appear first
    A
    and this second
    B
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently came across this piece of SQL: SELECT members.id FROM members, members_to_groups WHERE
I recently came across this in some code - basically someone trying to create
I came across this recently, up until now I have been happily overriding the
I recently came across this blog post Yet another post about gamma correction which
I recently came across this error in IE9 that says :- The maximum number
I recently came across this code example in Mercury: append(X,Y,Z) :- X == [],
I recently came across this post on Forrst: http://forr.st/~Nbp and thought it'd be a
I recently came across an IE7 only bug that I thought I'd share so
I recently came across a ASP 1.1 web application that put a whole heap
I recently came across the data structure known as a skip list . It

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.