vendredi 5 août 2016

Undefined local variable or method for module

I have module CurrencyExchange with following methods

 CURRENCIES = %w(uah rub eur usd)

 def available_currencies
   CURRENCIES.join(' ').downcase.split.permutation(2).map{|c|c.join('_to_')}
 end

and when i want to use available_currencies with

define_method

 available_currencies.each do |(c1, c2)|
    define_method(:"#{c1}_to_#{c2}") do |cr| ... end end

i have got an error

undefined local variable or method `available_currencies'
 for CurrencyExchange:Module (NameError)

but when i use it like

     CURRENCIES.join(' ').downcase.split.permutation(2).map{|c|c.join('_to_')}.each do |(c1, c2)|
  define_method(:"#{c1}_to_#{c2}") .... end end 

it works fine

Why it happens?

Aucun commentaire:

Enregistrer un commentaire