RFC - One word alias for require_relative

D

David Masover

What if, instead of having CWD in the load path, we had
`File.dirname(__FILE__)`? Essentially, CWD of the file that the require
statement is originated through.

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.
Then the old interface would work again,
it would be simpler to think about, security risks of CWD in the load path
would be addressed, and most importantly, I could write beautiful code
again (I die a little bit inside each time I write __FILE__).

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.
Of course, I have no idea if it's even possible, and it's not immediately
clear to me how it would affect gems.

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?
 
I

Ilias Lazaridis

You do know that 'include' is already used in Ruby. Right?

Right?!??

yes, to describe "mixing"

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

Any other word?

..
 
I

Ilias Lazaridis

This is a simple Request for Comments.

Scenario:

require_relative 'lib/alter'
require 'alibrary'

Some project manager complains about "require_relative", and asks you
to find a one-word alias.

Which name would you select and for what reasons?

Requirements
must:
* one word

optional:
* ideally a 7 letter word

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

To my understanding, both should do the same thing.

Is this right?

..
 
M

Michael Edgar

[Note: parts of this message were removed to make it a legal post.]

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

To my understanding, both should do the same thing.

Is this right?


No.

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

those two are the same.

Michael Edgar
(e-mail address removed)
http://carboni.ca/
 
F

Florian Gilcher

=20
require_relative 'lib/alter'
require './lib/alter'
=20
To my understanding, both should do the same thing.
=20
Is this right?

No, the first is relative to the file, the second is relative to =
"Dir.pwd", the process working directory.

Regards,
Florian
 
I

Ilias Lazaridis

No, the first is relative to the file, the second is relative to "Dir.pwd", the process working directory.

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.

..
 
M

Michael Edgar

And so I still need a shorter name.


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
(e-mail address removed)
http://carboni.ca=
 
J

John W Higgins

[Note: parts of this message were removed to make it a legal post.]

Afternoon,

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

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
 
M

Michael Edgar

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.
=20
Why would you ever think Ilias would need something foolish? Are you = aware
of any past history that the rest of us are not?

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
(e-mail address removed)
http://carboni.ca/=
 
I

Ilias Lazaridis

If *you* need a shorter name for some absurd reason,

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.
then *you* can simply do the following:

module Kernel
  alias locally require_relative
end

I am aware of this mechanism.
Where "locally" can be any name *you* wish!

But still, I need a concise name.
Problem solved!

..
 
I

Ilias Lazaridis

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

"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)

..
 
D

David Masover

=20
I understand.
=20
Thus the first works for the main file, *and* for included files which
can use again require_relative.
=20
The second works only for the main file (usually executed from it's
location).

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

Ilias Lazaridis

Not really, no.

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

The second doesn't work at all, unless it's actually what you intend. For
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.

Yes, you're right.

I forgot those cases.

What is sure for me now is:

I need the functionality that "require_relative" provides.

..
 
D

David Masover

=20
If something is absurd, than it's the fact that the name
=20
"require_relative"
=20
has made it into the core. For sure this was kind of an accident,
because everyone was busy or tired or something else.

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.
Either it should be corrected soon to be a key*word*.

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.
or at least, if multi-word, then it should be written more clearly:
=20
"require_relative_to_this_file"

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.
There are many ways to ruin a language.

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
 
I

Ilias Lazaridis

[...]

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
 
J

John W Higgins

[Note: parts of this message were removed to make it a legal post.]

2011/6/15 Ilias Lazaridis said:
(I'm unemployed, ....

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

John
 
P

Phillip Gawlowski

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

John

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
 
A

Adam Prescott

2011/6/15 Ilias Lazaridis said:
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 can only assume that these two pairs of lines being next to each
other is a joke.
 
I

Ilias Lazaridis

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

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.
And now I have to shower...

Good idea!

..
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top