1

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 no, as to the best of my knowledge everything is an object in Ruby.

psychoslave
  • 2,783
  • 3
  • 27
  • 44

1 Answers1

0

Found a working solution through In Ruby, how do I check if method "foo=()" is defined?

So in this case, this would be checkable through

eval("defined? #{celo}") == 'method'
psychoslave
  • 2,783
  • 3
  • 27
  • 44
  • It's not yet completely right actually. It fact, it seems that as is, `defined?` will returns 'method' anytime `celo` have a `to_s` method. First I meant `to_sym`, and even with that it won't work. The argument should be the identfier corresponding to the `celo` string. – psychoslave Jul 01 '18 at 21:33
  • Fixed the problem in the anwer – psychoslave Jul 01 '18 at 22:23