Posts Tagged Under: ruby

gem – trollop

trollop lets you easily create script parameters with meaningful names, instead of useing ARGV[0], ARGV[1],…etc.

If you have the following in your ruby script:

cat ./testscript.rb
#!/usr/bin/env ruby

require 'trollop'

opts = Trollop::options do
  opt :hostname, "Server FQDN.", :type => :string

puts opts[:hostname]

Next we install the gem:

$ gem install trollop

Now we have builtin “help” feature:

$ ruby ./testscript.rb --help
  -h, --hostname=    Server FQDN.
  -e, --help            Show this message

Now if we do:

$ ruby ./testscript.rb

gem – yaml

Yaml actually comes as part of the ruby core, therefore there is no ruby gem for it.

Lets say you have the following yaml file:

$ cat /etc/credentials.yaml
  :username: 'codingbee'
  :password: 'liverpool'

now you want use these content, then you do:

cat yamldemo.rb
require 'yaml'

credentials = YAML.load_file("/etc/credentials.yaml")
@username = credentials[:foreman][:username]
@password = credentials[:foreman][:password]

puts @username
puts @password

Which leads to:

$ ruby yamldemo.rb

Ruby – A hello world ruby script example

$ cat testscript.rb
#!/usr/bin/env ruby

a_string = "Hello World!"
puts a_string
puts a_string.class

The first line is slightly unusual because:

Next make it an executable:

$ chmod 777 testscript.rb

then run the script:

$ ./testscript.rb
Hello World!

RVM – Using rvmsudo instead of sudo

There are times when you need to run a ruby related command to install something, e.g. “bundle install” or “gem install gem-name”. If you are an rvm managed ruby version, then it is bad/messy practice to do this. because doing so will install the gem system-wide, rather then ruby version level. This all comes down to an environment variable called “rvm_path”. When using sudo, path no longer becomes available. That’s why to get around this, we use rvmsudo instead, which retains the user’s environment data while in privileged mode

[sher@puppetmaster ~]$ env | grep "^rvm_path"
[sher@puppetmaster ~]$ sudo env | grep "^rvm_path"      # notice that there's no match here.
[sher@puppetmaster ~]$ rvmsudo env | grep "^rvm_path"
[sher@puppetmaster ~]$

Hence the gem is installed system wide.

RVM – Deleting a Ruby version

Any gems that you install while using an RVM’s ruby version, is self contained in that version. However there may come a time when you no longer want to use a particular ruby version and want to delete it along with all it’s gems. Then this can be done using the “remove” command.

Let’s say you have:

[sher@puppetmaster ~]$ ruby --version
ruby 2.0.0p598 (2014-11-13 revision 48408) [x86_64-linux]
[sher@puppetmaster ~]$ rvm list

rvm rubies

   ruby-1.8.7-head [ x86_64 ]
   ruby-1.9.3-p551 [ x86_64 ]
=> ruby-2.0.0-p598 [ x86_64 ]
 * ruby-2.2.0 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

[sher@puppetmaster ~]$

Now let’s say we want to delete version 1.8.7, in that case we simply do:

[sher@puppetmaster ~]$ rvm remove 1.8.7
ruby-1.8.7-head - #removing src/ruby-1.8.7-head..
ruby-1.8.7-head - #removing rubies/ruby-1.8.7-head..
ruby-1.8.7-head - #removing gems....

RVM – Installing a version of ruby

You are now ready to install a version of ruby to run on top of RMV.

Before you install any new version of ruby to run on top of rvm, let’s first confirm that there are no versions of ruby running on top of rvm to begin with:

[sher@puppetmaster ~]$ rvm list

rvm rubies

# No rvm rubies installed yet. Try 'rvm help install'.

Note: the version of ruby that was on the machine before RVM was installed, is not managed by ruby. More about this later.

Now let’s see what versions of ruby are available to run on inside rvm:

[sher@puppetmaster bin]$ rvm list known | head -15
# MRI Rubies
[ruby-]1.8.7[-head] # security released on head

Now let’s install 2.2.0, which we do using the rvm “install” action (see “rvm help” for more info about

Ruby – Environment Variable

You can read all the environment variables via the irb using the “ENV” command:

irb(main):231:0* puts ENV.sort

Here the keys are in capital and the values are in string.

Alternatively we could use “pretty print” (pp) which comes with the Ruby standard library:

$ irb
irb(main):001:0> pp ENV.sort
NoMethodError: undefined method `pp' for main:Object
        from (irb):1
        from /usr/bin/irb:12:in `

This didn’t work because pp needs be required first:

irb(main):006:0* require 'pp'
=> true
irb(main):007:0> pp ENV.sort
[["COLUMNS", "188"],
 ["HISTCONTROL", "ignoredups"],
 ["HISTSIZE", "1000"],
 ["HOME", "/root"],
 ["HOSTNAME", "puppetmaster.local"],
 ["LANG", "en_GB.UTF-8"],
 ["LESSOPEN", "||/usr/bin/ %s"],
 ["LINES", "58"],
 ["LOGNAME", "root"],

Another option is to exit out of IRB and then install a gem called “awesome_print” and then use that tool instead:

$ gem install awesome_print
Fetching: awesome_print-1.6.1.gem (100%)
Successfully installed awesome_print-1.6.1
Parsing documentation for awesome_print-1.6.1
Installing ri