KO

EN

Ruby Cheatsheet

Ruby is a language with elegant syntax that is naturally readable and easy to write. Its philosophy is centered around human-centric design, giving it excellent readability, and the language itself is designed to be easy to use. It's also open-source and a pure object-oriented programming language, meaning that all data types such as integers and strings are objects.

  • VHLL (Very High-Level Language)
  • Pure OOP (Object-Oriented Programming)
  • Multiple Platforms

But in fact we need to focus on humans, on how humans care about doing programming or operating the application of the machines. Matz

# Greeter class
class Greeter
  def initialize(name)
    @name = name.capitalize
  end

  def salute
    puts "Hello #{@name}!"
  end
end

# Create a new object
g = Greeter.new("world")

# Output "Hello World!"
g.salute

๐Ÿ“œ Style Guide

https://rubystyle.guide/

๐Ÿ˜Ž Awesome Resources

๐ŸŽ› RVM

On macOS, Ruby is installed on the system by default. However, there are cases where you need to install on other OSes or need specific Ruby versions. In these cases, it's better to use a Version Manager like rvm or rbenv rather than installing directly on your system.

# Install RVM
curl -sSL https://get.rvm.io | bash -s stable

# Install/Use Ruby 3.0
rvm install 3.0.0
rvm use 3.0.0

# Set default ruby version
rvm use 3.3.0 --default

Note that sometimes during Ruby installation with RVM or after installation when installing gems, you might encounter OpenSSL-related errors. This issue occurs when the OpenSSL path is incorrectly specified during Ruby installation through RVM. It can be resolved by adding the --with-openssl-dir flag as follows:

rvm install 3.2.1 --with-openssl-dir=`brew --prefix openssl`

๐Ÿ’Ž Gem

Gem is Ruby's package manager. You can install, uninstall, or search for Ruby-based tools or libraries using the gem command.

# Install
gem install yaml

# Uninstall
gem uninstall yaml

# Search
gem search yaml

Gemfile and Bundler

It's difficult to manage application dependencies with individual gem commands. Therefore, you can manage multiple packages through Gemfile and Bundler.

source 'https://rubygems.org'

# Basic way to specify a gem
gem 'yaml'

# You can specify a version as the second argument
gem 'xspear', '1.4.1'

# You can also specify to install only certain versions of a gem or above
gem "haml-rails", "~> 0.3.4"

# You can specify require to prevent bundler from requiring the gem
gem 'rspec', :require => false

# Using source, you can also fetch gems from private gem servers
source 'https://your.private.gems.repo' do
  gem 'my_gem', :group => :development
end

This specified Gemfile can be installed or managed at once with bundler.

bundle install

Make Gem

Gem Structure

A Gem is a Ruby App packaged as a single package based on the contents specified in a .gemspec file. So you can build a Gem from any directory if you write a .gemspec file. However, most people create Gems by generating the Gem directory structure all at once using the Bundler command ($ bundle gem).

Gem::Specification.new do |spec|
  spec.name = "testapp"
  spec.version = Testapp::VERSION
  spec.authors = ["hahwul"]
  # ...
end
  • <APPNAME>.gemspec: Specifies general information and settings about the Gem
  • lib: Directory where library code goes
  • bin: Directory where execution code goes
    • bin/console: Code for IRB support.
    • bin/setup: Specifies code to process additionally when installing gem (e.g., 3rd-party installations).
    • bin/<NAME>: Files under bin other than console and setup represent commands that can be used after gem installation.

With bundler

# bundle gem <GEMNAME>
# --------------------
bundle gem testapp

# Creating gem 'testapp'...
# Initializing git repo in /Users/hahwul/nop/testapp
#       create  testapp/Gemfile
#       create  testapp/lib/testapp.rb
#       create  testapp/lib/testapp/version.rb
#       create  testapp/sig/testapp.rbs
#       create  testapp/testapp.gemspec
#       create  testapp/Rakefile
# .... snip ....
#       create  testapp/spec/spec_helper.rb
#       create  testapp/spec/testapp_spec.rb

Build

# Build
gem build testapp.gemspec

# Install
gem install ./testapp-0.0.1.gem

๐ŸŽฎ IRB

IRB (Interactive Ruby Shell) is a shell where you can write Ruby in real-time and check the results. It's provided by default, and you can use it to process simple tasks like calculations using Ruby and it's also extremely useful for real-time debugging.

irb

๐Ÿš€ Go-to Gems

Env

Concurrency

Utils

Terminal

Parser

Web

Log

Notify

CommandLine

Task

Articles

https://www.hahwul.com/tag/ruby/

References