get specification of executed gem from within - ruby

I have build a gem and want it to print its version.
I cant use Gem::Specification.find_by_name('mygem').version
because there are several versions of it installed.
Lets just say my program has just a single src file /bin/myruby containing this:
#!/usr/bin/env ruby
mygem_version = ???
puts "This is my gems version: #{mygem_version}"

A common convention is to create a lib/<your_gem_name>/version.rb that defines a YourGemName::VERSION constant. Then, you can refer to that constant in your gemspec, which is Ruby code that gets evaluated when the gem is built.
Read http://timelessrepo.com/making-ruby-gems for a guide that uses this approach.

If you're using Bundler (think rails), try
Bundler.definition.specs
else, make sure your gem has a VERSION constant you can ask these things

This worked for me and always returned the correct version.
#!/usr/bin/env ruby
puts "This is my gems version: #{Gem.loaded_specs['mygem'].version}"

Related

how can I know which Ruby executable was used in ruby script?

there are two Ruby environments on a system, normal ruby and Chef embedded ruby. I want to know, in a ruby script, which ruby executable is used to invoke the script itself. How can get that?
Recommended Solutions
Use the poorly-documented RbConfig module, if available:
RbConfig.ruby
#=> "/Users/foo/.rubies/ruby-2.7.0/bin/ruby"
Alternatively, you can use the easier-to-find Gem module from the standard library to do the same thing:
Gem.ruby
#=> "/Users/foo/.rubies/ruby-2.7.0/bin/ruby"
Other Approaches
The RbConfig and Gem modules are your best bet, but there may be times when you need to get at the version or path information another way. Here are some different approaches.
Get the Version
You can return the version of the executing Ruby as a String with:
RUBY_VERSION
#=> "2.7.0"
Get the Path
Ruby is usually installed to bin/ruby in the RUBY_ROOT. You can return the expected path to the running Ruby binary (and verify it actually exists, if necessary) as follows:
ENV["RUBY_ROOT"] + "/bin/ruby"
#=> "/Users/foo/.rubies/ruby-2.7.0/bin/ruby"
File.exist? ENV["RUBY_ROOT"] + "/bin/ruby"
#=> true
Alternatively, you can use Kernel#` to find the first Ruby in your PATH as follows:
`which ruby`.chomp
=> "/Users/foo/.rubies/ruby-2.7.0/bin/ruby"
There are certainly edge cases where either approach can be misleading, though. For example, Ruby may have been built in a non-standard way, or you may have invoked Ruby with a fully-qualified path rather than calling the first binary in PATH. That makes "roll your own" lookups less reliable, but if your environment is missing the RbConfig or Gem modules for some reason, this might be a reasonable alternative for you.

How do i get list of all installed packages in ruby 2.0.0

I want to get list of all installed packages on my machine using ruby gem or plugin. Functionality is similar to dpkg -l command on ubuntu. Is there any appropriate ruby gem or plugin available to get the same?
If you mean Ruby gems, then
gem list
You can execute a shell command inside the ruby interpreter. In your case, simply run
`dpkg -l`
The output will be a string containing the output of the command. Please note the `.
Keep in mind there are several ways to perform a shell command in Ruby.
There is a Debian package called ruby-debian (in older Debian releases it is called dpkg-ruby) which contains Ruby bindings to dkpg.
Note: it relies on a C extension, so it will not work on Ruby implementations that don't support them, such as IronRuby.

how do you start ruby 1.9 without rubygems

I want my app to not be able to use any installed gems. Is there a ruby 1.9 startup parameter or way of doing this programmatically?
ruby --disable-gems
is the MRI (1.9) commandline parameter. "It prevents the addition of gem installation directories to the default load path". (The Ruby Programming Language, p. 391)
Edit 25-10-2012: Ruby core had the same idea as #rogerdpack in the comments and added the more verbose ruby --help parameter. Ruby revision!
Looking at the rubygems configuration file, I would attempt to hack out gempath or gemhome to see if you can override (instead of just append to) defaults.
If, for example, setting gempath to be empty, or to point to /dev/null, prevents using system gems, then that would be the way to go.
The main advantage to this, as I see it, is that your anti-rubygems config file can be passed to ruby 1.9 as a startup parameter (so not coded in), well documented, and checked into your repository.
All of this is, of course, disregarding that rubygems is part of ruby 1.9's standard library - so ruby may choke and die if it can't have access to its gems, depending on how much of ruby's base install requires gem functionality. YMMV.

Why is ri on Windows returning nothing?

When I type ri at the command prompt it returns nothing:
Classes and Modules known to ri:
How do I set this up properly?
Many thanks.
You can type gem rdoc --all --overwrite to have gem recreate ri and rdocs for all gems.
You don't say what version of Ruby you have installed, but 1.8.7 seemed to have problems remembering whether it had docs for the core and standard libraries. There was a gem called something like core-data that helped. If I can remember the real name I'll update this.
1.9.2 is better about its docs but I've seen times where I have to force the rebuild using the command above. A gem can act up, causing RDoc to puke, which ends up keeping all the docs from being generated so watch its progress and compare it to what gem list outputs as a sanity check. If that happens I have a shell script I use to walk through all the gems and rebuild their docs one by one. It's an easy piece of code to write and steps around the problem of a single gem killing the processing of everything.
I think you don't have the path to your ruby install folder in your PATH variable.
You have to do it to be allowed to launch ri in your command prompt.
Otherwise you can use the entire path : C:\Ruby\bin\ri.bat

How do you check the Gem Version in Ruby at Runtime?

Is it possible to check the gem version of the currently loaded gem in a ruby/rails app?
During debugging, I would like to be able to do something like:
puts RubyGem.loaded_version(:active_support)
Anything like that exist?
puts Gem.loaded_specs["activesupport"].version
careful when comparing against Gem.loaded_specs['mini_magick'].version as it's not a String but a Gem::Version object !
the version string is accessible using Gem.loaded_specs['mini_magick'].version.version which is ugly and might not work as expected e.g. '2.2' > '2.10' !
the correct way to compare against a gem version is :
Gem.loaded_specs['mini_magick'].version < Gem::Version.create('2.0')

Resources