So, I’m trying my hand at metaprogramming and am finding this the result
of executing my code with -w enabled:
warning: class variable access from toplevel singleton method
(This is spammed ad infinitum anywhere I use the overridden gets, but
it’s always the same message and always the same cause: gets.)
The code it’s pointing to looks like this:
def override_methods(io_in)
puts io_in
class << io_in
alias :old_gets :gets
@@override_buffer=[]
def gets separator = $/
raise ArgumentError, “cannot use non-standard
separator” unless separator == $/
unless line = @@override_buffer.shift
return old_gets
else
return line
end
end
def find_end
for a in 1..20
line = self.old_gets
if line == "__END__\n"
@@override_buffer = []
break
else
@@override_buffer << line
end
end
self
end
end
io_in
end
def find_block_begin io_in
io_in = override_methods(io_in)
io_in.find_end
end
To me the culprit is likely that @@override_buffer thing, but I can’t
for the life of me figure out how to get around it. My modified gets
needs access to that (shared) buffer that the added find_end method
generates while peeking ahead. @override_buffer and override_buffer
don’t work (the latter for reasons obvious to me, the former … not so
obvious).
Now, the output of the code is exactly what I expect it to be, so this
isn’t a show-stopper. I am, however, in the habit of making my code -w
clean if it’s at all possible. How would I do it with that snippet?