All learnings about "ruby"

…that you can use a .gemrc file to not install documentation.

Everyone loves [Rubygems][Rubygems]. And you probably have heard of the --no-rdoc & --no-ri flags, right? The flags that fasten up your gem installation since you do not have to generate the RDoc and ri documentation? Ah, I see you also have heard about it. Well, I figured those flags are now deprecated. Time to replace them with new stuff. It is called --no-document now.

Best thing to add it to your ~/.gemrc file (and create one if you don’t have one yet). Create a ~/.gemrc file like this:

gem: --no-document

Now you don’t have to worry about this anymore.

…that there is the only flag for rubocop.

Rubocop is a great tool to keep you code consistent, sane and informs you about some performance pitfalls.

Lately, I wanted to slowly add rubocop to an existing codebase. Rubocop has the -a parameter – which is short for --auto-correct – which corrects all the offenses. Having a thousand offenses and trying to auto fix them all, will generate quite the Pull Request. But there is also the --only parameter that allows you to check (and fix) only some or one type of offense.

So a neat way to add all the missing magic frozen_string_literal: true comments everywhere, might look like:

rubocop --only Style/FrozenStringLiteralComment -a

…how to add functionality to existing rake tasks.

Sometimes you just want to add some extra functionality to some existing rake task.

So image having these rake tasks defined:

namespace :do do
  task :foo do
    puts "foo"

  task :after do
    puts "after"

  task :before do
    puts "before"

We can prepend the do:before task to the do:foo task like this:

Rake::Task["do:foo"].enhance ["do:before"]

# output would be:
# before
# foo

And let something run after a task like this:

Rake::Task["do:foo"].enhance do

# output would be:
# foo
# after

Really neat if you want to add some more stuff, to existing rails tasks for example. I got that idea from here, btw.

…bundler can be used without a gemfile.

I didn’t know that up until now, but obviously, you can inline bundler within your ruby scripts. No need to have a dedicated Gemfile or to call bundler before calling your script. Just do:

require 'bundler/inline'

gemfile do
  source ''
  gem 'rest-client'

puts RestClient.get('')

You can see the source on their GitHub repo. This becomes espescially interesting not only for small scripts, but also to have small executable files, that behave like a regular executable.

A small example

Image being able to do this from your command line:

$ weather berlin
Today it is -2.51°C in Berlin with few clouds.

Easily done:

#!/usr/bin/env ruby
require 'bundler/inline'

gemfile do
  source ''
  gem 'json'
  gem 'rest-client'

API_KEY = 'XXX' # put your api key here
city = ARGV.count > 0 ? ARGV.first : 'Hamburg,de'

body = RestClient.get("{city}&appid=#{API_KEY}&units=metric")

response = JSON.parse(body)

temp = response['main']['temp']
city = response['name']
weather = response['weather'][0]['description']

puts "Today it is #{temp}°C in #{city} with #{weather}."

Put this into a weather file, make it exectuable chmod +x weather and then put it into your PATH variable.