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

  • Home
  • SEARCH
  • 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 4608916
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:54:45+00:00 2026-05-22T00:54:45+00:00

I am making a function that turns the first argument into a PHP var

  • 0

I am making a function that turns the first argument into a PHP var (useless, I know), and set it equal to the second argument. I’m trying to gsub! it to get rid of all the characters that can’t be used in a PHP var. Here is what I have:

dvar = "$" + name.gsub!(/.?\/!@\#{}$%^&*()`~/, "") { |match| puts match }

I have the puts match there to make sure some of the characters were removed. name is a variable passed into a method in which this is its purpose. I am getting this error:

TypeError: can't convert nil into String
cVar at ./Web.rb:31
(root) at C:\Users\Andrew\Documents\NetBeansProjects\Web\lib\main.rb:13

Web.rb is the file this line is in, and main.rb is the file calling this method. How can I fix this?

EDIT: If I remove the ! in gsub!, it goes through, but the characters aren’t removed.

  • 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-22T00:54:46+00:00Added an answer on May 22, 2026 at 12:54 am

    Short answer

    Use dvar = "$" + name.tr(".?\/!@\#{}$%^&*()``~", '')

    Long answer

    The problem you are facing is that the gsub! call is returning nil. You can’t concatenate (+) a String with a nil.

    That’s happening because you have a malformed Regexp. You aren’t escaping the special regex symbols, like $, * and ., just for a start. Also, the way it is now, gsub will only match if your string contains all that symbols in sequence. You should use the pipe (|) operator to make an OR like operation.

    gsub! will also return nil if no substitutions happened.
    See the documentation for gsub and gsub! here: http://ruby-doc.org/core/classes/String.html#M001186
    I think you should replace gsub! with gsub. Do you really need name to change?

    Example:

    name = "m$var.name$$"
    dvar = "$" + name.gsub!(/\$|\.|\*/, "") # $ or . or *
    # dvar now contains $mvarname and name is mvarname
    

    Your line, corrected:

    dvar = "$" + name.gsub(/\.|\?|\/|\!|\@|\\|\#|\{|\}|\$|\%|\^|\&|\*|\(|\)|\`|\~/, "")
    # some things shouldn't (or aren't needed to) be escaped, I don't remember them all right now
    

    As J-_-L appointed, you could also use a character class ([]), that makes it a little clearer, I guess. Well, it’s hard to mentally parse anyway.

    dvar = "$" + name.gsub(/[\.\?\/\!\@\\\#\{\}\$\%\^\&\*\(\)\`\~]/, "")
    

    But because what you are doing is simple character replacement, the best method is tr (again reminded by J-_-L!):

    dvar = "$" + name.tr(".?\/!@\#{}$%^&*()`~", '')
    

    Way easier to read and make modifications.

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

Sidebar

Related Questions

I am making a function that turns a list of words into an array
When making a function call in Linux (or OS X for that matter), can
I am trying to create a function with a variant record-type parameter that allows
I'm making an AJAX call to a page that returns XML. It turns out
How would I go about making a function so that x has a range
I am making a Tic Tac Toe game and i created a function that
Im making firefox extension. One function stores value in every tab, to use it
i am making an upload with Ajaxupload plugin and i am using this function
I have the following code making a GET request on a URL: $('#searchButton').click(function() {
Hay guys, I'm making a simple preload function (function($) { $.fn.preload = function(settings) {

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.