ways to define a global method in ruby

前端 未结 2 1924
甜味超标
甜味超标 2021-02-09 08:15

I\'m writing a small gem, and I want to define a DSL-like method, pretty much the same as the desc and task methods in Rake.

R

2条回答
  •  生来不讨喜
    2021-02-09 09:05

    If you define private method in Kernel module it will be available in the whole project. You will also rewrite desc method that project use to define rake task. But if you write your methods in your submodule and then extend it in superclass or some module - you can easily write any kind of DSL lang like you might saw in Rake or RSpec.
    P.S. Making methods private prevents other moludes or classes (but not subclasses) to use them (but not owerwrite) - I mean module nesting hierarchy.

提交回复
热议问题