Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #6709

Re: Subtle Bug Reveals Major Design Flaw

From Christian Neukirchen <chneukirchen@gmail.com>
Newsgroups comp.lang.ruby
Subject Re: Subtle Bug Reveals Major Design Flaw
Date 2012-12-24 22:29 +0100
Organization [ posted via ] IN-Ulm
Message-ID <87sj6vm949.fsf@gmail.com> (permalink)
References <ecf2782f-07a0-4ac5-b093-30ce71b48078@googlegroups.com>

Show all headers | View raw


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},
 "<compiled>",
 "<compiled>",
 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  <chneukirchen@gmail.com>  http://chneukirchen.org

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Subtle Bug Reveals Major Design Flaw rantingrickjohnson@gmail.com - 2012-12-24 09:19 -0800
  Re: Subtle Bug Reveals Major Design Flaw Christian Neukirchen <chneukirchen@gmail.com> - 2012-12-24 22:29 +0100
  Re: Subtle Bug Reveals Major Design Flaw Robert Klemme <shortcutter@googlemail.com> - 2012-12-25 00:05 +0100
    Re: Subtle Bug Reveals Major Design Flaw Rick Johnson <rantingrickjohnson@gmail.com> - 2012-12-24 20:33 -0800
    Re: Subtle Bug Reveals Major Design Flaw Paul Magnussen <magiconinc@earthlink.net> - 2012-12-25 06:31 -0800

csiph-web