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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:29:13+00:00 2026-05-22T17:29:13+00:00

Both Ruby and Vim use g with substitution commands to mean all occurrences. What

  • 0

Both Ruby and Vim use “g” with substitution commands to mean “all occurrences.” What does the “g” stand for?

Specifically, in Ruby, the String class has two “sub” commands: sub will replace only the first occurrence, and gsub will replace all occurrences. For example:

string = "One potato, two potato, three potato, four."
string.sub('potato','banana') # => "One banana, two potato, three potato, four."
string.gsub('potato','banana') # => "One banana, two banana, three banana, four."

Similarly, in Vim, :%s/foo/bar will look through the whole file (which is what % means) and substitute one occurrence per line, but :%s/foo/bar/g will do all occurrences on each line.

My guess would be that in both cases, “g” means “greedy,” because both the Ruby commands and the Vim command accept a regular expression, but my understanding of greedy matching is “match the longest possible substring meeting these criteria,” not “match as many substrings as possible.” (See “Watch Out for The Greediness!“)

  • 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-22T17:29:13+00:00Added an answer on May 22, 2026 at 5:29 pm

    If I remember correctly it means global, which this Wiki page seems to confirm:

    The g flag means global – each
    occurrence in the line is changed,
    rather than just the first.

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

Sidebar

Related Questions

I use both ruby on rails and Java. I really enjoy using migrations when
In Ruby (1.8.X) Why does Object both inherit off Kernel and include it? Wouldn't
I've a Ruby regexp question. if string == /(^\d{1,3})/ # this matches both 24
Does anyone have any insights regarding compiling Ruby code for Windows? I've tried both
Both of these plugins require Vim to be compiled with Ruby support, need to
Both Ruby and Python have the ability of calling the debugger from code (
New to both Ruby and Rails but I'm book educated by now (which apparently
Given that both Ruby on Rails (RoR) and Grails can run on the JVM
I've heard both MacRuby and Ruby CocoaBridge discussed in relation to iPhone/iPad development in
Why do this Ruby object both a to_s and inspect methods that appear to

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.