1

I've a gem with a helper module. eg

Gem module

module Hotel
 module MenuItem
  def menu(session)
    x = DEFAULT_FOOD_MENU[session]
  end

  def print_menu(menu)
    #printing menu
  end
end

custom class

module SizzSuzzHotel
 module MenuItem
   include Hotel::MenuItem
  def menu(session)
    # I want to use the default menu item and also specific menu related to this hotel! . 
  end
end

module SizzSuzzHotel
 class order
  include MenuItem
    def order(session)
       menu_item = menu(session)  
       print(menu_item)
    end


  end
end

Here I want to override the menu and I want to use the existing print_menu! How can I achieve this? use the gem module method and also add few more stuffs into it?

1 Answer 1

2

If you include the MenuItem module, both methods will be available, so you can easily redefine one of them.

include MenuItem

def menu(session)
  ... here you write you custom menu method behaviour
end

def order(session)
  menu_item = menu(session) # => above menu method will be called
  print(menu_item) # => print method from MenuItem module will be called
end
Sign up to request clarification or add additional context in comments.

1 Comment

I don't want to copy/paste or rewrite the base menu(session) function. Like using a super in class method , is there a better approach to reuse the gem method here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.