RFC - One word alias for require_relative

Discussion in 'Ruby' started by Ilias Lazaridis, Jun 11, 2011.

  1. Close, but no. When I call 'require' in, say, lib/foo/bar.rb, I want "require
    'foo/baz'" to work, but "require 'baz'" should not, and it especially should
    not have lib/foo/baz.rb mask lib/baz.rb.

    So, if this is going to be a default, I'd much rather it apply to the original
    file only -- that is, the file in which Ilias' Kernel#executed? (or whatever)
    returns true.

    Then again, how many points of entry does a typical application have? I think
    the worst-case structure might be something like:

    bunch of scripts
    bunch of library stuff that should be in the load path

    Worst case, I might put an init.rb or something in the project root, so that
    each script starts off with:

    require_relative '../init'

    Then, init.rb includes something like this:

    $: << File.join(File.dirname(__FILE__), 'lib')

    Ugly, but it's only in one place. Or, if you're likely to deal with enough
    other pathnames to make it worthwhile:

    require 'pathname'
    $: << Pathname(__FILE__).parent.join('lib')

    Slightly prettier, and __FILE__ is contained to one place inside your project.
    I don't like it either, but it just doesn't seem that critical to me.
    I tend to agree that it reduces the security risks. I can see myself putting a
    Ruby script in my PATH and running it as a command in an unsafe directory. I
    can't often see a case where CWD in the path would be more useful than the
    root of the project. I really can't see a case where I would drop a Ruby
    script in an unsafe directory and then execute it -- it seems like if the
    directory is unsafe, no attempt to make the script safe is going to get me

    Still, as I said, it doesn't seem that critical to me. It seems like
    require_relative, Pathname, and __FILE__ all do the trick.
    I'm not sure how it would affect them, but it seems like gems would need this
    even less. With a gem, your $: is pretty much set up for you, so long as you
    follow the conventions about putting scripts in bin and libraries in lib.

    So it's again something that doesn't seem to matter much -- how many
    applications do we have, in comparison to gems?
    David Masover, Jun 12, 2011
    1. Advertisements

  2. yes, to describe "mixing"

    I realize that this would lead to confusion (or even collisions).

    Any other word?

    Ilias Lazaridis, Jun 12, 2011
    1. Advertisements

  3. require_relative 'lib/alter'
    require './lib/alter'

    To my understanding, both should do the same thing.

    Is this right?

    Ilias Lazaridis, Jun 15, 2011
  4. [Note: parts of this message were removed to make it a legal post.]


    require_relative 'lib/alter'
    require File.join(File.dirname(__FILE__), 'lib/alter')

    those two are the same.

    Michael Edgar

    Michael Edgar, Jun 15, 2011
  5. No, the first is relative to the file, the second is relative to =
    "Dir.pwd", the process working directory.

    Florian Gilcher, Jun 15, 2011
  6. I understand.

    Thus the first works for the main file, *and* for included files which
    can use again require_relative.

    The second works only for the main file (usually executed from it's


    So it looks I can't drop require_relative.

    And so I still need a shorter name.

    Ilias Lazaridis, Jun 15, 2011

  7. If *you* need a shorter name for some absurd reason, then *you* can =
    simply do the

    module Kernel
    alias locally require_relative

    Where "locally" can be any name *you* wish!

    Problem solved!

    Michael Edgar

    Michael Edgar, Jun 15, 2011
  8. [Note: parts of this message were removed to make it a legal post.]


    Michael, you clearly didn't read the first message here..... Ilias wouldn't
    ever need something this absurd - it's a "project manager" that needs this

    Why would you ever think Ilias would need something foolish? Are you aware
    of any past history that the rest of us are not?

    John W Higgins, Jun 15, 2011
  9. While I guess I deserve a (honestly delightfully) sarcastic reply for =
    responding to
    him, I was mainly picking on how he slipped up and said "I still need a =
    shorter name."

    Michael Edgar

    Michael Edgar, Jun 15, 2011
  10. If something is absurd, than it's the fact that the name


    has made it into the core. For sure this was kind of an accident,
    because everyone was busy or tired or something else.

    Either it should be corrected soon to be a key*word*.

    or at least, if multi-word, then it should be written more clearly:


    There are many ways to ruin a language.

    Step by step, silently.
    I am aware of this mechanism.
    But still, I need a concise name.
    Ilias Lazaridis, Jun 15, 2011
  11. "I still need a shorter name" for the project-manager.

    (I'm unemployed, the project-manager is a fictional one, just to have
    a more realistic use-case)

    Ilias Lazaridis, Jun 15, 2011
  12. Not really, no.

    More like, the first works wherever I'd normally be hacking around with=20
    __FILE__ to get a decent require path.

    The second doesn't work at all, unless it's actually what you intend. For=20
    instance, if I add a ruby script to my PATH as a command, and then run it,=
    what directory I happen to be in when I run it is what determines what the=
    second is relative to.

    Even on Windows, the working directory isn't always where the main file is.
    David Masover, Jun 15, 2011
  13. Yes, you're right.

    I forgot those cases.

    What is sure for me now is:

    I need the functionality that "require_relative" provides.

    Ilias Lazaridis, Jun 15, 2011
  14. Of course, because the only reason anyone would ever disagree with you is=20
    because they're busy, tired, stupid, or the wrong "kind" of person.
    It's not a keyword, it's a method. It's also far from the only two-word met=
    =2D- consider define_method or instance_variable_set.
    At a certain point, you have to assume that if it isn't obvious from the=20
    context what something does, people are willing to look it up. 'locally' is=
    any better in this regard -- in fact, it's significantly worse, since at le=
    we know what 'require' usually means.

    The alternative is Java-like madness, where define_method is actually=20
    Dramatic much?

    require_relative couldn't ruin a language -- it barely even touches the=20
    language, and can trivially be renamed or removed at runtime:

    Kernel.send :remove_method, :require_relative

    Oh, sorry, I guess that should be:

    Kernel.send_method_call :remove_method_from_this_class :require_relative
    David Masover, Jun 15, 2011
  15. [...]

    I've read everything, but I'll not comment.

    Do me a favour, please.

    Can you please try to find one word (ideally with 7 chars),
    independent of you think about the need.

    Just as an exercise.


    I have too cook now!


    Ilias Lazaridis, Jun 15, 2011
  16. How very suprising, given your invaluable work "auditing" many languages.
    Matt Harrison, Jun 15, 2011
  17. [Note: parts of this message were removed to make it a legal post.]

    Too easy, simply too easy.........

    John W Higgins, Jun 15, 2011
  18. Well, Ilias' grasp of the English language isn't the best, so he
    probably meant "I'm not employed by a company, but am self-employed".

    And now I have to shower...

    Phillip Gawlowski

    A method of solution is perfect if we can forsee from the start,
    and even prove, that following that method we shall attain our aim.
    =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-- Leibnitz
    Phillip Gawlowski, Jun 16, 2011
  19. I can only assume that these two pairs of lines being next to each
    other is a joke.
    Adam Prescott, Jun 16, 2011
  20. You are right, I was to lazy to write.

    Self-employed, but as I currently look for a contract, I'm kind of un-
    (self-)employed, or self-unemployed or whatever.

    The effect of "unemployed" and "self-unemployed" is essentially the
    same (e.g. empty freezer), possibly with the difference (in my case)
    that I am focused on personal projects, as long as I'm un-
    Good idea!

    Ilias Lazaridis, Jun 16, 2011
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.