Ruby 아샬

Ruby는 무엇인가?

간결함과 생산성을 강조한 동적인 오픈 소스 프로그래밍 언어입니다.

루비의 우아한 문법으로 자연스럽게 읽고 쓰기 편한 프로그램을 만들수 있습니다.

-출처: http://www.ruby-lang.org/ko/

자세한 내용은 공식 홈페이지 소개글을 참고하세요. :)

관련 사이트

튜토리얼 내용

해커에게 즐거운 프로그래밍 언어 루비를 간단히 소개하고, 직접 코드를 실행하며 생각하는 과정을 통해 루비를 느끼고, TDD와 함께 좀 더 아름다운, 좀 더 인간다운 코드를 만들어가는 경험을 합니다.

준비할 것

    1. 열린 마음

    2. 해커가 되고픈 열정

    3. 그리고, 이 모든 마음과 열정을 실천으로 옮길 수 있는 노트북 (루비를 미리 설치해 오시면 사랑합니다 ㅋ)

튜토리얼 자료

irb에서 다음을 입력합니다.

    1. => 다음에 나오는 결과는 어떤 의미일까요?

    2. 각각의 문장은 어떤 의미일까요? (작게 분해해서 실행해 보세요!)

    3. 계속 변형해서 실험해 보세요. (상상력을 자극하고, 현실로 끌어내세요!)

실행 순서가 중요한 것도 있습니다. 영 아니다 싶으면 과감히 exit를 때리고 다시 시작하세요.

674

"Hello, world"

puts "Hello, world"

1 + 2

3 * 4

"abc" + "def"

"abc" * 3

[1, 2, 3] * 3

[1, 2, 3] + [3, 4]

[1, 2, 3] - [3, 4]

[1, 2, 3] & [3, 4]

[1, 2, 3] | [3, 4]

a = 1

a

puts a

a += 1

b = "test"

b += "..."

b << "!!!"

c = [1, 2, "3"]

c << "hello"

c += ["world"]

c[0]

c[10]

c[-1]

c[0..1]

c[1...3]

b[1]

b[1..2]

d = {:b => 3, :a => "test"}

d[:a]

d[:c]

d[1] = "ok!"

d["HAHA"] = "d[:a] is #{d[:a]}"

d

1 == 1

1 == 2

1 != 1

a = 1 == 1

b = 3 < 1

123.class

"abc".class

[1, 2, 3].class

{:b => 3, :a => "test"}.class

true.class

false.class

nil.class

a = (1 + 10).class

b = (1 == 1).class

(a.class == Array).class

if 2 == 1 + 1

"You're a genius!"

end

a = if 1 == 1 + 1

"O"

else

"X"

end

a = 1 if 1 == 0

a

a = 2 if 1 == 1

a

def one

1

end

one

def two(x)

x * 2

end

two(10)

two 10

def three x

x * 3

end

a = three 8

def four(x=1)

x * 4

end

four

four(2)

def human(options={})

[options[:sex], "#{options[:name]} (#{options[:age]})"]

end

human({:name => "Sam", :age => 12, :sex => "?!"})

human(:name => "Sam", :age => 12, :sex => "?!")

human :name => "Sam", :age => 12, :sex => "?!"

def sex(sex)

case sex

when "M"

"man"

when "F"

"woman"

else

"etc"

end

end

sex("M")

sex("F")

sex(1234)

3.times{|i| puts i}

(0...3).each{|i| puts i}

(0..2).each{|i| puts i}

0.upto(2){|i| puts i}

[0, 1, 2].each{|i| puts i}

[1, 3, 4, 2, 5].sort

[1, 3, 4, 2, 5].sort.reverse

[1, 3, 4, 2, 5].sort{|a, b| b <=> a}

(0...10).map{|i| i * 2}

(0...10).select{|i| i % 2 == 0}

(0...10).inject{|a, b| a + b}

"123".to_i * 2

123.to_s * 2

"abcdef".split("").join(",")

%w[abc ABC 123].sort

class MyClass

def one

1

end

end

my_instance = MyClass.new

my_instance.one

my_instance.send(:one)

class MyClass

def two(x)

temp = x * 2

end

end

MyClass.new.one

MyClass.new.two(3)

class MyClass

def two(x)

2

end

end

MyClass.new.two(3)

class Man

def initialize(name)

@name = name

end

def name

@name

end

def name=(name)

@name = name

end

end

boy = Man.new("Ashal")

boy.name

boy.name = "Superman"

boy.name

class Woman

attr_accessor :name

def initialize(options)

@name = options[:name]

@name = options[:sex]

end

end

girl = Woman.new :name => "Yuko Ogura", :sex => "F"

girl.name

girl.name = "Yunha"

girl.sex

Woman.instance_methods

class Array

def sum

inject{|a, b| a + b}

end

end

module ZeroSum

def sum

0

end

end

class MyClass

include ZeroSum

end

[1, 2, 3].sum

[1, 2, 3].extend(ZeroSum).sum

MyClass.new.sum

require 'test/unit'

class TestFibo < Test::Unit::TestCase

def test_fibo_one

assert_equal 1, fibo(1)

end

end

irb에서 실행.

require 'test/unit/ui/console/testrunner'

Test::Unit::UI::Console::TestRunner.run(TestFibo)

콘솔에서 실행.

ruby test.rb

문제!

끝까지 오신 분은...

언어 튜토리얼 페이지에 댓글을 남겨주세요!