Open Synapse

Hi, I'm Abhay Kumar. I like to throw up interesting things I encounter here. You can also see the result of my mustache pact with my coworkers, check out what I'm up to or see the few photos I take. I like to receive email and instant messages (AIM / Jabber), as well. Some links of interest: Calais Text Tagger, Powerset

Jan 24
Permalink

Ruby memory leak: regular expressions in class methods

A few people were taking about this snippet of code today:

module Foo
  def class_name
    name.split(/::/)
  end
end

class Bar; extend Foo; end

loop { Bar.class_name }

It leaks like a old faucet. For some reason this snippet doesn’t:

module Foo
  def class_name
    x = /::/
    name.split(x)
  end
end

class Bar; extend Foo; end

loop { Bar.class_name }

It seems that it’s better to define a regex outside of a split, gsub or other similar method in a class method. I wonder if Rails does anything like this…
Comments (View)
blog comments powered by Disqus

Change CongressVote Obama