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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:17:53+00:00 2026-05-22T19:17:53+00:00

For clarification, here’s the exact question in the about_regular_expressions.rb file that I’m having trouble

  • 0

For clarification, here’s the exact question in the about_regular_expressions.rb file that I’m having trouble with:

def test_sub_is_like_find_and_replace
    assert_equal __, "one two-three".sub(/(t\w*)/) { $1[0, 1] }
end

I know what the answer is to this, but I don’t understand what’s happening to get that answer. I’m pretty new to Ruby and to regex, and in particular I’m confused about the code between the braces and how that’s coming into play.

  • 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-22T19:17:54+00:00Added an answer on May 22, 2026 at 7:17 pm

    The code inside the braces is a block that sub uses to replace the match:

    In the block form […] The value returned by the block will be substituted for the match on each call.

    The block receives the match as an argument but the usual regex variables ($1, $2, …) are also available.

    In this specific case, the $1 inside the block is "two" and the array notation extracts the first character of $1 (which is "t" in this case). So, the block returns "t" and sub replaces the "two" in the original string with just "t".

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

Sidebar

Related Questions

Quick question/clarification required here. I have a DB table that will quite possibly have
Clarification/summary for the question -- we're looking for: a hosted bug tracking system, that
I have a very naive conceptual question here for clarification. In my app I'm
UPDATED FOR CLARIFICATION I posted a similar question a while back found here which
I am having clarification regarding using Hadoop for large file size around 2 million.
OK n00b here with SOAP, Would like some clarification on how to use SOAP.
Just need some quick clarification I have 2 Queries in my Access Database that
I need some clarification. I have a Reportwriter dll that uses Crystal Reports. It
This is a clarification question I've got from a Java EE 5 migration. I'm
I have a file name abc.txt . On that file have some lines like

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.