rubyhighline

Rewrite a ruby class on the fly


I'm using HighLine to write my console application and I would like to modify the HighLine::Question::in_range! function so that tab completion stayed activated but that highline do not bother checking if the words typed are in the range.

So let's say I have a tab completion list like %w{app1, app2, app3}

I'd like to be able to do that in my console:

app1 -option1 value1 -option2 value2

From what I understand from the code in HighLine it appears that I would need to pass the in_range! check, so that function needs to return true.

I don't want to modify to code source HighLine directly in its file obviously. So I'm looking for a solution to be able to modify or rewrite that function on the fly in my own code. Is there a solution to do what I'm looking for?

Thanks for your time and I hope I've asked my question clearly enough.


Solution

  • If you want to modify the in_range? method for a particular Question object x, you can do

    class << x
      def in_range?
        true
      end
    end
    

    If you want to modify it for all Question objects:

    class Question
      def in_range?
        true
      end
    end
    

    However, I wonder if you really need to do that in your case. I think you can simply set above, below and in to nil in your question to trivialize the in_range? check.