user user - 3 months ago 9
Ruby Question

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)
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).each do |(c1, c2)|
define_method(:"#{c1}_to_#{c2}") .... end end


it works fine

Why it happens?

Answer

I think you need to write def self.available_currencies

Comments