6 ms·
Why "require 'rubygems'" Is Wrong
- sunkencity 17y agoStupid article. So if I write a ruby program that needs rubygems I need to ask people that will run it to please run my program with "ruby -rubygems foo.rb" or set an environment variable, instead of just requiring the gems. Because that would infringe on the domain system administrator of that system. That doesn't make much sense to me.
- robertduncan 17y agoAgreed. I don't understand how require 'rubygems' is any less succinct than ruby -rubygems foo.rb.
- masklinn 17y ago> So if I write a ruby program that needs rubygems I think Ryan's point is that most of the time your program doesn't require rubygems itself. You just happen to manage your libraries using gems, somebody else could do it an other way (and never need rubygems), but you're forcing that person to install gems regardless.
- sunkencity 17y agoThings tend to break a little less over time if I can require a version of a gem, like: gem 'png', '= 1.1' The good part about rubygems is that it's possible to get a certain version of a gem. I'd rather use gem and version than just require "png", and get god knows what version or file.
- masklinn 17y agoThat's a good point, and in this case requiring rubygems does make sense, but I really doubt that's the most common use case. Stuff like this: http://github.com/mojombo/jekyll/blob/2f2e45bedf67dedb8d1dc0d02612345ee5c893f2/lib/jekyll.rb http://github.com/mojombo/jekyll/blob/2f2e45bedf67dedb8d1dc0... or that: http://github.com/mwunsch/weary/blob/cd7b35ccee66757c5efb0a03cd54f9e89f1eaad7/lib/weary.rb http://github.com/mwunsch/weary/blob/cd7b35ccee66757c5efb0a0... is probably the kind of stuff Ryan rages against in his rant (though you'd have to ask him if that's that, and how he'd see you use case)
- sunkencity 17y agoyep I can agree that require 'rubygems' require 'png' is kind of pointless.
- sho 17y agoI don't know who is voting you up? Maybe because you sound angry, and people associate angry technical rants with people who know what they're talking about? "I think Ryan's point is that most of the time your program doesn't require rubygems itself." Yeah! You could have manually downloaded the files for all the libraries you need, placed them in the directory, and required them directly. See, who needs rubygems? Oh hang on, rubygems is about 1000x easier than doing all that. "You just happen to manage your libraries using gems" Yes, because gems are the standard way of managing libraries in Ruby, and work transparently and conveniently. Where the hell is Ryan getting all these gem-using libraries anyway, if not from gems? And what is supposed to be wrong with it, anyway? I have never had a problem using a non-gem library even with gems present and a duplicate installed. Just require the non-gem lib first. What is so freaking hard? I assume it's some stupid problem he has to solve at Heroku - and his solution is trying to change how the entire Ruby community does things, to their detriment, benefiting only him. Screw him. Oh well, anyway .. who cares. You're a crank and so is he. Rubygems is here to stay and if you don't like it .. well again, who cares.
- masklinn 17y ago> You could have manually downloaded the files for all the libraries you need Yes, why not? > placed them in the directory Placed them in any directory on my $LOAD_PATH, yes indeed. > and required them directly Uh no, there's nothing to touch there. Library is on $LOAD_PATH, any require $the_library will find it perfectly normally. > See, who needs rubygems? Nobody. Rubygems is a very nice convenience, not a requirement for anything. > Where the hell is Ryan getting all these gem-using libraries anyway, if not from gems? Tarballs? Github? Rubyforge? His colleagues? The Internet? And as a side note: if the library was obtained from gems, then it was loaded from gems, which means by the time the library is reached rubygems has already been required and loaded. Where's the sense in requiring the tool which loaded you in the first place, exactly? Doesn't that seem a bit dumb to you? > And what is supposed to be wrong with it, anyway? And what's wrong with not wanting to use it, anyway? (and what's wrong with it is that they clutter the $LOAD_PATH. You don't care, Ryan apparently does) > Oh well, anyway .. who cares. You're a crank and so is he. Rubygems is here to stay and if you don't like it .. well again, who cares. You… completely missed the point. It's impressive.
- anatoly 17y agoPlease don't start your shell scripts with #!/bin/sh. If a script foobar needs to be interpreted with sh, just run it with '/bin/sh foobar', instead of ./foobar. Please don't put #include <stdlib.h> in your C source files. By doing this you remove my ability to compile your files in an environment where the standard library is not present. Instead, whenever you need to compile a file that uses stdlib.h functionality, simply use a small wrapper file that includes stdlib.h and then includes your source file, and compile that.
- flooha 17y ago"Please don't start your shell scripts with #!/bin/sh...run it with '/bin/sh foobar', instead of ./foobar." Are you serious?
- adamc 17y agoHe's making an analogy to the article.
- flooha 17y agoThat's what I was hoping...I guess I prefer my sarcasm a little more heavy handed. lol
- masklinn 17y agoYeah but his analogy is wrong in that /bin/sh is usually a link (or a copy) of the user's shell. So the user can easily configure which shell is loaded and how using /bin/sh (though /usr/bin/env sh would probably be even better). What Ryan's gripping against here is that the people requiring rubygems usually don't care for rubygems itself. They just want to require a library which (on their machine) happens to be managed via rubygems. And this management is not the sole strategy. Somebody else could be using another utility, or even no utility at all if they handle their $LOAD_PATH manually (which Ryan apparently does). And in these cases, requiring rubygems breaks their platform: they might not want or care for rubygems. In any case, it's usually not needed so why require it? And that's Ryan's point: by requiring rubygems when it's not needed (which is most of the time) in your library, you're forcing people to use rubygems whether they like (or need) it or not. Furthremore, his analogy is also wrong because even with a /bin/sh shebang *it's trivial for me to overload it by calling `./mysupershell yourscript`. With `require rubygems` there's no such recourse (apart from `require` hackery or other such crap).
- nanijoe 17y agoLet me see...every time I have required rubygems it has done exactly what I intended for it to do (which is to allow me to load some ruby gem), so I think I'll just go ahead and ignore your advise.
- patio11 17y agoThe system you use to manage your $LOAD_PATH is not my library / app / tests concern. Whether rubygems is used or not is an environment issue. My library or app should have no say in the matter. Explicitly requiring me to not use rubygems is either not necessary or misguided. When you feel the urge to strike "require 'rubygems'" because some shit isn’t working, stop and consider whether one of these solutions is more appropriate: 1) An empty rubygems.rb in your $LOAD_PATH should do the trick. 2) Redefine Kernel#require to intercept calls to load rubygems and silently discard them. Why You Shouldn't Force Non-Rubygems On People When I write you a library, appplication, or tests, I may not want to have to care how you execute my program. When you require that I not include 'rubygems' in my code, you remove my ability to make that decision. I cannot anticipate all of your requirements in advance, but you can set up your environment properly such that the defaults I choose are overridden.
- masklinn 17y agoSo instead of separating the library and the environment you suggest that ugly hacks are more appropriate? Really?
- tptacek 17y agoHaving the 99.9% of ruby users who don't have a religious problem with rubygems all specify rubygems on the commandline seems like a more ugly hack than having the 3 developers in the world who care about this problem alter their environment.
- extension 17y agoYou underestimate the amount of discontent with rubygems. There is at least one replacement available (hellorip.com) and for good reason. I've already come across a few libs that use it exclusively. Ruby 1.9 actually ignores "require 'rubygems'" entirely which I suppose will make this whole issue moot, as soon as we all start using it.. um, any day now.
- deleted 17y ago[deleted]
- ajkirwin 17y agoWhoever this guy is, he really needs to pull his head out of his ass. If someone's writing a library or app.. it's up to THEM whether they want to require rubygems or not. If you don't like how they do it.. then don't use their application/library, it's simple. Most people create code for their OWN use and THEN publish it for others to use.
- masklinn 17y ago> If someone's writing a library or app.. it's up to THEM whether they want to require rubygems or not. But… why? Most of the time it's not like they actually need gems. They just happen to use gems as a $LOAD_PATH manager, so they're leaking their environment configuration into the library. How can anyone defend that? It's like hardcoding the paths of dynamic libraries, or respawning your shell scripts because damn it if any god damn user is going to run it with anything other than /bin/bash. It's completely insane.
- sho 17y agothey just happen to use gems as a $LOAD_PATH manager Oh come on. That's like saying "you should not make your MacOSX app require Finder just because some people just happen to use it as their file manager". Just happen? It's the default, and overwhelmingly dominant, choice! It's freaking bundled in 1.9 core! rtomayko is a prominent and well regarded community member but he's pretty much a crank on this issue. He is the extreme odd one out who doesn't like rubygems, so it's on him to deal with it, rather than causing inconvenience for everyone else. He doesn't even give a good, or any, reason why he doesn't use gems. And as if I'm going to write -rubygems every time I run anything, give me a break. Rubygems is the official package manager and is used without any issue by 99.9% of Rubyists. It's not 100% perfect but in general works beautifully and greatly contributes to making everything in Ruby "just work". There is no reason for people to stop using it as the default choice.
- masklinn 17y ago> Oh come on. That's like saying "you should not make your MacOSX app require Finder just because some people just happen to use it as their file manager". Yeah it is. And indeed you shouldn't. > Just happen? It's the default, and overwhelmingly dominant, choice! It's freaking bundled in 1.9 core! So fucking what? (and as others have said, in 1.9 rubygems is required by default so that's a very good reason not to require it yourself ever). > Rubygems is the official package manager and is used without any issue by 99.9% of Rubyists. And so the rest should just take it up the ass and live with completely insane and nonsensical decisions such as randomly requiring rubygems just because? > There is no reason for people to stop using it as the default choice. That is not what Ryan says. Ryan doesn't care how you handle your own $LOAD_PATH. If you want to use gems, knock yourself out, that's your own business not his. Ryan just asks that you don't force others to use rubygems without any justification.
- ionfish 17y agoContext failure: linking to Ryan Tomayko's article without linking to the article that's brought it to people's attention again, namely Josh Peek's 'Gem Packaging: Best Practices'. http://weblog.rubyonrails.org/2009/9/1/gem-packaging-best-practices http://weblog.rubyonrails.org/2009/9/1/gem-packaging-best-pr... Giles Bowkett has also written about the related issue of screwing up the load path: http://gilesbowkett.blogspot.com/2009/04/unshiftfiledirnamefile.html http://gilesbowkett.blogspot.com/2009/04/unshiftfiledirnamef... It's also worth noting that Ruby 1.9 requires rubygems automatically, as it's included in the standard library.
- defunkt 17y agoThanks! Both the Rubygems Bundler and Rip (as Josh explains) are great examples of why "require 'rubygems'" might not always be the best idea. Bundler: http://github.com/wycats/bundler http://github.com/wycats/bundler Rip: http://hellorip.com/ http://hellorip.com/
- jdminhbg 17y agoThis is called begging the question: "Why You Shouldn’t Force Rubygems On People: When I use your library, deploy your app, or run your tests I may not want to use rubygems."
- toretore 17y agoThe point about not requiring rubygems in a library or a gem (as ironic as that may sound) is that by doing so you're coupling your implementation to your environment. This is as bad as coupling your classes to global constants, and not something you should do. Case in point: Rails allows you to "unpack" gems to make your app more portable, and it shouldn't matter if the "foo" gem uses the unpacked version or the one installed in the gem repository on the server.