Hm, I should’ve added that I didn’t want to involve ‘eval’. But then why
not, I’ll just need to check the input using a simple regexp. After all,
almost everything in Ruby (require, load, irb, class_eval) is using eval
anyway…
Hm, I should’ve added that I didn’t want to involve ‘eval’. But then why
not, I’ll just need to check the input using a simple regexp. After all,
almost everything in Ruby (require, load, irb, class_eval) is using eval
anyway…
“D” == David Garamond lists@zara.6.isreserved.com writes:
Hm, I should’ve added that I didn’t want to involve ‘eval’. But then why
not, I’ll just need to check the input using a simple regexp.
well, if you think that a simple regexp can do anything against the evil
eval, then you have perhaps a P background
Don’t you also have a P background? (Or Matz? Or many others?)
Anyway, I didn’t say regexp can make eval safe generally, but only for
my case:
def class_defined?(classname)
classname.kind_of? String or
raise ArgumentError, “please give me string”
classname =~ /\A[A-Z][A-Za-z0-9_](::[A-Z][A-Za-z0-9_])*\z/ or
raise ArgumentError, “invalid class name”
eval("defined? " + classname) != nil
end
“D” == David Garamond lists@zara.6.isreserved.com writes:
Hm, I should’ve added that I didn’t want to involve ‘eval’. But then
why
not, I’ll just need to check the input using a simple regexp.
well, if you think that a simple regexp can do anything against the
evil
eval, then you have perhaps a P background
Don’t you also have a P background? (Or Matz? Or many others?)
Anyway, I didn’t say regexp can make eval safe generally, but only for
my case:
def class_defined?(classname)
classname.kind_of? String or
raise ArgumentError, “please give me string”
classname =~ /\A[A-Z][A-Za-z0-9_](::[A-Z][A-Za-z0-9_])*\z/ or
raise ArgumentError, “invalid class name”
eval("defined? " + classname) != nil
You don’t need to compare with nil, that’s converting something to a boolean
that is essentially a boolean already - unless, of course, if you want to
hide the outcome of “defined?”…
To make the methods semantic match the name you should do
eval("defined? " + classname) && Class === eval(classname)
Because otherwise you will get true for all sorts of constants. (Try with
“IO::CREAT”)