GSUB! The argument does not work

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

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

I have puts match to make sure some of the characters have been removed. name is a variable passed to the method in which this is its purpose. I get 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 that calls this method. How can i fix this?

EDIT: If I remove! in gsub !, it passes, but characters are not deleted.

+4
source share
4 answers

Short answer

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

Long answer

The problem you are facing is that gsub! the call returns zero. You cannot concatenate (+) a string with zero.

This is because you have the wrong Regexp. You do not avoid special regular expression characters such as $, * and., Just for starters. Also, as of now, gsub will only match if your string contains all of these characters in sequence. You must use the pipe (|) operator to perform an OR type operation.

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

Example:

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

Your line is fixed:

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

As the designated J -_- L, you can also use the character class ([]), which makes it a little clearer, I think. Well, anyway, it's hard to mentally disassemble.

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

But since what you are doing is a simple character swap, the best method is tr (again reminded J -_- L!):

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

Easier to read and make changes.

+5
source

I don’t see that block for just do

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

or use [] to indicate OR

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

you need to avoid special characters and then OR so that they delete them separately, not just if they were all found together

also no need to use gsub! to change the string used, using the non- gsub() mutator, since you assign it to a new variable,

gsub! returns nil for which the + operator is not defined for bites, giving you an unspecified method error

+3
source
  • You cannot apply the second parameter and block for gsub (block is ignored)
  • Regularly wrong, you forgot the square brackets: /[.?\/ !@ \#{}$%^&*() ~] / `
  • Since your regular expression is incorrect, it did not match anything, and therefore GSUB! returns zero, if nothing was replaced, you will get this strange zero no method error
  • btw: you should use gsub not gsub! in this case, because you are using the return value (and not the name itself) - and there would be no error.
+3
source

It seems that the object "name" is zero, you can call gsub! to zero, which usually complains about NoMethodError: private method gusb! called for nilNilClass, since I do not know the ruby ​​version that you are using. I'm not sure if the error will be the same, but this is a good place to start looking.

+2
source

All Articles