Some context
I'm playing with Ruby to deepen my knowledge and have fun while at the same time improving my knowledge of Esperanto with a just starting toy project called Ĝue. Basically, the aim is to use Ruby facilities to implement a DSL that matches Esperanto traits that I think interesting in the context of a programming language.
The actual problem
So a first trait I would like to implement is inflection of verbs, using infinitive in method declaration (ending with -i), and jussive (ending with -u) for call to the method.
A first working basic implementation is like that:
module Ĝue
def method_missing(igo, *args, &block)
case igo
when /u$/
celo = igo.to_s.sub(/u$/, 'i').to_s
send(celo)
else
super
end
end
end
And it works. Now the next step is to make it more resilient, because there is no guaranty that celo
will exists when the module try to call it. That is, the module should implement the respond_to?
method. Thus the question, how do the module know if the context where module was required include the corresponding infinitive method? Even after adding extend self
at the beginning of the module, inside of the module methods.include? :testi
still return false
when tested with the following code, although the testu
call works perfectly:
#!/usr/bin/env ruby
require './teke/ĝue.rb'
include Ĝue
def testi; puts 'testo!' ;end
testu
Note that the test is run directly into the main
scope. I don't know if this makes any difference with using a dedicated class scope, I would guess that know, as to the of my knowledge everything is an object in Ruby.
Aucun commentaire:
Enregistrer un commentaire