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

···

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

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 Edgar
adgar@carboni.ca
http://carboni.ca/

···

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

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

I was afraid of that. Thanks.

···

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

On Jun 17, 2011, at 1:49 PM, Chad Perrin 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.

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

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 Edgar
adgar@carboni.ca
http://carboni.ca/

···

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

On Sat, Jun 18, 2011 at 03:03:50AM +0900, Michael Edgar 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.

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 Fri, Jun 17, 2011 at 11:16 AM, Chad Perrin <code@apotheon.net> wrote:

On Sat, Jun 18, 2011 at 03:03:50AM +0900, Michael Edgar wrote:
> On Jun 17, 2011, at 1:49 PM, Chad Perrin 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.

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.

···

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

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

> On Sat, Jun 18, 2011 at 03:03:50AM +0900, Michael Edgar 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.

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

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

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

···

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

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

On Sat, Jun 18, 2011 at 03:03:50AM +0900, Michael Edgar 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.

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.

K = Kernel
K.puts

···

El 17/06/2011 20:56, "m b" <snert@hotmail.se> escribió:

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

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

···

On Fri, Jun 17, 2011 at 12:53 PM, m b <snert@hotmail.se> 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?

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

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

Date: Sat, 18 Jun 2011 04:25:11 +0900
From: jgabrielygalan@gmail.com
Subject: Re: Rename Kernel ?
To: ruby-talk@ruby-lang.org

K = Kernel
K.puts
>
> 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
>

Ah.. Simple as that.. x) Thanks.

···

El 17/06/2011 20:56, "m b" <snert@hotmail.se> escribió:

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

Ok. Thank you for explaining.