Problem with second one is that it's adding the function to Object's instance_methods. So monkey patching + no namespacing. It's good for scripts, but iffy for apps and shouldn't be done in a lib. The first one actually has this problem, too, but it's added to Object's constants.
-
-
-
I think people probably prefer the first one because you can then namespace the rest of your constants within that one toplevel module/class, which you can't do with functions in Ruby. Anyway, my alternative of preference is `do_stuff = lambda { |x, y| }`
- 9 more replies
New conversation -
-
-
Might have to do with the sad fact that def do_stuff (unnamespaced) ends up on all objects :(
-
Sure, but you have the same issue with constants (though method names are certainly more likely to conflict than constant names). It's a shame we don't have a way to expose functions without affecting global state somewhere.
- 5 more replies
New conversation -
-
-
Although I mostly agree, ensuring everything is in a class/module does at least give us a bit of consistency and less chance of conflicts given that in Ruby everything is dumped into the global namespace once it's been required anywhere
- End of conversation
New conversation -
-
-
def do_stuff(x,y) http://ImmaMonster.new (x,y).do_stuff end
-
Oh yeah I forgot about this pattern too class TotallyNecessary def initialize(x, y)
@x = x@y = y end def http://self.call (x, y) new(x, y).call end def call # use instance variables instead of locals. Yay mutability! end end - 1 more reply
New conversation -
-
-
Don't even get me started on utility modules with module_function
Thanks. Twitter will use this to make your timeline better. UndoUndo
-
-
-
That always felt to me like Java in Ruby.
Thanks. Twitter will use this to make your timeline better. UndoUndo
-
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.