Ruby는 1995년 마츠모토 유키히로(Matz)가 만든 순수 객체지향 동적 언어다. "개발자의 행복"을 철학으로 가독성과 생산성을 극대화했다. Ruby on Rails 프레임워크로 웹 개발에 널리 사용된다.
기본 문법
ruby
# 변수와 출력
name = "Alice"
age = 30
puts "Hello, #{name}! You are #{age} years old."
# 배열
fruits = ["apple", "banana", "cherry"]
fruits.each { |fruit| puts fruit }
fruits.map { |f| f.upcase } # ["APPLE", "BANANA", "CHERRY"]
fruits.select { |f| f.length > 5 } # ["banana", "cherry"]
# 해시
person = { name: "Alice", age: 30 }
puts person[:name] # Alice
클래스
ruby
class Animal
attr_reader :name
def initialize(name)
@name = name # 인스턴스 변수
end
def speak
"..."
end
end
class Dog < Animal
def speak
"Woof!"
end
end
dog = Dog.new("Buddy")
puts dog.speak # Woof!
Ruby on Rails 예시
ruby
# Controller
class UsersController < ApplicationController
def index
@users = User.all
end
def create
@user = User.new(user_params)
if @user.save
redirect_to @user
else
render :new
end
end
end
관련 개념
참고문헌
- •Thomas, D. Programming Ruby (Pickaxe Book)
- •Ruby 공식 문서: ruby-lang.org