3 ms·
Does Ruby have support for just exporting functions around? All these examples are class based. When I was a Python programmer, we found functions scaled infini
by no_wizard 4y ago
Does Ruby have support for just exporting functions around? All these examples are class based. When I was a Python programmer, we found functions scaled infinitely better over time (with the exception of data classes, unfortunately) for complex business logic especially.
- makeitdouble 4y agoI think the smallest footprint would be a module ? Importing a module add all the functions declared in the module to your current scope. From memory, you can't restrict to one or two specific functions, but in practice it's rarely an issue.
- thr0wawayf00 4y agoNo, unfortunately. This is one thing I've always wished Ruby had.
- remram 4y agoRuby is object-oriented, for better or for worse. Similar to Java, you can have static methods, but they have to live in a class (or module in Ruby's case).
- thr0wawayf00 4y agoI assumed OP was asking if Ruby had a way to import specific functions from a module (i.e. "from math import pi".) the way that many other languages (like Python) support. Instead, Ruby forces you to require the entire module, which makes tracking public interface usage harder.
- kayodelycaon 4y agoIf you're looking for that level of granularity, you can implement each method as a class, or as a static method on a class or module. Then it's fairly easy to figure out where things are used.
- transfire 4y agoNot sure what you mean. You can do… module Foo extend self def bar “Bar!” end end Foo.bar #=> “Bar!” And you can include that module in other modules. No classes required.
- TylerE 4y agoIn python you can ‘from foo import bar, buzz’ and get JUST those two functions, without having to prefix them or clobbering your namespace. This really nice when the one function you need is giantlib.helpers.foobar.convert_baz_to_qux - it’s both less bytes (especially if you are calling it a lot) and more importantly to the next reader of the code it means that c_b_to_q is the only thing you’re using in giantlib
- transfire 4y agoI see. Yeah because of this Ruby kind of pressures thoughtful coders into small modules. Heck, I created Ruby Facets and it’s basically one method per file!
- kayodelycaon 4y agoNot really. Your options would be implementing one class per method, static methods on a class/module, or making a module you can include in classes that need them. I tend to go with static methods on modules.