I have this piece of code:
module Encryption
extend self
def adapter
return @adapter if @adapter
self.adapter = :@adapter
@adapter
end
def adapter=(adapter_name)
case adapter_name
when :@adapter
require_relative "#{adapter_name}"
# @adapter = Encryption::Adapters.const_get("#{adapter_name.pascalize_string}")
@adapter = Encryption::Adapters.const_get("#{adapter_name.pascalize_string}")
else
raise "Missing adapter #{adapter_name.pascalize_string}"
end
end
def new(key)
print key
adapter.new(key)
end
end
And I have a method:
def pascalize_string(string)
return string if string !~ /_/ && string =~ /[A-Z]+.*/
string.split('_').map { |part| part.capitalize }.join
end
How should I attach it to the first code, so I can use (@adapter = Encryption::Adapters.const_get("#{adapter_name**.pascalize_string**}")
.
I keep getting this error:
C:/Users/Ian/Desktop/Alpha/encryption/adapters/adapter.rb:23:in `adapter=': undefined method `pascalize_string' for :caesar:Symbol (NoMethodError)
Just define it inside the method.
It should be called with
not
because you are defining it into the module, not in the
String
module.There are a few other strange things in your code, such as the use of
:@adapter
, but they are not relevant to the topic of this question hence I left them as they are.