tags:

views:

74

answers:

1

Hi there,

I want to rewrite several methods of HighLine to customise my console and at the moment my code looks like this:

        cmd = ask("#{@prompt_label} #{@prompt_separator} ", @tab_completion_candidates) do |q|
            q.readline = true

            # rewriting the Question class to make it do what we want
            class << q
               HERE I WRITE MY CODE
            end
        end

I would like to be able to separate my changes from my main console file, so let's say i have a class Console::Question that contains all the changes I want to do in HighLine::Console, I'd like to be able to do something like that:

Console::Question << q
end

But unfortunately that doesn't work :)

Any solution?

Thanks for your time.

+2  A: 

If you put your changes in a module rather than a class then you can then do

q.extend(YourModule)

e.g. to override valid_answer?

module QuestionCustomisations
  def valid_answer?
    # your code here
  end
end

q.extend(QuestionCustomisations)

This will apply your changes in just the object instance which is passed to the block.

mikej
thanks for that :)
Benjamin