Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news1.tnib.de!feed.news.tnib.de!news.tnib.de!newsfeed.in-ulm.de!not-for-mail From: Christian Neukirchen Newsgroups: comp.lang.ruby Subject: Re: Subtle Bug Reveals Major Design Flaw Date: Mon, 24 Dec 2012 22:29:58 +0100 Organization: [ posted via ] IN-Ulm Lines: 72 Sender: chris@juno Message-ID: <87sj6vm949.fsf@gmail.com> References: Mime-Version: 1.0 Content-Type: text/plain X-Trace: news.in-ulm.de 70A0AA730592B9BC46AE5B46A7E89B8B In-Reply-To: (rantingrickjohnson@gmail.com's message of "Mon, 24 Dec 2012 09:19:46 -0800 (PST)") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux) Xref: csiph.com comp.lang.ruby:6709 rantingrickjohnson@gmail.com writes: > 1. Ruby did not throw a SyntaxError: It is not a syntax error. In theory, "xpath" even could have been a method, of which is only known at execution time. >> Ripper.sexp("if false; a = 5; else p a; end") => [:program, [[:if, [:var_ref, [:@kw, "false", [1, 3]]], [[:assign, [:var_field, [:@ident, "a", [1, 10]]], [:@int, "5", [1, 14]]]], [:else, [[:command, [:@ident, "p", [1, 22]], [:args_add_block, [[:var_ref, [:@ident, "a", [1, 24]]]], false]]]]]]] "a" parses nicely here as arbitrary identifier. > I guess some might argue that the programmer should be responsible breaking syntax, 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. This is just how scoping works in Ruby: if/elsif/else don't introduce new scope: >> RubyVM::InstructionSequence.compile("if false; a = 5; else p a; end").to_a => ["YARVInstructionSequence/SimpleDataFormat", 1, 2, 1, {:arg_size=>0, :local_size=>2, :stack_max=>2}, "", "", nil, 1, :top, [:a], 0, [], [1, [:trace, 1], [:jump, :label_13], [:trace, 1], [:putobject, 5], [:dup], [:setlocal, 2], # a = 5 [:leave], [:pop], :label_13, [:trace, 1], [:putself], [:getlocal, 2], # a [:send, :p, 1, nil, 8, 0], [:leave]]] ... both "a" refer to the same local variable. > 3. Ruby did not throw a NameError: There is no NameError because the local variable is in scope. > Can someone please explain the philosophy behind this design? I'd prefer it to be otherwise, too. :) Essentially, nested scopes are introduced by blocks ({} or do...end) only. hth, -- Christian Neukirchen http://chneukirchen.org