Gem info

Hi list,

Does anyone know if there's a way to know if my code is being run as a gem installed on the system, or just from source laying around?

In the former case, is there an explicit way to acquire the Gem::Specification belonging to the code actually being ran, as oposed to searching by e.g. gem name?

thx
mortee

You could go into your terminal and do the command gem list, as for your second question I'm not entirely sure what you mean

···

Sent from my iPhone

On Dec 8, 2015, at 10:56 AM, mortee <mortee.lists@kavemalna.hu> wrote:

Hi list,

Does anyone know if there's a way to know if my code is being run as a gem installed on the system, or just from source laying around?

In the former case, is there an explicit way to acquire the Gem::Specification belonging to the code actually being ran, as oposed to searching by e.g. gem name?

thx
mortee

You mean something like `Gem.loaded_specs` or `$LOADED_FEATURES`?

···

On Tue, Dec 8, 2015 at 5:56 PM, mortee <mortee.lists@kavemalna.hu> wrote:

Hi list,

Does anyone know if there's a way to know if my code is being run as a gem
installed on the system, or just from source laying around?

In the former case, is there an explicit way to acquire the
Gem::Specification belonging to the code actually being ran, as oposed to
searching by e.g. gem name?

thx
mortee

--
Michael Fellinger

I mean to find out from within the code itself (programmatically), if it's being run as an installed gem, and if so, get the gemspec belonging to it. Something like:

Gem::Specification.find_by_contained_file(__FILE__)

which would either return a gemspec, if the code is being ran as a properly installed gem, and nil otherwise, e.g. if it's just executed from a checked out working copy.

···

On 2015.12.08. 19:51, Michael Fellinger wrote:

You mean something like `Gem.loaded_specs` or `$LOADED_FEATURES`?

On Tue, Dec 8, 2015 at 5:56 PM, mortee <mortee.lists@kavemalna.hu > <mailto:mortee.lists@kavemalna.hu>> wrote:

    Hi list,

    Does anyone know if there's a way to know if my code is being run
    as a gem installed on the system, or just from source laying around?

    In the former case, is there an explicit way to acquire the
    Gem::Specification belonging to the code actually being ran, as
    oposed to searching by e.g. gem name?

    thx
    mortee

--
Michael Fellinger

You could use a shell command:

puts `gem list`

···

Sent from my iPhone

On Dec 8, 2015, at 2:26 PM, mortee <mortee.lists@kavemalna.hu> wrote:

I mean to find out from within the code itself (programmatically), if it's being run as an installed gem, and if so, get the gemspec belonging to it. Something like:

Gem::Specification.find_by_contained_file(__FILE__)

which would either return a gemspec, if the code is being ran as a properly installed gem, and nil otherwise, e.g. if it's just executed from a checked out working copy.

On 2015.12.08. 19:51, Michael Fellinger wrote:
You mean something like `Gem.loaded_specs` or `$LOADED_FEATURES`?

On Tue, Dec 8, 2015 at 5:56 PM, mortee <mortee.lists@kavemalna.hu> wrote:
Hi list,

Does anyone know if there's a way to know if my code is being run as a gem installed on the system, or just from source laying around?

In the former case, is there an explicit way to acquire the Gem::Specification belonging to the code actually being ran, as oposed to searching by e.g. gem name?

thx
mortee

--
Michael Fellinger

I guess the easiest might be to check Gem.path:
file = File.expand_path(__FILE__)
Gem.path.any?{|path| file.start_with?(path) } # => true || false

···

On Tue, Dec 8, 2015 at 9:26 PM, mortee <mortee.lists@kavemalna.hu> wrote:

I mean to find out from within the code itself (programmatically), if it's
being run as an installed gem, and if so, get the gemspec belonging to it.
Something like:

Gem::Specification.find_by_contained_file(__FILE__)

which would either return a gemspec, if the code is being ran as a
properly installed gem, and nil otherwise, e.g. if it's just executed from
a checked out working copy.

On 2015.12.08. 19:51, Michael Fellinger wrote:

You mean something like `Gem.loaded_specs` or `$LOADED_FEATURES`?

On Tue, Dec 8, 2015 at 5:56 PM, mortee <mortee.lists@kavemalna.hu> wrote:

Hi list,

Does anyone know if there's a way to know if my code is being run as a
gem installed on the system, or just from source laying around?

In the former case, is there an explicit way to acquire the
Gem::Specification belonging to the code actually being ran, as oposed to
searching by e.g. gem name?

thx
mortee

--
Michael Fellinger

--
Michael Fellinger

What's the point in that?

First, I'm seeking a programmatic solution, not calling shell commands, if possible. The gem system has a lot of configuration information (actually, all of it) accessible via regular ruby objects, the commnad-line interface is just a wrapper around it.

Second, "gem list" merely lists installed gems - it reveals nothing about whether the code being currently executed is actually installed as a gem or running from elsewhere.

Third (though this is unrelated to my concern), there's no point in capturing the output of a shell command into a string using backticks, and then only using that to pass to puts which outputs it to the standard output. If you want to do that, simply call

system("gem list")

regards
mortee

···

On 2015.12.09. 00:10, thomas Perkins wrote:

You could use a shell command:

puts `gem list`

Sent from my iPhone

On Dec 8, 2015, at 2:26 PM, mortee <mortee.lists@kavemalna.hu > <mailto:mortee.lists@kavemalna.hu>> wrote:

I mean to find out from within the code itself (programmatically), if it's being run as an installed gem, and if so, get the gemspec belonging to it. Something like:

Gem::Specification.find_by_contained_file(__FILE__)

which would either return a gemspec, if the code is being ran as a properly installed gem, and nil otherwise, e.g. if it's just executed from a checked out working copy.

On 2015.12.08. 19:51, Michael Fellinger wrote:

You mean something like `Gem.loaded_specs` or `$LOADED_FEATURES`?

On Tue, Dec 8, 2015 at 5:56 PM, mortee <mortee.lists@kavemalna.hu >>> <mailto:mortee.lists@kavemalna.hu>> wrote:

    Hi list,

    Does anyone know if there's a way to know if my code is being
    run as a gem installed on the system, or just from source laying
    around?

    In the former case, is there an explicit way to acquire the
    Gem::Specification belonging to the code actually being ran, as
    oposed to searching by e.g. gem name?

    thx
    mortee

--
Michael Fellinger

Interesting approach, this might do it for deciding the question, if we're lacking a better solution.

I'd prefer consulting the gem database though, and see if any actually installed (and active?) gem lists the current file as its own. And then precisely identify that gem, and get the gemspec.

···

On 2015.12.08. 21:39, Michael Fellinger wrote:

I guess the easiest might be to check Gem.path:
file = File.expand_path(__FILE__)
Gem.path.any?{|path| file.start_with?(path) } # => true || false

On Tue, Dec 8, 2015 at 9:26 PM, mortee <mortee.lists@kavemalna.hu > <mailto:mortee.lists@kavemalna.hu>> wrote:

    I mean to find out from within the code itself (programmatically),
    if it's being run as an installed gem, and if so, get the gemspec
    belonging to it. Something like:

    Gem::Specification.find_by_contained_file(__FILE__)

    which would either return a gemspec, if the code is being ran as a
    properly installed gem, and nil otherwise, e.g. if it's just
    executed from a checked out working copy.

    On 2015.12.08. 19:51, Michael Fellinger wrote:

    You mean something like `Gem.loaded_specs` or `$LOADED_FEATURES`?

    On Tue, Dec 8, 2015 at 5:56 PM, mortee <mortee.lists@kavemalna.hu >> <mailto:mortee.lists@kavemalna.hu>> wrote:

        Hi list,

        Does anyone know if there's a way to know if my code is being
        run as a gem installed on the system, or just from source
        laying around?

        In the former case, is there an explicit way to acquire the
        Gem::Specification belonging to the code actually being ran,
        as oposed to searching by e.g. gem name?

        thx
        mortee

    -- Michael Fellinger

--
Michael Fellinger

What hasn’t kept you from making a trial for the sake of speed and easy memory analytics?

1 million is indeed a challenge. I my last language, maps (hash) was huge quick, but I couldn’t begin to address the memory footprint.

In all cases, clean up your footprint.

···

On Dec 8, 2015, at 8:44 PM, mortee <mortee.lists@kavemalna.hu> wrote:

What's the point in that?

First, I'm seeking a programmatic solution, not calling shell commands, if possible. The gem system has a lot of configuration information (actually, all of it) accessible via regular ruby objects, the commnad-line interface is just a wrapper around it.

Second, "gem list" merely lists installed gems - it reveals nothing about whether the code being currently executed is actually installed as a gem or running from elsewhere.

Third (though this is unrelated to my concern), there's no point in capturing the output of a shell command into a string using backticks, and then only using that to pass to puts which outputs it to the standard output. If you want to do that, simply call

system("gem list")

regards
mortee

Cheers, Bee

For the record: I came up with the below solution, it's more explicit than just checking against the gem base directories (e.g. prevents confusion caused by multiple versions of the same gem installed), and also it immediately yields the specification itself in an unambiguous way:

Gem::Specification.find{|g|__dir__.start_with? g.full_gem_path}

mortee

···

On 2015.12.08. 21:39, Michael Fellinger wrote:

I guess the easiest might be to check Gem.path:
file = File.expand_path(__FILE__)
Gem.path.any?{|path| file.start_with?(path) } # => true || false

On Tue, Dec 8, 2015 at 9:26 PM, mortee <mortee.lists@kavemalna.hu > <mailto:mortee.lists@kavemalna.hu>> wrote:

    I mean to find out from within the code itself (programmatically),
    if it's being run as an installed gem, and if so, get the gemspec
    belonging to it. Something like:

    Gem::Specification.find_by_contained_file(__FILE__)

    which would either return a gemspec, if the code is being ran as a
    properly installed gem, and nil otherwise, e.g. if it's just
    executed from a checked out working copy.

    On 2015.12.08. 19:51, Michael Fellinger wrote:

    You mean something like `Gem.loaded_specs` or `$LOADED_FEATURES`?

    On Tue, Dec 8, 2015 at 5:56 PM, mortee <mortee.lists@kavemalna.hu >> <mailto:mortee.lists@kavemalna.hu>> wrote:

        Hi list,

        Does anyone know if there's a way to know if my code is being
        run as a gem installed on the system, or just from source
        laying around?

        In the former case, is there an explicit way to acquire the
        Gem::Specification belonging to the code actually being ran,
        as oposed to searching by e.g. gem name?

        thx
        mortee

    -- Michael Fellinger

--
Michael Fellinger