Get execution name of program

Either $0 or FILE will return a filename to give context for how a
Ruby program is executed, but I have yet to figure out how to get the
command used to execute it. Specifically, I want the command without
options. If a program is executed using an alias from within a Unix
shell, for instance, I want the alias name.

Example:

> alias foo='/home/username/bin/foo.rb'

What would I use to get a return value of “foo”?

On Jun 17, 2011, at 1:49 PM, Chad P. wrote:

If a program is executed using an alias from within a Unix
shell, for instance, I want the alias name.

In bash, alias is a builtin; these aliases are expanded by the shell
before
the program begins execution. I’m afraid what you wish for is not
feasible.

Michael E.
[email protected]
http://carboni.ca/

On Sat, Jun 18, 2011 at 03:03:50AM +0900, Michael E. wrote:

On Jun 17, 2011, at 1:49 PM, Chad P. wrote:

If a program is executed using an alias from within a Unix shell, for
instance, I want the alias name.

In bash, alias is a builtin; these aliases are expanded by the shell
before the program begins execution. I’m afraid what you wish for is
not feasible.

I was afraid of that. Thanks.

On Jun 17, 2011, at 2:16 PM, Chad P. wrote:

On Sat, Jun 18, 2011 at 03:03:50AM +0900, Michael E. wrote:

In bash, alias is a builtin; these aliases are expanded by the shell
before the program begins execution. I’m afraid what you wish for is
not feasible.

I was afraid of that. Thanks.

If you’re desperate, you may find a way to interrogate bash as to its
current settings/aliases. You could then look at ARGV and find all
matching aliases. You’d always have ambiguity between an alias vs.
the expanded form, but you might be able to show an alias was not
used.

Michael E.
[email protected]
http://carboni.ca/

On Fri, Jun 17, 2011 at 11:16 AM, Chad P. [email protected] wrote:

I was afraid of that. Thanks.

You could always have your alias add an option for you to identify
itself -
i.e. alias x=‘ruby y.rb --alias=x’

John

On Sat, Jun 18, 2011 at 03:31:20AM +0900, Michael E. wrote:

If you’re desperate, you may find a way to interrogate bash as to its
current settings/aliases. You could then look at ARGV and find all
matching aliases. You’d always have ambiguity between an alias vs.
the expanded form, but you might be able to show an alias was not
used.

Yeah . . . but that’s much more effort than is warranted by my use case.
I’m just looking for a way to have my program use what one uses to call
the program in the usage banner for a --help option. Frankly, the user
can figure out the alias for him/her self at this point.

Thanks for the suggestion, anyway.

Hi.

I’m learning ruby. Amateur programmer.
I like to use the verbose form to call metods in module Kernel e. g.
‘puts’ like this ‘Kernel.puts’
I now it’s not required, but just to get a better grip of what’s going
on.
But I rather use K.puts if that’s possible…
I tried ‘alias’ but it doesn’t seem to work with module names?

/Mix

I was afraid of that. Thanks.
can figure out the alias for him/her self at this point.

Thanks for the suggestion, anyway.

If you’re wanting it for a --help usage, then why not use the old
hardlink
trick? Create hardlinks for the various “aliases” you’d invoke this as,
then $0 will be set appropriately.

Matt

K = Kernel
K.puts
El 17/06/2011 20:56, “m b” [email protected] escribi:

On Sat, Jun 18, 2011 at 03:55:40AM +0900, Matthew K. Williams wrote:

If you’re wanting it for a --help usage, then why not use the old
hardlink trick? Create hardlinks for the various “aliases” you’d
invoke this as, then $0 will be set appropriately.

This is for general usage, when I will be allowing wide distribution of
a
program, and not limited to circumstances where I control the
environment
in which it is deployed.

On Fri, Jun 17, 2011 at 12:53 PM, m b [email protected] wrote:

Hi.

I’m learning ruby. Amateur programmer.
I like to use the verbose form to call metods in module Kernel e. g.
‘puts’ like this ‘Kernel.puts’
I now it’s not required, but just to get a better grip of what’s going on.
But I rather use K.puts if that’s possible…
I tried ‘alias’ but it doesn’t seem to work with module names?

The constant “Kernel” is pointing to the module object that has has the
methods you are interested in. What you want is to create a new
constant,
“K”, that points to that same object.

K = Kernel
K.puts "Hello world!"

I’m learning ruby. Amateur programmer.
I like to use the verbose form to call metods in module Kernel e. g.
‘puts’ like this ‘Kernel.puts’
I now it’s not required, but just to get a better grip of what’s going on.
But I rather use K.puts if that’s possible…
I tried ‘alias’ but it doesn’t seem to work with module names?

/Mix

Ah… Simple as that… x) Thanks.

K = Kernel
K.puts "Hello world!"

Ok. Thank you for explaining.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs