Jan 24 2008
∞
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…

