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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:41:25+00:00 2026-05-20T08:41:25+00:00

I am new to ruby and I just come across my first tricky problem.

  • 0

I am new to ruby and I just come across my first tricky problem. I am trying to redefine some methods of String in order to attach some functionality around them. The problematic method appears to be gsub. (EDIT) Let me paste my main.rb that showcases this error.

require 'rubygems' if RUBY_VERSION < "1.9"
require 'sinatra'

class String
    alias_method :old_gsub, :gsub
    def gsub (*params, &block )
        old_gsub *params, &block
    end
end

get '/' do
  s="Hello world! "
end

This is my starting point and I expect this to work just as the original String does. Unfotunately, with this redefinition in place, some existing code breaks.

As you can see in this script, I try to load Sinatra and serve a page. When I do this and request the index, the web server fails with the following output in the console.

127.0.0.1 – – [25/Feb/2011 17:56:26] “GET / HTTP/1.1” 200 13 0.0012
[2011-02-25 17:56:26] ERROR NoMethodError: undefined method `upcase’ for nil:NilClass
/usr/local/ruby/lib/ruby/1.9.1/webrick/httpresponse.rb:172:in `block (2 levels) in send_header’
/data/Dropbox/Ruby/RubyTrack/lib/main.rb:227:in `gsub’
/data/Dropbox/Ruby/RubyTrack/lib/main.rb:227:in `gsub’
/usr/local/ruby/lib/ruby/1.9.1/webrick/httpresponse.rb:172:in `block in send_header’
+plus more stuff in the trace

[2011-02-25 17:56:26] ERROR NoMethodError: undefined method `[]’ for nil:NilClass
/usr/local/ruby/lib/ruby/1.9.1/webrick/accesslog.rb:52:in `block in format’
/data/Dropbox/Ruby/RubyTrack/lib/main.rb:227:in `gsub’
/data/Dropbox/Ruby/RubyTrack/lib/main.rb:227:in `gsub’
/usr/local/ruby/lib/ruby/1.9.1/webrick/accesslog.rb:50:in `format’
+more stuff in the trace

The first exception (httpresponse.rb:172) is on the line

tmp = key.gsub(/\bwww|^te$|\b\w/){ $&.upcase }

and the second error is caused by the result of (accesslog.rb:50)

format_string.gsub(/\%(?:\{(.*?)\})?>?([a-zA-Z%])/)

From the above, it appers that I am using Ruby 1.9.1 (even though ruby -v gives me 1.9.2p0). I’m on Ubuntu 10.04. It is true that there may be something wrong in my configuration of Ruby, installing it in Ubuntu was a bit messy.

So, to sum up, why does my redefinition of gsub behave differenlty than the original gsub?

  • 1 1 Answer
  • 2 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-20T08:41:25+00:00Added an answer on May 20, 2026 at 8:41 am

    MRI Ruby’s internal C code implementation of gsub does some magic to set up $&, $1, and so on in the calling method. When you redefine gsub, they end up getting set in your redefinition, rather than the caller of your redefinition.

    I don’t know a way around this in MRI. In Rubinius, enough of the internals are implemented in Ruby and made accessible to user-level code that it is possible. This is something Yehuda Katz touches on in this post.

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

Sidebar

Related Questions

I'm new to ruby and it could be some basic stuff, but it just
I'd just started toying around with Ruby on Rails and had come across an
I'm currently finishing up testing a new Ruby on Rails app. Just recently, some
I am still new to Ruby and basically just writing my first micro-program after
I'm new to Ruby on Rails (I know Ruby just decently though) and looking
I just installed ruby on rails on windows. install mysql and created a new
I just upgraded to Snow Leopard, including installing the new XCode, re-compiled Ruby 1.8
I am new to ruby. I've just installed Ruby and Rails on my fresh
Not sure why I'm getting this, but I just installed RVM, the new Ruby
I have just optimised some Ruby code that was in a controller method, replacing

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.