On Jan 27, 2:50 pm, Richard Roberts <ricisb... / yahoo.co.uk> wrote:
> I'm glad not everyone thinks I'm mad!
>
> Does anyone know a good way to simulate the C++ and Java behaviour in
> ruby? I've tried various things, but to no avail (e.g. declaring them as
> class-level instance variables in a private block, creating private
> accessors etc).

Use define_method against local vars.

  class X
    x = 100

    define_method(:x) do ; x; end
  end

  X.new.x #=> 10

Generalizing this, perhaps make use an OpenStruct (or Facets' 
OpenObject class) and use that for local vars:

  class X
    local = OpenObject.new
    local.x = 10

    define_method(:x) do
      local.x
    end
  end

Personally I think Ruby would do well to make it's method defintion 
syntax uniform across def and define_method. Instead it could control 
scope via alternate forms of 'do'. In other words, let 'def' be a 
method too, or at least a short hand that actually calls 
define_method. It would be nice to write:

  class X
    local = OpenObject.new
    local.x = 10

    def x does
      local.x
    end
  end

Where 'does' keeps the scope open.

T.