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:

    bin/
    bunch of scripts
    lib/
    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
    anywhere.

    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
    #21
    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
    #22
    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
    #23
  4. [Note: parts of this message were removed to make it a legal post.]


    No.

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

    those two are the same.

    Michael Edgar

    http://carboni.ca/
     
    Michael Edgar, Jun 15, 2011
    #24
  5. No, the first is relative to the file, the second is relative to =
    "Dir.pwd", the process working directory.

    Regards,
    Florian
     
    Florian Gilcher, Jun 15, 2011
    #25
  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
    location).

    -

    So it looks I can't drop require_relative.

    And so I still need a shorter name.

    ..
     
    Ilias Lazaridis, Jun 15, 2011
    #26

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

    module Kernel
    alias locally require_relative
    end

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

    Problem solved!

    Michael Edgar

    http://carboni.ca=
     
    Michael Edgar, Jun 15, 2011
    #27
  8. [Note: parts of this message were removed to make it a legal post.]

    Afternoon,

    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
    alias.

    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
     
    John W Higgins, Jun 15, 2011
    #28
  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

    http://carboni.ca/=
     
    Michael Edgar, Jun 15, 2011
    #29
  10. If something is absurd, than it's the fact that the name

    "require_relative"

    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:

    "require_relative_to_this_file"

    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
    #30
  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
    #31
  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,=
    =20
    what directory I happen to be in when I run it is what determines what the=
    =20
    second is relative to.

    Even on Windows, the working directory isn't always where the main file is.
     
    David Masover, Jun 15, 2011
    #32
  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
    #33
  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=
    hod=20
    =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=
    n't=20
    any better in this regard -- in fact, it's significantly worse, since at le=
    ast=20
    we know what 'require' usually means.

    The alternative is Java-like madness, where define_method is actually=20
    define_method_on_this_class_with_this_block_I_pass_in_here.
    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
    #34
  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!

    ..

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

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

    John
     
    John W Higgins, Jun 15, 2011
    #37
  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...

    --=20
    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
    #38
  19. I can only assume that these two pairs of lines being next to each
    other is a joke.
     
    Adam Prescott, Jun 16, 2011
    #39
  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-
    (self)employed.
    Good idea!

    ..
     
    Ilias Lazaridis, Jun 16, 2011
    #40
    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.