Ruby Study Plan — Devkit 프로젝트 기반
개요
dotfiles/devkit 프로젝트의 Ruby 코드를 읽으며 Ruby 핵심 개념을 학습하는 로드맵. 단계별로 난이도가 올라가며, 각 단계에서 배울 Ruby 개념을 정리했다.
1단계: 가장 간단한 파일 — RequiredComponent
[!tip] 목표: Ruby의 기본 문법과 클래스 구조에 익숙해지기
읽을 파일
lib/components/required_component.rb(19줄)lib/components/prerequisites/curl.rb(32줄)
배울 Ruby 개념
| 개념 | 설명 | 예시 |
|---|---|---|
require |
다른 파일 불러오기 | require "components/base" |
module / class |
Ruby의 기본 구조 단위 | module Component / class CurlComponent |
상속 (<) |
부모 클래스 기능 물려받기 | RequiredComponent < BaseComponent |
raise |
예외 발생시키기 | raise NotImplementedError |
| Backtick 실행 | 셸 명령 실행 후 출력 반환 | `curl --version` |
$? |
마지막 명령의 종료 상태 | $?.success? |
rescue |
예외 처리 | rescue Errno::ENOENT |
체크리스트
-
required_component.rb를 읽고, 추상 메서드 패턴 이해 -
curl.rb를 읽고,version,available?,download메서드 이해 - irb에서 backtick 명령 실행해보기 (
`ls -la`) - irb에서
begin/rescue/end블록 실험
2단계: Mixin(모듈) 동작 이해
[!tip] 목표: Ruby의 모듈 시스템과 Mixin 패턴 이해
읽을 파일
lib/mixins/loggable.rb(145줄)lib/mixins/configurable.rb(33줄)
배울 Ruby 개념
| 개념 | 설명 | 예시 |
|---|---|---|
module + include |
Mixin 패턴 (다중 상속 대안) | include Loggable |
Struct |
가벼운 데이터 클래스 | LogEntry = Struct.new(:message, :file) |
proc / 블록 |
함수형 프로그래밍 요소 | proc do |severity| ... end |
define_method |
메타프로그래밍 — 동적 메서드 생성 | define_method(level) do ... end |
tap |
객체 초기화 이디엄 | Logger.new($stdout).tap { |l| l.level = ... } |
||= |
Memoization 패턴 | @config ||= ... |
freeze |
상수를 불변으로 만듦 | COLORS = { ... }.freeze |
&. (Safe Navigation) |
nil 안전 메서드 호출 | caller_info&.path&.split("/") |
체크리스트
-
loggable.rb의BroadcastLogger클래스에서define_method동작 이해 -
Struct와 일반class의 차이점 정리 -
configurable.rb에서||=패턴이 왜 필요한지 이해 - irb에서 간단한 module을 만들고 class에
include해보기
3단계: 클래스 계층 구조
[!tip] 목표: 상속, 디자인 패턴, 접근 제어를 이해
읽을 파일
lib/components/base.rb(109줄)lib/components/installable_component.rb(177줄)lib/mixins/installable.rb(53줄)
배울 Ruby 개념
| 개념 | 설명 | 예시 |
|---|---|---|
| Template Method 패턴 | 부모가 흐름 정의, 자식이 각 단계 구현 | install! → pre_install → perform_install → ... |
Singleton |
인스턴스가 하나만 존재하도록 보장 | include Singleton |
self.inherited |
클래스 상속 시 자동 호출되는 훅 | 컴포넌트 Auto-discovery |
prepend vs include |
메서드 탐색 순서(MRO) 제어 | prepend Installable → super 호출 순서 변경 |
protected / private |
접근 제어 | protected def perform_install |
& (block 전달) |
블록을 명시적으로 받아서 전달 | def self.dependencies(&) |
instance_variable_get/set |
인스턴스 변수 동적 접근 | instance_variable_set("@#{name}", ...) |
클래스 다이어그램
BaseComponent (base.rb)
├── RequiredComponent (required_component.rb)
│ ├── CurlComponent
│ ├── GitComponent
│ ├── GithubComponent
│ └── TarComponent
└── InstallableComponent (installable_component.rb)
├── BatComponent
├── FzfComponent
├── RipgrepComponent
├── NeovimComponent
└── ...
체크리스트
-
base.rb의self.inherited훅이 어떻게 auto-discovery를 구현하는지 추적 -
installable.rb에서prepend와super의 관계 이해 (MRO) - Template Method 패턴:
install!의 흐름을 그림으로 그려보기 -
depends_on이define_method로 getter를 생성하는 과정 이해
4단계: 실전 컴포넌트 통째로 읽기
[!tip] 목표: 1~3단계 개념이 합쳐진 실제 코드를 완전히 이해
읽을 파일
lib/components/cli/bat.rb(123줄) — 메인 학습 대상lib/components/cli/fd.rb— 비교용lib/components/cli/ripgrep.rb— 비교용
핵심 포인트
- 의존성 선언:
depends_on Component::CurlComponent등 - TOML 설정 참조:
config.owner,config.repo,config.bin등 - Template Method 구현:
perform_install,post_installoverride - 버전 관리:
version,latest_version,version_tag,upgradable?
체크리스트
-
bat.rb의 설치 흐름을 처음부터 끝까지 추적 -
config/devkit.toml에서 bat 설정 찾아보기 -
bat.rb와fd.rb를 비교하여 공통 패턴과 차이점 정리 -
spec/lib/components/cli/bat_spec.rb테스트 코드 읽기
5단계: CLI 엔트리포인트
[!tip] 목표: 프로그램 진입점과 명령어 라우팅 이해
읽을 파일
bin/cli.rb(64줄)lib/commands/install_command.rblib/commands/update_command.rb
배울 Ruby 개념
| 개념 | 설명 | 예시 |
|---|---|---|
OptionParser |
CLI 옵션 파싱 (표준 라이브러리) | opts.on("-y", "--yes") |
case/when |
분기 처리 | case command when "install" |
if __FILE__ == $0 |
직접 실행 vs require 구분 | 스크립트 엔트리포인트 |
ARGV |
커맨드라인 인자 배열 | Runner.new.run(ARGV) |
체크리스트
-
cli.rb에서ruby bin/cli.rb install bat실행 시 코드 흐름 추적 -
OptionParser가-y,-f,--dry-run옵션을 처리하는 방식 이해 -
install_command.rb의execute메서드 흐름 파악
학습 팁
실험 환경
# irb로 Ruby 인터랙티브 실험
irb
# 프로젝트 테스트 실행
cd ~/Workspaces/dotfiles/devkit
bundle exec rspec
추천 학습 방법
- 테스트 먼저 읽기 —
spec/폴더의 테스트가 각 메서드의 사용법을 보여줌 - irb로 실험 — 새로운 개념을 만나면 irb에서 직접 타이핑
- 비슷한 파일 비교 —
bat.rb,fd.rb,ripgrep.rb를 나란히 놓고 패턴 파악 - 코드 따라 치기 — 읽기만 하지 말고 간단한 컴포넌트를 직접 만들어보기
주요 Ruby 문서
- Ruby 공식 문서
- Ruby Style Guide
- Ruby Koans — 인터랙티브 학습
이 프로젝트에서 배울 수 있는 디자인 패턴 정리
| 패턴 | 위치 | 설명 |
|---|---|---|
| Template Method | installable_component.rb |
install!이 흐름 골격을 정의 |
| Singleton | base.rb |
컴포넌트당 인스턴스 하나 |
| Mixin | mixins/ |
Loggable, Configurable, Installable |
| Registry | commands/utils/registry.rb |
Auto-discovery로 컴포넌트 등록 |
| Strategy | install_resources |
resource type에 따라 다른 설치 전략 |
| Dependency Injection | depends_on |
의존성을 선언적으로 관리 |