DEV Community

Cover image for How to Use Rails Magic Methods in Plain Ruby Scripts
Zil Norvilis
Zil Norvilis

Posted on • Originally published at norvilis.com

How to Use Rails Magic Methods in Plain Ruby Scripts

Very often I find myself writing small, standalone Ruby scripts. Maybe it's a web scraper, a small background worker, or a quick Sinatra API.

I start typing out my code, and naturally, I write something like this:

if my_variable.present?
  puts "We have data!"
end
Enter fullscreen mode Exit fullscreen mode

And immediately, my script crashes with NoMethodError: undefined method 'present?' for nil:NilClass.

This is the moment every Ruby developer realizes a shocking truth: Methods like .present?, .blank?, and 3.days.ago are NOT part of the Ruby language. They are part of Rails.

But what if you want to use these awesome methods without generating a massive, heavy Rails application?

You can. All of these "magic" methods live inside a single gem called ActiveSupport. You can easily extract it and use it in any plain Ruby script. Here is how to do it.

STEP 1: The Setup

First off, let's install the gem. If you are using a Gemfile for your script, just add this:

source 'https://rubygems.org'

gem 'activesupport'
Enter fullscreen mode Exit fullscreen mode

Run bundle install. If you are just writing a single .rb file without a Gemfile, you can install it directly in your terminal by running gem install activesupport.

STEP 2: The "All In" Approach

Now open your ruby script. The easiest way to get all the Rails magic is to require the entire ActiveSupport library at the very top of your file.

# script.rb
require 'active_support/all'

# Now you can use time helpers!
puts 3.days.ago

# You can check for blank arrays or strings!
empty_array =[]
puts empty_array.blank? # => true

# You can format strings!
puts "my_custom_class".camelize # => "MyCustomClass"
Enter fullscreen mode Exit fullscreen mode

This is great, but there is a catch. active_support/all is huge. It loads thousands of methods into memory. If you are building a tiny, fast script, loading the entire library just to use .blank? is overkill and will make your script boot up much slower.

STEP 3: The "Cherry-Picking" Approach (Recommended)

Instead of loading everything, ActiveSupport allows you to require only the specific extensions you actually need.

ActiveSupport groups its methods by the Ruby class they extend (like String, Integer, Date, Array, etc.).

Here is how you cherry-pick exactly what you want:

# script.rb

# 1. I only want .blank? and .present?
require 'active_support/core_ext/object/blank'

if "".blank?
  puts "String is empty"
end

# 2. I only want the time calculation helpers (like 2.days.from_now)
require 'active_support/core_ext/integer/time'
require 'active_support/core_ext/numeric/time'

puts 2.weeks.from_now

# 3. I only want string manipulations (like .squish or .pluralize)
require 'active_support/core_ext/string'

puts "  too   much   spacing  ".squish # => "too much spacing"
puts "apple".pluralize               # => "apples"
Enter fullscreen mode Exit fullscreen mode

By doing this, your script boots up instantly, uses almost zero RAM, but you still get to use your favorite Rails helpers.

My Top 3 ActiveSupport Methods

If you are wondering what else lives inside ActiveSupport, here are a few methods I use all the time in my standalone scripts:

1. Array .in_groups_of
Perfect for processing large lists of data in batches.

require 'active_support/core_ext/array/grouping'

users = [1, 2, 3, 4, 5, 6]
users.in_groups_of(2) do |group|
  puts group.inspect
end
# Outputs:[1, 2], then [3, 4], then [5, 6]
Enter fullscreen mode Exit fullscreen mode

2. Hash .with_indifferent_access
Have you ever tried to get a value from a hash using my_hash[:name] but the key was actually a string "name"? This fixes that annoyance completely.

require 'active_support/core_ext/hash/indifferent_access'

data = { "name" => "Zil" }.with_indifferent_access
puts data[:name] # => "Zil" (It works with a symbol too!)
Enter fullscreen mode Exit fullscreen mode

3. Enumerable .pluck
If you have an array of hashes (like an API response), you can easily extract just one specific key from all of them.

require 'active_support/core_ext/enumerable'

api_response =[{ id: 1, name: "Zil" }, { id: 2, name: "John" }]
puts api_response.pluck(:name) 
# Outputs: ["Zil", "John"]
Enter fullscreen mode Exit fullscreen mode

Summary

ActiveSupport is basically the ultimate utility belt for Ruby.

When you learn how to use it outside of Rails, you realize that Rails isn't just one big magic black box. It is a collection of really well-written, separate tools. Pulling activesupport into your plain Ruby scripts will save you from writing hundreds of lines of custom helper methods.

That's pretty much it. Next time you write a web scraper or a background worker, don't suffer with plain Ruby time calculations. Just require ActiveSupport.

Top comments (0)