On May 26, 2007, at 11:13 PM, Erwin Abbott wrote:

> I have a "BlankState" type of class and it's method_missing forwards
> everything to an instance variable.  This way I can treat the object
> specially in my own code and all other code doesn't need to know the
> difference.  For a contrived example:
>
> class Proxy
>  # remove methods inherited by Object, etc
>  instance_methods.each do |m|
>    undef_method m unless m =~ /^__/
>  end
>
>  def initialize name, value
>    @name, @value = name, value
>  end
>
>  def to_myformat
>    "#{@name}: #{@value}"
>  end
>
>  def method_missing name, *args, &block
>    @value.send name, *args, &block
>  end
> end
>
> object = "regular string"
> x = Proxy.new("UID", object)
>
> x.class # => Array
> x.size  # => 5
> x.to_myformat # => UID: regular string

What I see when I evaluate your code is:

    x.class # => String
    x.size  # => 14

> While that's a pretty boring example, the idea might come in handy for
> something like a graph node, or linked list node (etc). Anyway, the
> problem I have is the way this works:
>
> case x
> when Array puts "Array"
> when Proxy puts "Proxy"
> end

If you really have to do this kind of thing, a work-around you might  
try is:

    result = case x.class.new
             when Array then "Array"
             when String then "String"
             when Proxy then "Proxy"
             end
    result # => "String"

That's kind of ugly, but then I think using case statements to  
distinguish the class of an object is kind of ugly under any  
circumstances.

Regards, Morton