PROGRAMMING RUBY
PROGRAMMING RUBY : THE PRAGMATIC PROGRAMMER S GUIDE, 2nd Ed. Copyright c 2005 Published in the original in the English language by The Pragmatic Programmers, LLC, Lewisville. All rights reserved. Korean Translation Copyright c by Insight Press. 이책의한국어판저작권은저작권자와의독점계약으로인사이트에있습니다. 신저작권법에의해한국내에서보호를받는저작물이므로무단전재와무단복제를금합니다. 프로그래밍루비 PROGRAMMING RUBY 초판 PDF 1.0 2012년 5월 31일지은이데이브토머스 차드파울러 앤디헌트옮긴이강문식 박지인 양석호펴낸이한기성펴낸곳인사이트등록번호제10-2313호등록일자 2002년 2월 19일주소서울시마포구서교동 469-9번지석우빌딩 3층전화 02-322-5143 팩스 02-3143-5579 블로그 http://insightbook.co.kr 이메일 insight@insightbook.co.kr ISBN 978-89- 6626-032-4
PROGRAMMING RUBY
PROGRAMMING RUBY
P r o g r a m m i n g R u b y
1 % ruby -v ruby 1.8.2 (2004-08-24) [powerpc-darwin7.5.0] 1
1.8
class SampleCode def run #... end end 1 a = 1 b = 2 a + b 3 1
a = 1 1 b = 2 2 a + b 3 3.times { puts Hello! } : Hello! Hello! Hello! ruby [... ] [ ] [... ]
1.8
PROGRAMMING RUBY
1 P r o g r a m m i n g R u b y
# apt-cache search ruby interpreter libapache-mod-ruby - Embedding Ruby in the Apache web server liberb-ruby1.6 - Tiny eruby for Ruby 1.6 liberb-ruby1.8 - Tiny eruby ruby - An interpreter of object-oriented scripting language Ruby ruby1.7 - Interpreter of object-oriented scripting language Ruby ruby1.8 - Interpreter of object-oriented scripting language Ruby # apt-get install ruby1.8 Reading Package List... Done Building Dependency Tree... Done The following extra packages will be installed: libruby1.8 Suggested packages: ruby1.8-examples The following NEW packages will be installed: libruby1.8 ruby1.8
1. 2. 3.
% tar xzf snapshot.tar.gz ruby/ ruby/bcc32/ ruby/bcc32/makefile.sub ruby/bcc32/readme.bcc32 1 1
2 % cvs -z4 -d :pserver:anonymous@cvs.rubylang.org:/src login (Logging in to anonymous@cvs.rubylang.org) CVS password: % cvs -z4 -d :pserver:anonymous@cvs.rubylang.org:/src checkout ruby 2
% ruby puts Hello, world! ^D Hello, world! % irb irb(main):001:0> def sum(n1, n2) irb(main):002:1> n1 + n2 irb(main):003:1> end => nil irb(main):004:0> sum(3, 4) => 7 irb(main):005:0> sum( cat, dog ) => catdog % irb irb(main):001:0> load code/rdoc/fib_example.rb => true irb(main):002:0> Fibonacci.upto(20) => [1, 1, 2, 3, 5, 8, 13]
% ruby myprog.rb 3 #!/usr/local/bin/ruby -w puts Hello, world! %./myprog.rb Hello, world! 3
% ri GC ------------------------------------------------------------Class: GC The GC module provides an interface to Ruby s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module. -------------------------------------------------------------------- Class methods: disable, enable, start Instance methods: garbage_collect % ri enable ----------------------------------------------------------GC::enable GC.enable => true or false -------------------------------------------------------------------- Enables garbage collection, returning true if garbage collection was previously disabled. GC.disable GC.enable GC.enable #=> false #=> true #=> false
% ri start More than one method matched your request. You can refine your search by asking for information on one of: Date#new_start, Date#start, GC::start, Logger::Application#start, Thread::start % ri GC.start -------------------------------------------------------------GC::start GC.start => nil gc.garbage_collect => nil ObjectSpace.garbage_collect => nil -------------------------------------------------------------------- Initiates garbage collection, unless manually disabled. % export RI= --format ansi --width 70
2 P r o g r a m m i n g R u b y 1 1
song1 = Song.new( Ruby Tuesday ) song2 = Song.new( Enveloped in Python ) #.
2 gin joint.length 9 Rick.index( c ) 2-1942.abs 1942 sam.play(song)... 2
number = Math.abs(number) // number = number.abs def say_goodnight(name) result = Good night, + name return result end # puts say_goodnight( John-Boy ) puts say_goodnight( Mary-Ellen )
Good night, John-Boy Good night, Mary-Ellen puts say_goodbye( John-Boy ) puts(say_goodbye( John-Boy ))
puts And Goodnight, \ngrandma : And Goodnight, Grandma def say_goodnight(name) result = Goodnight, #{name} return result end puts say_goodnight( Pa ) : Good night, Pa