1

I have a gem. Is there a way to retrieve the gem's specification from within the gem itself?

Specifically, I have some metadata that I want to retrieve and display when running one of the gem's executables. Is there a way to retrieve that metadata from within the gem?

mipadi
  • 398,885
  • 90
  • 523
  • 479
  • Perhaps this will give some pointer http://stackoverflow.com/questions/3262697/how-do-i-get-the-version-from-a-gemspec-file – Wand Maker Sep 27 '16 at 18:26
  • @WandMaker: Yeah, I guess that could work. I'd like to query the gem database somehow, but I guess I can construct a path to the gemspec instead. – mipadi Sep 27 '16 at 18:29

2 Answers2

1

Since your gem is definitionally already loaded by this point, Gem.loaded_specs['name-of-your-gem'] will give you what you want.

Documentation for Gem.loaded_specs

philomory
  • 1,709
  • 12
  • 13
0

The answer by @philomony put me on the right track. Here are more details. The following was executed in a debugger:

specs = Gem.loaded_specs['jekyll_plugin_support']
#<Gem::Specification:0x00007fdcc3ba7000 ...

specs.class
Gem::Specification

specs.author
"Mike Slinn"

specs.metadata
{"allowed_push_host"=>"https://rubygems.org", "bug_tracker_uri"=>"https://github.com/mslinn/jekyll_plugin_support/issues", "changelog_uri"=>"https://github.com/mslinn/jekyll_plugin_support/CHANGELOG.md", "homepage_uri"=>"https://www.mslinn.com/blog/2020/10/03/jekyll-plugins.html#quote", "source_code_uri"=>"https://github.com/mslinn/jekyll_plugin_support"}

Here are the public methods: to_yaml, full_name, conflicts, remote=, dependencies, platform, name=, specification_version, date, summary, authors, autorequire, cert_chain, description, email, extensions, extra_rdoc_files, homepage, licenses, metadata, post_install_message, rdoc_options, required_ruby_version, required_rubygems_version, signing_key, <=>, test_files, ==, relative_loaded_from, rg_full_gem_path, rg_loaded_from, location, load_paths, rg_required_ruby_version=, git_version, rg_extension_dir, nondevelopment_dependencies, to_gemfile, to_spec, deleted_gem?, eql?, removed_method_calls, add_bindir, authors=, email=, license=, metadata=, homepage=, licenses=, author=, platform=, signing_key=, bindir=, activated=, autorequire=, rubygems_version=, require_paths=, original_platform=, add_development_dependency, _deprecated_has_rdoc=, _deprecated_default_executable=, add_runtime_dependency, _deprecated_default_executable, installed_by_version, installed_by_version=, method_missing, required_ruby_version=, _deprecated_validate_permissions, required_rubygems_version=, _deprecated_validate_metadata, _deprecated_validate_dependencies, test_files=, activated, default_executable, default_executable=, _deprecated_has_rdoc, original_platform, _deprecated_has_rdoc?, location=, gems_dir, relative_loaded_from=, source=, to_s, inspect, groups, activated?, activate, name, version, traverse, requirements, has_conflicts?, conficts_when_loaded_with?, bin_file, executables, reset_nil_attributes_to_default, bindir, loaded_from, hash, date=, sort_obj, raise_if_conflicts, activate_dependencies, add_self_to_load_path, runtime_dependencies, files, spec_file, abbreviate, files=, rdoc_options=, extra_rdoc_files=, cert_chain=, sanitize, summary=, sanitize_string, description=, post_install_message=, add_dependency, author, bin_dir, build_args, build_info_file, build_extensions, build_info_dir, cache_dir, cache_file, satisfies_requirement?, _dump, default_value, dependent_gems, dependent_specs, development_dependencies, doc_dir, encode_with, mark_version, executable, executable=, executables=, extensions=, file_name, for_cache, rubygems_version, has_rdoc, has_rdoc=, has_rdoc?, has_unit_tests?, has_test_suite?, init_with, yaml_initialize, version=, lib_files, license, internal_init, missing_extensions?, name_tuple, pretty_print, original_name, require_path, require_path=, requirements=, ri_dir, spec_dir, spec_name, test_file, test_file=, to_ruby, raw_require_paths, source, to_ruby_for_cache, validate, keep_only_files_and_directories, validate_metadata, validate_dependencies, validate_permissions, stubbed?, specification_version=, extension_dir, full_gem_path, remote, base_dir, gem_dir, normalize, match_platform, matches_current_rubygems?, matches_current_ruby?, plugins, source_paths, datadir, lib_dirs_glob, base_dir=, extension_dir=, ignored=, full_gem_path=, require_paths, this, contains_requirable_file?, matches_for_glob, default_gem?, full_require_paths, gem_build_complete_path, loaded_from=, extensions_dir, to_fullpath, to_json, pretty_print_instance_variables, pretty_print_cycle, pretty_print_inspect, singleton_class, dup, itself, taint, tainted?, untaint, untrust, untrusted?, trust, methods, singleton_methods, protected_methods, private_methods, public_methods, instance_variables, instance_variable_get, instance_variable_set, instance_variable_defined?, remove_instance_variable, instance_of?, kind_of?, is_a?, display, public_send, class, frozen?, tap, then, extend, yield_self, clone, method, public_method, singleton_method, ===, define_singleton_method, =~, !~, nil?, respond_to?, freeze, object_id, gem, send, to_enum, enum_for, pretty_inspect, __send__, !, __id__, instance_eval, instance_exec, !=, equal?

Mike Slinn
  • 7,705
  • 5
  • 51
  • 85