R
rantingrickjohnson
Hello folks.
I was hacking away today when i made a stupid mistake that revealed a subtle bug in ruby. Here is a test suite that will expose the problem. (be sure to supply a valid folder path on your machine)
## START CODE ##
def bug
path = 'USE A VALID FOLDER PATH HERE!'
d = Dir.new(path)
files = []
folders = []
d.entries.each do |x|
$stdout.write("--x=#{x}\n")
if x == '.' or x == '..'
next
xpath = File.join(path, x) #<-- ERROR!
elsif File.ftype(xpath)=="directory"
folders.push(xpath)
elsif File.ftype(xpath)=="file"
files.push(xpath)
end
end
$stdout.write("files=#{files}\n")
$stdout.write("folders=#{folders}\n")
end
## END CODE ##
## START RESULT ##
Error: #<TypeError: path/file.rb:14:in `ftype': can't convert nil into String>
## END RESULT ##
As you can see i accidentally pasted the line "xpath = File.join(path, x)" INSIDE the conditional. However, for some reason Ruby committed the following three crimes:
############################################################
# Warning #
############################################################
# In the following paragraphs the author has used #
# emotional language to describe his mental state whilst #
# suffering the affects from a subtle bug. Please read #
# this section objectively and do not get offended by #
# these words. The author does not intend to offend any #
# member of this fine community or the even the language #
# designer himself. The author simply wants to understand #
# what the heck is going on! Thank you. #
############################################################
1. Ruby did not throw a SyntaxError:
I guess some might argue that the programmer should be responsible breakingsyntax, and i agree, HOWEVER, syntax error can and will happen and catching this type of error at the syntactical level BEFORE it has time to metastasis is just good language design.
2. Ruby re-assigned the value of a variable, OR, failed to interpret the value correctly and simply defaulted to nil.
I am amazed that Ruby would overlook the broken control structure, okay, maybe this is a feature of the language that i was unaware of-- if so i can accept that--but why then did Ruby go on to add insult to injury by haphazardly assigning "nil" to my variable `xpath`. What possible good could come of that?
3. Ruby did not throw a NameError:
Finally, but worst of all, (and a direct result of re-assignment); Ruby didnot throw a NameError when i tried to access xpath. xpath should never have had existed. I could "somewhat" understand allowing the syntactical mistake and just ignoring xpath, then, throwing a NameError if i tried to access`xpath`. This would make sense at least. But what i uncovered makes no sense. It's madness.
Conclusion: Ruby had three chances to catch my mistake and notify me of that mistake but ruby failed all three times. Can someone please explain the philosophy behind this design?
"Errors should never pass silently!"
I was hacking away today when i made a stupid mistake that revealed a subtle bug in ruby. Here is a test suite that will expose the problem. (be sure to supply a valid folder path on your machine)
## START CODE ##
def bug
path = 'USE A VALID FOLDER PATH HERE!'
d = Dir.new(path)
files = []
folders = []
d.entries.each do |x|
$stdout.write("--x=#{x}\n")
if x == '.' or x == '..'
next
xpath = File.join(path, x) #<-- ERROR!
elsif File.ftype(xpath)=="directory"
folders.push(xpath)
elsif File.ftype(xpath)=="file"
files.push(xpath)
end
end
$stdout.write("files=#{files}\n")
$stdout.write("folders=#{folders}\n")
end
## END CODE ##
## START RESULT ##
Error: #<TypeError: path/file.rb:14:in `ftype': can't convert nil into String>
## END RESULT ##
As you can see i accidentally pasted the line "xpath = File.join(path, x)" INSIDE the conditional. However, for some reason Ruby committed the following three crimes:
############################################################
# Warning #
############################################################
# In the following paragraphs the author has used #
# emotional language to describe his mental state whilst #
# suffering the affects from a subtle bug. Please read #
# this section objectively and do not get offended by #
# these words. The author does not intend to offend any #
# member of this fine community or the even the language #
# designer himself. The author simply wants to understand #
# what the heck is going on! Thank you. #
############################################################
1. Ruby did not throw a SyntaxError:
I guess some might argue that the programmer should be responsible breakingsyntax, and i agree, HOWEVER, syntax error can and will happen and catching this type of error at the syntactical level BEFORE it has time to metastasis is just good language design.
2. Ruby re-assigned the value of a variable, OR, failed to interpret the value correctly and simply defaulted to nil.
I am amazed that Ruby would overlook the broken control structure, okay, maybe this is a feature of the language that i was unaware of-- if so i can accept that--but why then did Ruby go on to add insult to injury by haphazardly assigning "nil" to my variable `xpath`. What possible good could come of that?
3. Ruby did not throw a NameError:
Finally, but worst of all, (and a direct result of re-assignment); Ruby didnot throw a NameError when i tried to access xpath. xpath should never have had existed. I could "somewhat" understand allowing the syntactical mistake and just ignoring xpath, then, throwing a NameError if i tried to access`xpath`. This would make sense at least. But what i uncovered makes no sense. It's madness.
Conclusion: Ruby had three chances to catch my mistake and notify me of that mistake but ruby failed all three times. Can someone please explain the philosophy behind this design?
"Errors should never pass silently!"