IRB Rocks!

# Hack your code

Living outside Rails

Post image

Frameworks are powerful tools to develop software. However, they are dangerous. We get used to his classes, methods... and take them as property of the programming language they are written.

Rails isn't an exception. Welcome to the life outside Rails.

Gems

When we are writing a gem, we must have in mind if it's for general purpose or it has Rails as dependency.

It's typical to write a gem for general purpose and work with it on a Rails environment. That's a big problem because people that use our gem on another environment will get a lot of exceptions.

ActiveSupport

ActiveSupport is the responsible of utility classes and Ruby extensions. It's a gem so we can add it to non Rails projects, but I recommend you to add only functions you need.

Common methods of Rails that don't exist in Ruby are listed next.

Object

  • present?, blank?
  • as_json, to_json
  • try

From Object

Array

  • second, third, fourth, fifth
  • to_xml
  • split

From Array.

Hash

  • compact
  • to_xml, self.from_xml
  • except
  • symbolize_keys, stringify_keys
  • slice

From Hash.

Integer

  • multiple_of?

From Integer.

Numeric

  • Bytes methods: self.bytes, self.kilobytes...
  • Time methods: self.seconds, self.minutes, self.hours... (plus singular methods)

From Numeric.

String

  • first, last, at, from
  • to_date, to_time, to_datetime
  • remove
  • truncate
  • pluralize, singularize, constantize, camelize, humanize, demodulize
  • html_escape

From String.

Class

  • mattr_reader, mattr_writer, mattr_accessor

From Class.

Module

  • alias_method_chain (this method is deprecated)
  • delegate
  • deprecate

From Module.

Date

  • self.beginning_of_week, self.yesterday, self.tomorrow, self.current
  • beginning_of_day, middle_of_day, end_of_day
  • today?

From Date.

DateTime

  • self.current
  • beginning_of_day, middle_of_day, end_of_day
  • beginning_of_hour, end_of_hour, beginning_of_minute, end_of_minute

From DateTime.

Time

  • self.current
  • beginning_of_day, middle_of_day, end_of_day
  • beginning_of_hour, end_of_hour, beginning_of_minute, end_of_minute
  • zone, use_zone

From Time

gem share .gem