user user - 1 year ago 95
Ruby Question

Undefined local variable or method for module

I have module

with following methods

CURRENCIES = %w(uah rub eur usd)

def available_currencies
CURRENCIES.join(' ').downcase.split.permutation(2)

and when i want to use


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 Source

I think you need to write def self.available_currencies

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download