Ruby

Ruby Cheatsheet

๐Ÿ” Introduction

Ruby๋Š” ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ฝํžˆ๊ณ  ์“ฐ๊ธฐ ์‰ฌ์šด ์šฐ์•„ํ•œ ๋ฌธ๋ฒ•์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ์ฒ ํ•™ ์ž์ฒด๊ฐ€ ์ธ๊ฐ„ ์ค‘์‹ฌ์˜ ์„ค๊ณ„๋‹ค ๋ณด๋‹ˆ ๋›ฐ์–ด๋‚œ ๊ฐ€๋…์„ฑ์„ ๊ฐ€์กŒ๊ณ  ์–ธ์–ด ์ž์ฒด๋„ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ณ ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์˜คํ”ˆ์†Œ์Šค์ด๋ฉฐ ์ˆœ์ˆ˜ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ •์ˆ˜, ๋ฌธ์ž์—ด ๋“ฑ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ํ˜•์‹์€ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

  • VHLL (Very High-Level Lanauge)
  • 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 Greeter
  def initialize(name)
    @name = name.capitalize
  end

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

# ์ƒˆ ๊ฐ์ฒด ์ƒ์„ฑ
g = Greeter.new("world")

# "Hello World!" ์ถœ๋ ฅ
g.salute

๐Ÿ“œ Style Guide

https://rubystyle.guide/

๐Ÿ˜Ž Awesome Resources

๐ŸŽ› RVM

Macos์˜ ๊ฒฝ์šฐ ๊ธฐ๋ณธ์ ์œผ๋กœ Ruby๊ฐ€ ์‹œ์Šคํ…œ์— ์„ค์น˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋‹ค๋ฅธOS๋‚˜ ํŠน์ • ๋ฃจ๋น„ ๋ฒ„์ „ ์„ค์น˜๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด ๋•Œ๋Š” ์ง์ ‘ ์‹œ์Šคํ…œ์— ์„ค์น˜ํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค rvm์ด๋‚˜ rbenv ๊ฐ™์€ Version Manager๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

# 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

์ฐธ๊ณ ๋กœ ์ข…์ข… RVM์œผ๋กœ Ruby ์„ค์น˜ ๊ณผ์ • ๋˜๋Š” Ruby ์„ค์น˜ ์ดํ›„ Gem ์„ค์น˜ ์ค‘ OpenSSL ๊ด€๋ จ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” RVM์„ ํ†ตํ•ด Ruby๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ณผ์ •์—์„œ OpenSSL ๊ฒฝ๋กœ๊ฐ€ ์ž˜๋ชป ์ง€์ •๋˜๋ฉด ๋ฐœ์ƒํ•˜๋Š” ์ด์Šˆ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด --with-openssl-dir flag๋ฅผ ์ฃผ์–ด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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

๐Ÿ’Ž Gem

Gem์€ Ruby์˜ ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ์ž์ž…๋‹ˆ๋‹ค. gem ๋ช…๋ น์„ ํ†ตํ•ด ์›ํ•˜๋Š” ๋ฃจ๋น„ ๊ธฐ๋ฐ˜์˜ ๋„๊ตฌ๋‚˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜/์‚ญ์ œ/๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# ์„ค์น˜
gem install yaml 

# ์‚ญ์ œ
gem uninstall yaml

# ๊ฒ€์ƒ‰
gem search yaml

Gemfile and Bundler

์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์˜์กด์„ฑ์„ ๋งค๋ฒˆ gem ๋ช…๋ น ํ•˜๋‚˜๋กœ ๊ด€๋ฆฌํ•˜๊ธด ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ Gemfile๊ณผ Bundler๋ฅผ ํ†ตํ•ด ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํŒจํ‚ค์ง€๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

source 'https://rubygems.org'

# ๊ธฐ๋ณธ์ ์ธ gem ๋ช…์‹œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
gem 'yaml' 

# ๋‘๋ฒˆ์งธ ์ธ์ž ๊ฐ’์œผ๋กœ ๋ฒ„์ „์„ ๋ช…์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 
gem 'xspear', '1.4.1' 

# ํŠน์ • ๋ฒ„์ „ ์ด์ƒ์˜ gem๋งŒ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ช…์‹œํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
gem "haml-rails", "~> 0.3.4" 

# require๋ฅผ ์ง€์ •ํ•ด์„œ bundler๊ฐ€ gem์„ requireํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
gem 'rspec', :require => false 

# source๋ฅผ ์ด์šฉํ•˜๋ฉด ์‚ฌ์„ค gem ์„œ๋ฒ„ ๋“ฑ์—์„œ๋„ gem์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
source 'https://your.private.gems.repo' do
  gem 'my_gem', :group => :development
end

์ด๋ ‡๊ฒŒ ๋ช…์‹œํ•œ Gemfile์€ bundler๋กœ ํ•œ๋ฒˆ์— ์„ค์น˜ํ•˜๊ฑฐ๋‚˜ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

bundle install

Make Gem

Gem Structure

.gemspec ํŒŒ์ผ์— ๋ช…์‹œ๋œ ๋‚ด์šฉ์„ ๊ธฐ๋ฐ˜์œผ๋กœ Ruby App์„ ํ•˜๋‚˜์˜ ํŒจํ‚ค์ง€๋กœ ๋ฌถ์—ฌ์ง„ ๊ฒƒ์„ Gem์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ .gemspec ํŒŒ์ผ๋งŒ ์ž‘์„ฑํ•˜๋ฉด ์–ด๋–ค ๋””๋ ‰ํ† ๋ฆฌ์—์„œ๋„ Gem์„ ๋นŒ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ ๋Œ€๋ถ€๋ถ„์€ Gem์„ ๋งŒ๋“ค๋•Œ๋Š” Bundler($ bundle gem) ๋ช…๋ น์œผ๋กœ Gem ๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ํ•œ๋ฒˆ์— ์ƒ์„ฑํ•˜์—ฌ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

Gem::Specification.new do |spec|
  spec.name = "testapp"
  spec.version = Testapp::VERSION
  spec.authors = ["hahwul"]
  # ...
end
  • <APPNAME>.gemspec: Gem์— ๋Œ€ํ•œ ์ „๋ฐ˜์ ์ธ ์ •๋ณด์™€ ์„ค์ •์„ ๋ช…์‹œ
  • lib: ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๋””๋ ‰ํ† ๋ฆฌ
  • bin: ์‹คํ–‰์„ ์œ„ํ•œ ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๋””๋ ‰ํ† ๋ฆฌ
    • bin/console: IRB ์ง€์›์„ ์œ„ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
    • bin/setup: gem install ์‹œ ์ถ”๊ฐ€์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์ค„ ์ฝ”๋“œ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. (์˜ˆ๋ฅผ๋“ค์–ด 3rd-party ์„ค์น˜ ๋“ฑ)
    • bin/<NAME>: bin ํ•˜์œ„์— console, setup ์ด์™ธ ํŒŒ์ผ์€ gem ์„ค์น˜ ํ›„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ช…๋ น์–ด๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

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)์€ Ruby๋ฅผ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ž‘์„ฑํ•˜๊ณ  ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์‰˜์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณต๋˜๋ฉฐ ์ด๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๊ณ„์‚ฐ ์ž‘์—… ๋“ฑ ๊ฐ„๋‹จํ•œ ์ž‘์—…์— ๋ฃจ๋น„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ณ  ์‹ค์‹œ๊ฐ„ ๋””๋ฒ„๊น…์—๋„ ๊ต‰์žฅํžˆ ์šฉ์ดํ•ฉ๋‹ˆ๋‹ค.

irb

๐Ÿš€ Go-to Gems

Env

Concurrency

Utils

Terminal

Parser

Web

Log

Notify

CommandLine

Task

๐Ÿ“š Articles

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

๐Ÿ“Œ References