Topic: Why is gsub returning nil?

FIrst of i'm using gsub and not gsub! .

Here's my code:

  def ultraviolet(text)
    text.gsub(/\[code( lang="(.+?)")?\](.+?)\[\/code\]/m) do
      coder=HTMLEntities.new
      Uv.parse(coder.decode($3).gsub(/\<br \/\>/,''), "xhtml" , $2, true, "sunburst")
    end
  end

coder.decode($3).gsub(/\<br \/\>/,'') evaluates to nil. But coder.decode($3) has <br /> tags which I have confirmed using

"#{coder.decode($3}"

Moreover the weird part is that gsub is returning nil?? If nothing is replaced then it should return the original string.

Regards,

Utsav

Re: Why is gsub returning nil?

If your question is about gsub then you should simplify it massively by just telling us what the value of "coder.decode($3)" is.  That is what gsub is being called on.  all the rest of the (to me, mysterious) stuff in your code isn't relevant.

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: Why is gsub returning nil?

You are calling $2 after another gsub which you shouldn't do. Instead, you should assign $2 to a variable so you can use it after doing another gsub.

Even though you don't have groups in your second gsub the $2 still gets replaced by nil.

Last edited by Kentor (2010-06-23 05:56:59)

Re: Why is gsub returning nil?

Thanks Kentor! It worked

I didn't have groups in the second one so that was weird!

Last edited by utsavgupta (2010-06-23 05:28:04)