#ruby - 18 March 2018
« Back 1 day Forward 1 day »
[20:50:49] cagomez: I have MyModule::SomeName::Factory and MyModule::SomeName::SomeClass. When I do `MyModule::SomeName.const_get(:SomeClass)`, it finds the `SomeClass` defined in my models folder (this is a Rails app). If I set `inherit` to False, I get "uninitialized constant MyModule::SomeName::SomeClass"
[20:52:05] cagomez: I'm trying to avoid enumerating the classes in `MyModule::SomeName` within my Factory. Is there a way around this?
[21:16:31] apeiros: cagomez: you have to require the file where MyModule::SomeName::Factory is defined
[21:17:53] cagomez: ah I see. I'm only getting this error when running a test. We have `const_get` being used in production and it's working. is my error because my module constants aren't being required during RSpec tests? is this even the right terminology?
[21:25:06] apeiros: cagomez: a constant only exists when it has been defined. and in order for it to be defined, the code which defines it has to be loaded. in plain ruby, this is usually a manual process via require
[21:25:07] apeiros: in rails apps, constants which don't exist are dynamically loaded upon first use via const_missing
[21:25:07] apeiros: rails looks in app/*/ for files matching the constant name which is missing and loads it
[21:58:13] xco: hi i have a folder name that looks like this “./folder/cats/1.4” what will be the best way to grab just “cats/1.4” out of the name? rejex or using #splic(‘/‘) to pick the last two and joining again?
[21:59:52] Radar: xco: Pathname.new("./folder/cats/1.4").each_filename.to_a.last(2).join(File::SEPARATOR)
[21:59:57] Radar: >> Pathname.new("./folder/cats/1.4").each_filename.to_a.last(2).join(File::SEPARATOR)
[21:59:58] ruby[bot]: Radar: # => uninitialized constant Pathname (NameError) ...check link for more (https://eval.in/974206)
[22:00:06] Radar: >> require 'pathname'; Pathname.new("./folder/cats/1.4").each_filename.to_a.last(2).join(File::SEPARATOR)