Pit Capitain <pit / capitain.de> writes:

> Christian Neukirchen schrieb:
>>>>Sometimes, I'd like an "eating nil" that returns itself for each
>>>>method call, and is false.
>>>>
>>>>Then, stuff like that would be possible
>>>>
>>>>while line = gets.ignore_if_nil.chomp
>>>>  ...
>>>>end
>
> Here you go:
>
>    EATING_NIL = Object.new
>
>    def EATING_NIL.nil?
>      true
>    end
>
>    def EATING_NIL.method_missing(*)
>      self
>    end
>
>    class Object
>      def ignore_if_nil
>        self
>      end
>    end
>
>    def nil.ignore_if_nil
>      EATING_NIL
>    end

Unfortunately, that would make the loop turn endless, as:

  irb(main):021:0> puts "Doesn't work as I want"  if EATING_NIL
  Doesn't work as I want

It can't be done in Ruby-only, I think.

> Regards,
> Pit
-- 
Christian Neukirchen  <chneukirchen / gmail.com>  http://chneukirchen.org