Tidbits

This is my personal library I use to share the Ruby tidbits I made it for my various projects

Installation

Add this line to your application's Gemfile:

gem 'ruby-tidbits', git: 'https://github.com/konoplev-roman/ruby-tidbits'

And then execute:

$ bundle

Usage

Bool

To use bool extensions, add to the top of your file:

require 'tidbits/bool'

You can now use

  • .to_bool on NilClass, String, Numeric, FalseClass and TrueClass
  • .to_i for FalseClass and TrueClass

Example:

nil.to_bool
# => false

'yes'.to_bool
# => true

'no'.to_bool
# => false

1.to_bool
# => true

0.to_bool
# => false

false.to_bool
# => false

false.to_i
# => 0

false.to_bool
# => true

true.to_i
# => 1

Mysql2

Add to the top of your file:

require 'mysql2'
require 'tidbits/mysql2'

This code provides a set of extensions to the mysql2 Rubygem.

  • The result of the query is returned as an array.
  • The exception is returned with the sql query that caused the error.

Example:

# when valid sql
database.query 'SELECT * FROM users;'
# => [{ ... }, { ... }]

# when invalid sql
database.query 'SELECT * FROM user;'
# => Mysql2::Error (Table 'user' doesn't exist; SQL: SELECT * FROM user;)

Symbolize keys

Add to the top of your file:

require 'tidbits/symbolize_keys'

Recursive converts the keys of the hash to symbols

Example:

{ 'foo' => { 'bar' => { 'baz' => 1 } } }.symbolize_keys
# => { :foo => { :bar => { :baz => 1 } } }

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/konoplev-roman/ruby-tidbits. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Ruby::Tidbits project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.