1.1 P絟
1.2 ¦ó¿×Ruby¡H
1.3 Åý§Ú¬Ý¨Ç¥ÎRuby¼g¦¨ªºµ{¦¡½X
1.4 ¬°¦ón¥s¡uRuby¡v¡H
1.5 Rubyªº¾ú¥v
1.6 Rubyªº¥D¶¦bþ¡H
1.7 ¦³µLRubyªº·s»D²Õ¡H
1.8 ¦³µLRubyªº¶l»¼½×¾Â¡H
1.9 RubyÁÙ¬Oruby¥¿½T¡H
1.10 ¦³µL¦³ÃöRubyªº®Ñ¡H
1.11 ¬Æ»ò½s¿è¾¹¤ä´©Ruby¡H
1.12
1.13
1.14 ¬Æ»ò¸ê®Æ¾A¦X·sRubyµ{¦¡û¡H
1.15
1.16 Ruby¬Oª½Ä¶ÁÙ¬O½sĶªº¡H
1.17
1.18
1.19 ½Ö¥i¦^µª
ìµÛ¡J«e¥Ð×§^( shugo{at}netlab.co.jp)¡C
²{¦b¥ÑDave Thomas¡]dave{at}pragmaticprogrammer.com ¡^ºûÅ@¡C¦hÁÂgotoken¡] gotoken{at}math.sci.hokudai.ac.jp¡^»s§@¤Fì¨Óªº^Ķ¥»¡A¥t¤]¦hÁÂDell¡¦Aquila¡BRobert Gustavsson Clemens Hintze¡BJosh Huber¡BH Morita¡BAleksi Niemela¡BHugh Sasse¡BConrad Schneiker¡BLarry W. Virden¡BJim Weirich¡A·íµM¤Ö¤£¤FMatz¡CRuby¬OÓ²{¥Nªºª«¥ó¾É¦V»y¨¥¡A ´|¦X¤FPerl¡BSmalltalk©MSchemeªº¤¸¯À©ó¤@Ó²³æ±j¤jªº»yªk¤§¤¤¡C³o¥÷¤å¥ó¼g¤F¤@¨Ç¸g±`³Q°Ý¨ì¦³ÃöRubyªº°ÝÃD©Mµª®×¡C³oùتºµ{¦¡½X¬O¥i¥H¥ÎRuby 1.6.8ª©¥»°õ¦æ¡C³o¥÷¤å¥óªºªk¤åª©¥i¥H¦brubyfr.org§ä¨ì¡C
Ruby¬OÓ²¼ä±j¤jªºª«¥ó¾É¦Vµ{¦¡»y¨¥ ¡A¥Ñまつもと ゆきひろ(Yukihiro Matsumoto)¡]¦b³o¥÷¤å¥ó©M¶l»¼½×¾Â¤¤¨Ï¥Î¡umatz¡v³oÓ¥~¸¹¡^¡C
¦PPerl¤@¼Ë¡ARuby¦³¨}¦nªº¤å¦r³B²z¯à¤O¡C¦n¹³Smalltalk¡A¦b RubyùØ¡A¤@¤Á¬Ò¬Oª«¥ó¡A¥t¥~Ruby ¦³°Ï¶ô(blocks), iterators, meta-classes ©M ¨ä¥L¦nªF¦è¡C
§A¥i¥H¥ÎRuby¨Ó¼g¦øªA¾¹¡Bexperiment with prototypes, ©M©Ò¦³¤é±`À³¥Î¡C§@¬°¤@Ófully-integrated object-oriented language, Ruby scales well.
Ruby ¯S¦â¡J
Åý§ÚÌ©w¸q¤@ÓÃþ§O¥sPerson(¤H)¡A¦³name(¦W)©Mage(¦~ÄÖ)¡C§ÚÌ·|³]p¤@¨Ç¤HµM«á¤ÀªR¥LÌ¡A¨Ó´ú¸Õ³o¨Çµ{¦¡½X¡C
class Person
attr_accessor :name, :age
def initialize(name, age)
@name = name
@age = age.to_i
end
def inspect
"#@name (#@age)"
end
end
p1 = Person.new('elmo', 4)
p2 = Person.new('zoe', 7)
puts p1
puts p2
Now let¡¦s populate an array of people by reading their names and ages from a file containing lines like:
bert: 8 cookie: 11 elmo: 4 ernie: 8 zoe: 7
³o¨Çµ{¦¡½X¨Ï¥Î¥¿³Wªí¥Ü¦¡¨ÓThe code uses regular expressions to parse successive lines from the input file, creating a new Person object for each match and pushing it on to the end of the array people.
people = Array.new
File.foreach("ages") do |line|
people << Person.new($1, $2) if line =~ /(.*):\s+(\d+)/
end
people # -> [bert (8), cookie (11), elmo (4), ernie (8), zoe (7)]
²{¦b¨Ì¤H̪º¦~ÄֱƦn§Ú̪ºµ²ªG¡A¦³«Ü¦h¤èªk¥i¥H¿ì¨ìªº¡C§ÚÌ¥i¥H©w¸q¤@ӱƧǪº°Ï¶ô¡A§i¶DRuby ¦p¦ó¤ñ¸û¨âÓ¤Hªº¥ý«á¦¸§Ç¡C
sorted = people.sort { |a,b| a.age <=> b.age }
sorted # -> [elmo (4), zoe (7), bert (8), ernie (8), cookie (11)]
¥t¤@Ó¤èªk¬O´ÀÃþ§O§ïÅÜ¡u¤ñ¸û¡vªºmethod Person:
class Person
def <=>(other)
@age <=> other.age
end
end
people.sort
¦bPerlªº¼vÅT¤U¡AMatz·Q¥Î¤@ÓÄ_¥Ûªº¦W¦r§@¥Lªº·sµ{¦¡»y¨¥ªº¦W¦r©ó¬O¥L¨Ìa colleague's birthstone©R¦WRuby¡C
¨ä«á¥Lµo²{Ruby¦b«Ü¦hÀô¹Ò¤U³£±µµÛPerlªº¦ì¸m¡C ¬Ã¯](Perlì¦W¬OPearl)¬O¤»¤ëªº½Ï¥Í¥Û¡A¬õÄ_¥Û(ruby)«h¬O¤C¤ë¡C pºâ¦r«¬¤j¤pªº¸Ü¡Apearl¬O5pt¡Aruby¬O5.5pt¡C ¤@Ó¤ñPerl¸û·s¡]¦P®É§Æ±æ¸û¦n¡^ªºµ{¦¡»y¨¥¡AMatz»{¬°Ruby¬OÓ¦n¦W¦r¡C
(Based on an explanation from matz in [ruby-talk:00394] on June 11, 1999.)
Rubyªº©x¤èºô¯¸¬O www.ruby-lang.org(^¤å)©M www.ruby-lang.org/ja(¤é¤å)¡C
§A¥ç¥i¥H¦bwww.rubycentral.com§ä¨ìRubyªº¸ê®Æ¡C ¯S§Oªº¬O¡A§A¥i¥H¦bwww.rubycentral.com/book§ä¨ìProgramming Ruby³o¥»®Ñªº½u¤Wª©¥»¡C
Rubygarden¡Awww.rubygarden.com¬O¥t¤@ÓÃö©óRubyªº¦nºô¯¸¡A¯S§O¤§³B¬O¦³Rubyªº·s®ø®§©MWiki¡C
¦b¤G¹s¹s¹s¦~¤¤ëcomp.lang.ruby¥X²{¤F(±on¦hÁ Conrad Schneikerªº§V¤O)¡C
¼È®É¦³¤Ó°Q½×Rubyªº¶l»¼½×¾Â¡C²Ä¤@Ó¬O^¤åªº¡A¨ä¾l¬O¤é¤å¡J
³z¹Lblade.nagaokaut.ac.jp/ruby/ruby-talk/index.shtml.¡A¥i¥H·j´M¶l»¼½×¾ÂªºÀÉ®× ¡]³o¬O ruby-talkªºÃìµ²¡J munge as required for the others).
¥¿¦¡ªº»¡ªk¬O¡uRuby¡v¡C¦b¤j³¡¥÷ªº¨t²Î¡A¨Ï¥Î¡uruby¡v¨Ó°õ¦æRuby¡C¨Ï¥Îruby¨ú¥N Ruby¬O³\¥iªº¡C
½Ð¤Å¨Ï¥ÎRUBY¨Ó¬°³oÓµ{¦¡»y¨¥ªº¦W¦r¡C
쥻¥¦¬O¥s§@¡uruby¡vªº¡C
½Ðª`·N¥H¤U³s©¹RubyGarden WikiªºÃìµ²¡Jwww.rubygarden.org/ruby?RubyBookList
¼w¤å®ÑÄy¡J
Rubyªº»yªk¦Ûª©¥»1.0«á¤w¸géw¤U¨Ó¡A¦ý·s¥\¯à¤´¤£Â_¼W¥[¡C ©Ò¥H®ÑÄy©Mºô¤W¤å¥ó¥i¯à·|¸ò¤£¤W¡C
Y¦³°ÝÃD¡AÀH®É¥i©ó¶l»¼½×¾Â¤Wµo°Ý¡C¤@¯ë¨Ó»¡§A·|«Ü§Ö±o¨ì ¨Ó¦Ûmatz¡]Rubyªº§@ªÌ¡^¡B¨ä¥L¤j®v©M¤@¨Ç¸Ñ¨M¹LÃþ¦üªº°ÝÃDªº¤HªºµªÂСC
YªG¬O¸ß°Ý¦³°ÝÃDªºµ{¦¡½X¡A½Ð±Nruby -vªº¿é¥X¤]¤@¨Ö°O¤U¡C
Y§A¨Ï¥Îirb¡A¤p¤ß¥¦ªº¤@¨Ç¨î¡C¥Îirb -single-irb ¹Á¸Õ§AªºScript¡A©Îª½±µ¨Ï¥Îruby¡C
There might be similar questions in the mailing list, and it is good netiquette to read through recent mails (RFC1855:3.1.1, 3.1.2) before asking. But do ask on the list, and a correct answer will be forthcoming.
¨ä¤¤¤@Óµ¹ªì¾ÇªÌªº«n¤å¥ó¬OWilliam Djaja Tjokroaminataªº§@«~¡C¥¦ÂI¥X¤Fªì¾ÇªÌ±`¥Çªº¿ù»~¡C
www.glue.umd.edu/~billtj/ruby.htmlwww.rubygarden.org/ruby?DistributeARubyAppInASingleExecutable has a fair bit of information about this.µª®×¬OªÖ©wªº¡A±a¦³¨âÓĵ§i¡J
Ruby¬Oª½Ä¶ªº¡CRubyªº¥¼¨Óp¹º¬OÁͪñ¤@Ó½sĶªºµ{¦¡»y¨¥¡C ³o¥]¬A¤F³]p¤@ÓRubyVM (Rite / Ruby 2.0)¡A¤SºÙ§@Cardinal (Ruby frontend for ParrotVM)¡C Being interpreted does affect the speed of execution to some extent, but Ruby actually does pretty well in the language shootout rankings. ´¶¹M¨Ó»¡Rubyªº³t«×¤£·|©MPerl¤@¼Ë§Ö¡A¦ý¤£·|¤ñ¥¦¤Ó¹LºC¡C
±N¥H¤U´X¦æ¥[¤J§Aªº .vimrc
filetype plugin on "Turn ftplugins on filetype indent on "Turn ftindent on
¸Ô¨£www.rubygarden.org/ruby?VimRubySupport¡C
#!¹ï^¤åª©¦Ó¨¥¡A¤£¬O¹ï³oÓ¤¤Ä¶¥»
There are a group of four or five folks who have update privileges¡J·í§Ú̬ݨì°ÝÃD¡A·|¦³Ó[ANSWER]Áä¥X²{¡A§ÚÌ«ö¥¦¨Ó¼W¥[µª®×¡C
¦pªG¥ô¦ó¤H·Q°µ¦^µªªÌ¡A¥iµ¹§Ú¹q¶l¡C
Dave
2.1 Ruby©MPython¬Û¤ñ¡H
2.2 Ruby¹ï¨ä¥L°ª¶¥°ÊºAµ{¦¡»y¨¥
Python©MRuby§¡¬Oª«¥ó¾É¦Vªº»y¨¥¡A¬Ò´£¨Ñ¤F¤@ӥѵ{§Ç¦¡ªº ¦Üª«¥ó¾É¦V½sµ{ªº¶ê¶¶Â½Ä¶¡C¬Û¹ï¨Ó»¡¡ASmalltalk ¥u¦³ª«¥ó¡X¡X§A¤£¥i¥H°µ¥ô¦ó¨Æ¡Aª½¦Ü§A©ú¥Õª«¥ó¡BÄ~©Ó©M the sizable Smalltalk class hierarchy¬°¤î¡C¬°¤F´£¨Ñ »²§Uµ¹µ{§Ç¦¡ªº½sµ{¡APython©MRuby¡u׸ɡv¤F¨ä¤¤¤@Ó ¥\¯à¡A¥i¯à¥OSmalltalkÂ÷¶}¤F¥D¬y¡C³o¨âÓµ{¦¡»y¨¥¥Î¹ï ¥ßªº¤èªk¨Ó¸Ñ¨M³oÓ°ÝÃD¡C
Python¬OÓ²VºØªº»y¨¥¡C¥¦¦³µ{§Ç¦¡½sµ{ªº¨ç¼Æ©Mª«¥ó ¾É¦V½sµ{ªºª«¥ó¡CPython¤¹³\¨ç¼Æ©Mmethods interconvert using the explicit ``self¡¦¡¦ parameter of every method def¡A¬°³o¨âÓ¥@¬É«Ø°_¾ô¼Ù¡C When a function is inserted into an object, the first argument automagically becomes a reference to the receiver.
Ruby¬O¯Âª«¥ó¾É¦V»y¨¥¡A¥i¥H¸Ë§ê¦¨µ{§Ç¦¡ªº»y¨¥¡C¥¦ ¨S¦³¨ç¼Æ¡A¥u¦³method calls¡CIn a Ruby method the receiver, also called self, is a hidden argument like ``this¡¦¡¦ in C++. A ``def¡¦¡¦ statement outside of a class definition, which is a function in Python, is actually a method call in Ruby. These ersatz functions become private methods of class Object, the root of the Ruby class hierarchy. Procedural programming is neatly solved from the other direction - everything is an object. If the user doesn¡¦t grok objects yet, they can just pretend that ``def¡¦¡¦ is a function definition and still get useful work done.
Ruby¯Â²bªºª«¥ó¾É¦V¯S©Ê´£¨Ñ¤F¤@¤j°ïPython¨S¦³©Î¤D ¦b¤u§@ªº¥\¯à¡Ja unified type/class hierarchy, metaclasses, the ability to subclass everything, and uniform method invocation (none of this len() is a function but items() is a method rubbish). Ruby¹³Smalltalk¯ë ¶È¤ä«ù¤@Ä~©Ó¡A¦ý¥¦¦³«D±`±j¤jªºmix-in concept: a class definition may include a module, which inserts that module¡¦s methods, constants, etc. into the class.
Ruby, again like Smalltalk, provides closures and code blocks and uses them to the same good effect. The Ruby collection classes and iterators are outstanding, much more powerful and elegant than the ad hoc solutions that Python is sprouting (lambdas and list comprehensions).
Rubyªº»yªk©M³]põ¾Ç¤j¨üPerlªº¼vÅT¡C¥¦ªº»yªk¦³«Ü¤j¼u©Ê¡C Statement modifiers ¡]if, unless, white, until µ¥µ¥¡^ ¥i¥H¥X²{©ó»y¥yªº§À³¡¡C¦s¦b¤@¨Ç¿ï¾Ü©ÊªºÃöÁä¦r¡]¨Ò¦pif»y¥y ùتºthen¡^¡Cmethod¤¤ªº¬A¸¹¦³®É¥i¥H¬Ù²¤ ³\¦h³\¦hªF¦èª½±µ¨ú¦ÛPerl¡C¤º«Øªº¥¿³Wªí¥Ü¦¡¡B$_©M¥¦ªº¥S§Ì¡B here documents¡B³æÂù¤Þ¸¹ªº¤À§O¡Bmethod¤¤ªº¬A¸¹¦³®É¥i¥H¬Ù²¤ ©M¨ä¥L¡C
Y§A³ßÅwPerl¡A§A·|³ßÅwRuby¡Aı±oÀH¥¦ªº»yªk¦^¨ì®a¤¤¡C Y§A³ßÅwSmalltalk¡A§A·|³ßÅwRuby¡Aı±oÀH¥¦ªº»y¸q¦^¨ì®a¤¤¡C Y§A³ßÅwPython¡A§A©Î·|©Î¤£·|¦]Python©MRuby/Perlªº³]põ¾Ç ªº¥¨¤j®t²§¦Ó¥¢¥h¿³½ì¡C
Ruby¤ñPython½ÆÂø±o¦h¤F¡A¦ý¥¦¤j³¡¤Àªº¥\¯à³£ºë±m¦a°t¦X¡C Ruby³]p¬ü¦n¡Aº¡¬Oºë¤ß³]pªº·N©À¡Athat might be mined for P3K. §Ú¤£ªÖ©w¦³¦h¤ÖPythonµ{¦¡³]p®v±N·|ª`µø¨ìRuby¡X¡X ¥¦²{¦b©|¥¼¹Ü¥h§Úªº¤ß¡C¦ý¥¦¬Oȱo«µø¡A ¨Ã¥i¥H¹ïPerl§@¥X¤@Ó¯u¥¿ªº«Â¯Ù¡C
11/17/2000 John Dell¡¦Aquila©ócomp.lang.python
½Æ»sÀò±o±ÂÅv¡C
Ruby ¹ïSmalltalk ¹ï Objective-C ¹ï C++ ¹ï Java ¹ï Python ¹ï CLOS ¹ï Perl5 ¹ï PHP ¹ï Dylan¡C
¦b«Ü¦h¤HÀ°¦£¤U¡A§ÚÌ»s¦¨¤F¤Uªí¡J
www.approximity.com/ruby/Comparison_rb_st_m_java.html
¥t¤@Ó¡J
www.smallscript.com/Language%20Comparison%20Chart.asp
3.1 ¬Æ»ò§@·~¨t²Î¤ä´©Ruby¡H
3.2 Rubyªºì©l½X¦bþùØ¡H
3.3 §Ú¥i§_¨ú±oRubyªºdevelopment source tree¡H
3.4 ¦p¦ó½sĶRuby¡H
3.5 ¦p¦ó§i¶DRuby§Úªº¨ç¦¡®w¦bþùØ¡H
3.6 ¦³¤w½sĶªºª©¥»¶Ü¡H
3.7 ¬Æ»ò¬O¡ucygwin¡v¡B¡umingw¡v©M¡udjgpp¡v¡H
3.8 ¬°¦óTkªº¹Ï¹³¤£¥i¦bWindows¤u§@¡H
Mac OS X 10.2¹w¥ý¦w¸Ë¤FRuby(Ruby 1.6.7 (2002-03-01) [powerpc-darwin6.0])¡C¸Ô¨£developer.apple.com/technotes/tn2002/tn2053.html.
H Morita ª`¡J
Ruby¦³Mac OS¡]¤£¬OX¡^ªºport¡A¥ÑHisakuni FUJIMOTOt³d¡A©ñ¦bwww.fobj.com/hisa/old/ruby/. ¤£¹L¥¦¬O«Ø°ò©óRuby 1.1b7¡A¦Û¤G¹s¹s¤@¦~¤Q¤G¤ë¤]¤£´¿§ó·s¡C
³sRubyª©¥»¤ºªº½d¨Òscripts¤]¦³¾÷·|³y¦¨·í¾÷¡]¬Ý¨Ó®¼¦³½ìªº ;-¡^¡C
Salvador Parra Camacho»¡³oùئ³¨ÑOS/2¨Ï¥ÎªºRuby¡Jhobbes.nmsu.edu/pub/os2/dev/misc/rbemx164.zip
³Ì·sª©¥»ªºRubyªº¤U¸ü³B¡Jwww.ruby-lang.org/en/20020102.html¡C¦b¸Ó¶¥ç¦C¥X¤FÃè¹³¯¸¡C
¦P®É¦b¸Ó¶¦³Ó³s¦Vnightly snapshot of the development treeªºÃìµ²¡C
Y§A¦³CVS client¡A§A¥i¥H¥Î¥H¤Uªº«ü¥O¨ú±o²{¦bªºsource tree¡J
% cvs -d :pserver:[email protected]:/home/cvs login (Logging in to [email protected]) CVS password: guest % cvs -d :pserver:[email protected]:/home/cvs co ruby
YªG§A¨S¦³CVS¡A§A¥i¥H¥Ñftp.netlab.co.jp/pub/lang/ruby/snapshot.tar.gz¨ú±o¤@Ónightly snapshot of the development source¡C
Unix¤U¡ARuby¨Ï¥Îautoconf¨t²Î¨Ó³]©w½sĶÀô¹Ò¡C¤£¥²¨Ï¥Îautoconf«ü¥O¨Ó±q¤@Óª©¥»«Ø¥ßRuby¡F¥u»Ý¥Î¥H¤U«ü¥O¡J
% ./configure [configure options] % make % make test % make install
Y§A¤£¬O¤£ÅU¹w³]ªº¦w¸Ë¸ô®|(/usr/local)¡A´N¥i¯à»Ýn¶W¯Å¨Ï¥ÎªÌªº¯SÅv¨Ó¦w¸ËRuby¡C§A¥i¥H¨Ï¥Î¥H¤U«ü¥O¨ú±o¤@¥÷configureªº¿ï¶µªº§¹¾ã¦Cªí¡J
% ./configure --help
YªG§A¥ÑCVS archive¤u§@¡A§A¥i¯à»Ýn¥ý°õ¦æ autoconf¡A¤~°õ¦æconfigure¡C
¦b¬Y¨Ç¨t²Î¡A«Ø¥ßRubyªº°Ê§@¥i¯à·|¦]¬°§ä¤£¨ì¬Y¨Ç¥Î¤FÂX¥R®M¥ó¼Ò²Õ(extension modules)ªº¨ç¦¡®w¡]¨Ò¦pdbm¨ç¦¡®w¡^¦Ó¥¢±Ñ¡C
§A¥i¥H¨Ï¥Îconfigure¨Ó§i¶DRuby¦bþùاä¨ç¦¡®w ¥Ñruby-talk:5041¡J
./configure --with-xxx-yyy=DIR
¦b¦¹¡Axxx¬O¥H¤U¨ä¤¤¤@Ó¡J
opt extra software path in general dbm path for dbm library gdbm path for gdbm library x11 ...for X11.. tk ...for Tk... tcl ...for Tcl...
¦Óyyy«h¥i¯à¬O
dir specifies -I DIR/include -L DIR/lib include specifies -I DIR lib specifies -L DIR
¦bHP-UX¡A¥Îgcc¨Ó«Ø¥ßRuby¥i¯à·|¦³°ÝÃD¡C¹Á¸Õ¨Ï¥ÎHP-UXªº½s;¹¡C WATANABE Tetsuya«ØÄ³¡J
CC="cc -Ae" CFLAGS=-O ./configure --prefix=/opt/gnu
HPªº¤º¸mªºsed¥ç¥i¯à¦³°ÝÃD¡C¥L«ØÄ³¦w¸ËGNUªºsed¡C
¥]¬A©Ò¦³ªF¦è§A»Ýn¦b¦UºØWindows¤U¹B§@Rubyªº³æ¤@¤U¸üÀÉ®×¥i©ó rubyinstaller.rubyforge.org/wiki/wiki.pl§ä¨ì¡C ¦Û2002¦~¤¤¡A³oÓª©¥»¨Ï¥ÎMicrosoft Visual C compiler¨Ó½sĶ¡C
¦pªG§A·Qn¨ä¥L¦w¸Ëªº¿ï¾Ü¡A¨ÑWindows¨Ï¥Îªº¤w½sĶª©¥» ¥ç¥i¦bwww.os.rim.or.jp/~eban§ä¨ì¡C Y§A¤U¸ü¤Fruby-1.x.y-yyyymmdd-i386-cygwin.tar.gz¡]¬OÓ¦n¿ï¾Ü¡^¡A §A¥çn¦b¸Ó¶¤U¸ücygwin DLL¡C
³oÓµ¹Acorn¹q¸£¨Ï¥Îªº¤w½sĶª©¥»©ñ¤F©ówww.cl.cam.ac.uk/users/rrt1001/ruby.zip¡C
Steve Christensen©ó14-Aug-04 08:26»¡¡J
¨ÑSPARC/Solaris©Mx86/Solaris 8-9¨Ï¥ÎªºRuby 1.8.1¤w½sĶªºÀÉ®×¥i©ósunfreeware.com §ä¨ì
dublinclontarf©ó16-Aug-04 15:45»¡¡J
Slackware Linux¤W¨Ï¥ÎªºRuby 1.8.1¥i©ówww.linuxpackages.net§ä¨ì
Rubyµ½¥Î¤FUnixÀô¹ÒªºÂ×´I¥\¯à¡C¤£©¯¦a¡AWindows¥¢¥h¤F¬Y¨Ç¥\¯à¡A¥BÃø¥H°õ¦æ¬Y¨Ç¥\¯à¡C¬G¦¹¦bWindows»Ýn¤@¨Çmapping layer¨Ó°õ¦æRuby¡]¥H¤Î¨ä¥L¥HUnix¬°®Ú°òªºµ{¦¡¡^¡C
§A¥i¥Hµo²{¤£¦Pª©¥»ªºRuby°õ¦æÀÉ¡A¨Ï¥Î¤£¦Pªº¥]¸Ëªºmapping layers¡C
rbdj¬OÓ¿W¥ßªºRuby Windows binaryª©¥»¡C¥¦¨Ï¥Î¤FDJ Delorieªº¤u¨ã¡]www.delorie.com¡^¡C
rbcw¬OÓ»Ýncygwin libraryªºRuby Windows binary¡A¥i©ówww.cygwin.com©ÎRuby download pages§ä¨ì¡CCygwin¬OÓ¼ÒÀÀ¼h©M¤@®M¥ÑCygnus Solutions¡]²{¦bRedhat¤½¥qªº¤@³¡¤À¡^»s³yªº¤u¨ã¡CRubyªºCygwinª©À³¸Ó¦³Windows¤U³Ì»ô¥þªº¥\¯à¡A©Ò¥H«Ü¦hµ{¦¡³]p®v·|·Q¥Î¥¦¡C
n¨Ï¥Îrbcwª©¥»¡A§A»Ýn¿W¥ß¦w¸Ëcygwin.dll¡C·í§A¦w¸Ë¤Fcygwin¡A½Æ»scygwin1.dll¡]¥i¥H¦bcygwinªºbin§ä¨ì¡^¡A¶K©¹WINDOWS\system32¸ê®Æ§¨¡]©Î§Aªº¸ô®|¤¤ªº¬Y³B¡^¡C
·PÁÂAnders Schneiderman´£¨Ñ¤F³o¬q´yzªº°ò¦¡C
6.1 :var¬O¬Æ»ò·N«ä¡H
6.2 How can I access the value of a symbol?
6.3 loop¬O§_±±¨îµ²ºc¡H
6.4 How can I code post-test loops?
6.5 a +b³y¦¨¿ù»~¤F¡I
6.6 s = "x"; puts s *10 ³y¦¨¿ù»~¡C
6.7 Why can't I pass a hash literal to a method: p {}?
6.8 How can I call lvalue methods?
6.9 \1©M\1¦³¦ó¤À§O¡H
6.10 or©M||¦³¦ó¤À§O¡H
6.11 and©M&&¦³¦ó¤À§O¡H
6.12 Rubyùبº¨Ç¬J¤£¬O¦r¥À¤S«D¼Æ¦rªº²Å¸¹¬O¬Æ»ò¡H
6.13 $:, $!, $_µ¥²Å¸¹¬O¬Æ»ò·N«ä¡H
6.14 ¦ó¸Ñ (1210 / 100) ±o¥X12¦Ó«D12.1¡H
The routines catch, throw, autoload, and so on, require a string or a symbol as an argument.
method_missing, method_added and singleton_method_added (and others) require a symbol.
The fact that a symbol springs into existence the first time it is referenced is sometimes used to assign unique values to constants:
NORTH = :NORTH SOUTH = :SOUTH EAST = :EAST WEST = :WEST
a = 'This is the content of "a"' b = eval(:a.id2name) a.id == b.id # b now references the same object as a
If your symbol corresponds to the name of a method, you can use the Method.method function to return a corresponding Method object, which you may then call.
class Demo
def meth
puts "Hello, world"
end
end
d = Demo.new
m = d.method(:meth)
m.call
Comments...
By Mark Reed
at 10-Feb-03 14:52:
It seems there should be a safer way of doing this. You can intern any string into a symbol, and calling id2name on the resulting symbol always gives you the original string back intact. So calling eval() on such a string when it has come from an external source is dangerous - yet I frequently wish to specify, say, the name of a class to use in a config file. It seems kludgy to use a regex to strip a string down to something safe and then apply the full power of eval on it just to find out what object that name maps to in the current runtime.
ÁöµMloop¬Ý¦ü±±¨îµ²ºc¡A¹ê»Ú¤W¥¦¬OÓmethod¡A©w¸q©óKernel¡C The block which follows introduces a new scope for local variables.
¦pªG§A»Ýn´ú¸Õ°j°é§À³¡¡]¬Û¤Ï¦a¦b¶}ÀY¡^ªº±¡ªp¡A¨Ï¥ÎRubyªºbegin ... end¡A»Pwhile©Îuntil statement modifiers³oÓ²Õ¦X¨Ó¨ú±o¬Û¦Pªº®ÄªG¡J
ì¤å¡J If you need loops that test the condition at the end of the block, rather than at the start, use a combination of Ruby¡¦s begin ¡K end and the while or until statement modifiers to achieve the same effect:
i = 0
begin
puts "i = #{i}"
i +=1
end until i > 4
Ķª`¡J³o¬q½Tͱo¤£¤Ó¦n¡A¦]¬°§Ú¤£¤Ó²z¸Ñ
Ruby works hard to distinguish method calls from operators, and variable names from method names. ¤£©¯¦a¡A¥¦¨Ã«D¦¸¦¸·Ç½TµL»~¡C²{¦b¡ua +b¡v³Q¤ÀªR¦¨¡ua(+b)¡v¡C²¾¨«¡u+¡v¥ªÃ䪺ªÅ¥Õ©Î¦b¨ä¥k¼W¥[¤@ӪťաA¥¦·|³Q¤ÀªR¦¨¥[¼Æªº¤F¡C
¦p¦P¤WÃD¡ARuby¬Ý¨ì¤£¹ïºÙªºªÅ¥Õ¡AµM«á±N¤§¤ÀªR¦¨puts(s(*10))¡]¯uªº¦³ÂI²Â¡K¡K¡^¡C¥Î¡us*10¡v©Î¡us * 10¡v¨Ó¨ú±o§A·Qnªºµ²ªG½}¡C
³oÓ{}³Qµø¬°block¡A¦Ó«Dhashªººc³y¨ç¼Æ¡CYou can force the {} to be treated as an expression by making the fact that it¡¦s a parameter explicit: p({})
Ruby lets you define lvalue methods, methods that can receive a value from an assignment. However, these methods must be called with an explicit receiver, otherwise Ruby simply interprets the call as a variable assignment:
def pos=(val) @pos = val end pos = 123 # doesn't call method self.pos = 456 # calls method
¥¦Ì·N«ä¬Û¦P¡C¦b¥Î³æ¤Þ¸¹¥]µÛªº¦r¦êùØ¡A¥u¦³\¡¦©M\ are transformed and other combinations remain unchanged.
µL½×¦p¦ó¡A¦bÂù¤Þ¸¹¥]µÛªº¦r¦êùØ¡A"\1"¬O \001³oÓ¦r¸`¡A"\1"ªí¥Ü¨âÓ¦r²Å¦ê(character string) ¡A¥]¬A¤@Ó˱׽u©M"1"³oÓ¦r¤¸¡C
||¦b¦¡¤lùسB²z¨â¶µ¸ê®Æ¡A
p(false || "Hello")
·|¼g¥XHello¡A¦]¬°¦b³oÓ±ø¥ó¤Uº¶µ¸ê®Æ¬Ofalse¡A±j¨î ³B²z²Ä¤G¶µ¸ê®Æ¡C
¥t¤@¤è±¡Aor¬O¥Î¨Ó¦b±ø¥óùسB²z¨â±ø¦¡¤l¡C
p(false or "Hello")
·|¾ÉP»yªk¤ÀªR¿ù»~(parse error)¡A¦]¬°Ruby¤£±µ¨ü¤@Ó±ø¥ó»y¥y¥X²{¦b¤Þ¼Æ¤º¡C
¦³®É¡uand¡vªºÀu¥ý©Ê·|¤ñ¡u=¡vn§C¡C¦³¨Ç¤H¦]¤U±ªº¦¡¤l·P¨ì¤£¾A¡J
have_both = a and b
·|µ¥¦P¡J
(have_both = a) and b
§AÀ³±Ä¥Î¥H¤U¨ä¤¤¤@Ó¤è®×¡J
have_both = a && b have_both = (a and b) if a and b # ... end
Á`¬A¨Ó»¡¡A¡u&&¡v¾A¦X³B²zÈ(values)¡F¡uand¡v¾A¦X³B²z¤Þ¼Æ(statements)¡C¤£À³¨Ï¥Î¡uand¡vªð¦^¥ô¦ó«D¥¬ªLÈ¡C
¦pªGarr¬Onil¡A¤U±ªº¦¡¤l¤]¤£·|¥X²{¿ù»~¡J
result = arr && arr.size
¤U±¬Oif ... then ... else ...ªºÂ²¼g¡J
action() and LOG.info "Performed action" or LOG.info "Failed to perform action"
©Î³\·s¤â¹ï¦¹·P¨ì§x´b¡A¦ý¦pªG§A§â¥¦¬Ý¦¨¦ÛµM»y¨¥¡A±N«Ü§ÖÀ´±o¦p¦ó¹B¥Î¥¦¡C
ª`¡J¡]¡^¡C¡u&&¡vªºÀu¥ý©Ê¤ñ¡u||¡v¬°°ª¡C¦Ó¡uand¡v¸ò¡uor¡v«h¦³¬Û¦PªºÀu¥ý©Ê¡C
¤Uªí¬O°ò¥»ªººKn¡F¸Ô¨£www.rubygarden.org/ruby?FunnySymbolsInCode¡A¥]¬A¨Ò¤l¡C
¥¦Ì¬O¥þ°ìÅܼơ]©Ò¦³$xxx¬Ò¬O¥þ°ìÅܼơ^¡A¥]§t¤Fµ{¦¡ªº «nªº¸ê®Æ©M¥¦ªº°õ¦æÀô¹Ò¡C¥¦Ì¥ÑPerl°½¹L¨Óªº¡A¦Ó§Ú̹ªÀy§A±Ä¥Î ¤ñ¸û±µªñRubyªººD¥Î¤è¦¡¨Ó¨ú±o¸ê°T¡C
½Ð¨£"Ruby in a Nutshell"²Ä¤T³¹(Built-in Libraray Reference)¡A ¨ú±o¥¦Ìªº¸ÑÄÀ¡C¥t¥i¬Ý¬Ý"English.rb"³oÓÀɮסA¥i¥H¥Îrequire English ¨Ó¨ú±o¥Î^¤å¨Ó§@³o¨ÇÅܼƪº¦W¦r¡C¨Ò¦p¡J
alias $ERROR_INFO $! alias $FIELD_SEPARATOR $; alias $DEFAULT_OUTPUT $> alias $ARGV $*
¦pªG§A¶i¦æ¾ã¼Æ¬Û°£¡ARuby·|µ¹¥X¤@Ó¾ã¼Æªºµª®×¡C n¨ú±o·Ç½Tªºµª®×¡A½ÐªÖ©w°£¼Æ¬O¯BÂI¼Æ¡C
1210 / 100 # -> 12 1210.0 / 100 # -> 12.1 1210 / 100.0 # -> 12.1 1210.0 / 100.0 # -> 12.1
¤@¯ë¨Ó»¡¡AnªÖ©wa¡Bbªº°Ó¬Oºë½Tªº¡A¥i¥H³o¼Ë°µ¡J
a.to_f / b
§A¥i¥H¥Î¤U±ªº¤èªk¨ú±o¾l¼Æ¡]¾ã¼Æ¬Û°£¡^¡J
remainder = 1210 % 100 # -> 10 quot, rem = 1210.divmod(100) # -> 12, 10
12.1 a ? b : c ¬O¬Æ»ò·N«ä¡H
12.2 ¦p¦ópºâÀɮתº¦æ¼Æ¡H
12.3 What do begin and end of MatchingData return?
12.4 «ç¼Ëpºâ°}¦Cùتº¤¸¯À¤§©M¡H
12.5 continuations
12.6 Ruby¦³CGI¼Ò²Õ¶Ü¡H
12.7 best use of require
§Yif a then b else c end¡A¥¦ÅýCµ{¦¡û·P¨ì§ó¤è«K¡C
±NÀɮ׳̧À¦³ªÅ¥Õ¦æ¤]pºâ¦b¤º¡A¤U±ªºµ{¦¡½X¥i¥Hµ¹¥X³Ì§Öªºµ²ªG¡C
open("ÀÉ®×").read.count("\n")
·íµM¦³°Õ¡Acgi.rb¬ORubyªº¼Ð·Ç¨ç¦¡®wªº¤@¤À¤l¡C §A¥i¥H¾\Ū"Programming Ruby" (chapter "Ruby and the Web")¨Ó¤F¸Ñ¥¦¡C
Other packages of interest are: * CGIKit (framework) * Amrita (templating library) * Narf (web library)¥H¤W§¡¥i¦bRAA§ä¨ì¡C