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
๐ 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