· KLDP.org · KLDP.net · KLDP Wiki · KLDP BBS ·
Humble Little Ruby Book/Chap3


1. À¯µµ¿Í È帧(Á¦¾î)


È帧Á¦¾î´Â ´ëºÎºÐÀÇ ¾îÇø®ÄÉÀ̼ǿ¡¼­ Áß¿äÇÑ ºÎºÐÀ» Â÷ÁöÇÕ´Ï´Ù. (ÀÛ¼ºÇÑ ÇÁ·Î±×·¥ÀÌ Ç×»ó µ¿ÀÛÀÇ ÇÑ ¹æÇâÀ» ÃëÇÏÁö ¾Ê´ÂÇÑ ÀÌ ÀåÀÇ ´ëºÎºÐÀÇ Äڵ带 º¹»çÇؼ­ ºÙ¿©³ÖÀ¸·Á´Â ½ÄÀÇ ²Ç¼öºÎ¸®·Á´Â ¼öÀÛÀº »ï°¡Çϱ⠹ٶø´Ï´Ù). È帧Á¦¾î´Â »óÅ¿¡ ´ëÇÑ ÇൿÀ» ÃëÇϱâ À§ÇÑ Çൿ±Ô¹ü(?)À» ¼ö¸³Çϵµ·Ï µµ¿ÍÁÝ´Ï´Ù.(i.e. °¢±â ´Ù¸¥ Äڵ带 °æ¿ì¿¡ µû¶ó ¼öÇàÇÏ·Á Çϴ°æ¿ì...), °°Àº °¥·¡»óÀÇ Äڵ带 ¿©·¯¹ø ½ÇÇàÇÒ ¼ö Àְųª ¾î¶² »óÅ°¡ ¸¸Á·½º·´Áö ¸øÇÑ°æ¿ì¿¡ ¿ìȸÇÒ ¼ö µµ ÀÖ½À´Ï´Ù. µÎ°¡Áö "ÀϹÝÀûÀÎ" È帧 Á¦¾î ±¸¼ºÀÌ Àִµ¥ »óÅÂ¿Í ¹Ýº¹ÀÔ´Ï´Ù. À̰͵éÀ» ³¡³»Áö ¾ÊÀ¸¸é ¶ËÅë¿¡ ºüÁ® ¸÷½Ã ´çȲÇÑ ¹ÌÄ£XÀÎ ¸¶³É ¿©±â¿¡¼­ Æ÷±âÇÑ ²ÃÀÌ µÇ¹Ç·Î ÇÊÀÚ´Â ÀÌÀåÀ» ÅëÇØ °¢°¢ÀÇ ¿ä¼Ò¸¦ Á¦´ë·Î Á¤¸®ÇÏ¿© ¾È³»ÇÒ °ÍÀÔ´Ï´Ù. Á¤½Å ¹Ù¦Â÷¸®°í µû¶ó¿À½Ã±â ¹Ù¶ø´Ï´Ù. -_¤Ñ)+


1.1. »óÅÂ

»óÅ ±¸Á¶´Â °¢°¢ÀÇ °æ¿ì¿¡ ´ëÇÑ ¼±ÅÃÀÇ °á°ú¸¦ ¸¸µé¾î³À´Ï´Ù. ÇÁ·Î±×·¥ ³»ºÎ¿¡¼­ »óŸ¦ ±â¹ÝÀ¸·Î ÇÏ´Â °¢°¢ÀÇ Äڵ带 ½ÇÇàÇÒ ¼ö ÀÖµµ·Ï °¥·¡¸¦ Çü¼ºÇÏ°Ô²û ÇÕ´Ï´Ù. °ªÀ» ºñ±³ÇÏ°í Âü °ÅÁþ ¿©ºÎ¸¦ ÆÇ´ÜÇÏ¿© ÇØ´ç °¥·¡¿¡ ´ëÇÑ Äڵ带 ½ÇÇàÇÏ°Ô²û ÇÕ´Ï´Ù. ÀÌ¿Í °°Àº ¿ä¼Ò´Â ·çºñ ÇÁ·Î±×·¡¹Ö ¼¼°èÀÇ ±³Åë ½ÅÈ£µî°ú ºñ½ÁÇÑ ¿ªÇÒÀ» ÇÑ´Ù »ý°¢ÇÏ½Ã¸é µÇ°Ú½À´Ï´Ù.

1.1.1. if ¹®

·çºñ¿¡ ÀÖ¾î ¸Å¿ì ±âÃÊÀûÀÎ »óŹ®Àº if¹®ÀÔ´Ï´Ù. ÀÌ´Â °ªµéÀ» °£´ÜÈ÷ ºñ±³ÇÏ°í Âü °ÅÁþÀ» ÆÇ´ÜÇÕ´Ï´Ù. ¸¸¾à ÂüÀ̶ó¸é, ÁÖ Äڵ尡 ½ÇÇàµÇ°í, °ÅÁþÀ̶ó¸é else Ç×ÀÇ Äڵ带 ½ÇÇàÇÏÁö¸¸, else Ç× ¸¶Àú ¾ø´Â °æ¿ì¿¡´Â, ±× ¾î¶² if ºí·Ï ³»ÀÇ Äڵ带 ½ÇÇàÇÏÁö ¾Ê°í ±×´ë·Î ³Ñ¾î°©´Ï´Ù..


if.jpg
[JPG image (56.11 KB)]

Figure 7: ¸¸¾à ³»°¡ ¹Ù´Ù¿¡ »ç´Â °í±â¶ó¸é, ±â»Ý¿¡ ³ÑÃÄ Çì¾öÄ¡¸®¿À...


¸ðµç if¹®ÀÇ °á°ú´Â ´Ù¾çÇÑ »óÅ °ü°è¿¡ ´ëÇÑ ÂüÀ̳ª °ÅÁþÀÔ´Ï´Ù. ´ÙÀ½ÀÇ ¿¹½Ã¸¦ º¸µµ·Ï ÇսôÙ:

if not (true != false) && (true != true):
    puts "Whoa! Alternate dimension!"
elsif (false === 'false') then
    puts "Type conversion? I think not..."
elsif (true == 1) or (3 == false)
    puts "Booleans != Numbers"
else
    puts "I guess we're still OK!"
end

¡æ I guess we're still OK!

¿©±â¿¡¼­ Âü ¸¹Àº °ÍÀ» º¼ ¼ö ÀÖ½À´Ï´Ù. ±×·¸±â ¶§¹®¿¡ ÀÌ ¼Ò½ºÄÚµåÀÇ ºÎºÐºÎºÐÀ» ½Ã½Ã¶§¶§·Î ÂÉ°³¾î ½ÇÇàÇغ¸µµ·Ï ÇÕ´Ï´Ù. if ¹®Àº µ¿µî¿©ºÎ µî¿¡ µû¸¥ »óźñ±³°ª¿¡ ÀÇÇÑ Å°¿öµå¿¡ ÀÇÇØ ±¸¼ºµË´Ï´Ù. if ¹®Àº ÀÚüÀûÀ¸·Î »óÅ ºí·ÏÀ» ¿±´Ï´Ù (i.e. if´Â begin/endºí·ÏÀÇ ½ÃÀÛÁ¡ÀÔ´Ï´Ù). ifÅ°¿öµå·Î ½ÃÀÛÇÏ°í end Å°¿öµå·Î ³¡À» ¸Î´Â ´ÜÀ§ Äڵ带 È°¿ëÇÏ¿© if¹® ºí·ÏÀ» °£´ÜÈ÷ ¸¸µé ¼ö ÀÖ½À´Ï´Ù. ÀÌ ¿¹Á¦¸¦ ÅëÇØ if ¹®À» ¸¸µé±â À§ÇÑ °¡´ÉÇÑ ±¸¼ºÀ» ¸ðµÎ Á¦½ÃÇÏ¿´½À´Ï´Ù. elsif ¿Í else Å°¿öµå´Â if ¹®¿¡¼­ Âü Á¶°ÇÀ» ÃæÁ·½ÃÅ°Áö ¸øÇÒ °æ¿ì ´Ù¸¥ °æ¿ì¿¡ °üÇÑ °¥·¡·Î À̵¿ÇÒ ¼ö ÀÖµµ·Ï ÇÕ´Ï´Ù. elsif ´Â ´Ù¸¥ »óÅ ºñ±³ ½ÃÇèÀ» ÅëÇØ Âü Á¶°ÇÀ» ¸¸Á·½ÃÅ°´Â °æ¿ì ÇØ´ç Äڵ带 ½ÇÇàÇÒ ¼ö ÀÖµµ·Ï ÇÕ´Ï´Ù. ±×·¯³ª else´Â if¿Í elsifÀÇ ±× ¾î¶² Á¶°Çµµ ¸¸Á·½ÃÅ°Áö ¸øÇßÀ» °æ¿ì¿¡ Äڵ带 ½ÇÇàÇÒ ¼ö ÀÖµµ·Ï ±æÀ» ¿­¾îÁÝ´Ï´Ù. ÀÌ ¾ê±â´Â °á±¹ ¼ö¸¹Àº elsif¸¦ Æ÷ÇÔÇÏ°í ÀÖ´Â if ºí·Ï Çϳª¸¦ ¸¸µå´Â °ÍÀÌ ³´Áö, ±»ÀÌ if ºí·ÏÀ» 10°³Á¤µµ ´õ ¸¸µé ÇÊ¿ä´Â ¾ø´Ù´Â ¾ê±â°¡ µË´Ï´Ù. (with the safety net of the else statement to boot).

µÎ¹ø°·Î ¿ì¸®°¡ º¸¾Æ¾ß ÇÒ °ÍÀº »óŹ®¿¡¼­ »ç¿ëÇÒ ¿¬»êÀÚµéÀÔ´Ï´Ù. µ¿µî¼ºÀ» ºñ±³Çϱâ À§ÇØ "==" ¿¬»êÀÚ¸¦ »ç¿ëÇÕ´Ï´Ù. (µÎ °³ÀÇ µîÈ£ ±âÈ£¸¦ Ç¥±âÇÏÁö¸¸ ±×°Í »Ó¸¸ÀÌ ¾Æ´Õ´Ï´Ù. ¸¸¾à ÇÑ°³¸¦ »ç¿ëÇß´Ù¸é °á°ú°¡ ÂüÀ¸·Î ³ª¿À°Ô µÇ´Âµ¥ ÀÌ´Â µîÈ£ ÇÑ°³¸¦ »ç¿ëÇÔÀ¸·Î ÀÎÇØ °ªÀ» º¯¼ö¿¡ ÇÒ´çÇÏ¿´À¸¹Ç·Î º¯¼ö°¡ 0ÀÌ ¾Æ´Ñ ¾î¶² °ª ÀÚü¸¦ Æ÷ÇÔÇÏ¿© ÂüÀÌ µÇ±â ¶§¹®ÀÔ´Ï´Ù). ¸¸¾à °°Áö ¾ÊÀ½ »óŸ¦ ºñ±³ÇÏ·Á¸é != ¿¬»êÀÚ¸¦ ¾¹´Ï´Ù. ÀÌ ¾öû³­ ¾çÀÇ ¹®´ÜÀ» ÅëÇØ À̸¦ ¼³¸íÇÏ´À´Ï Â÷¶ó¸® ¾Æ·¡ÀÇ Ç¥¸¦ ÀÌ¿ëÇØ °£´ÜÇÏ°Ô Ruby¿¡¼­ »ç¿ëµÇ´Â ¿¬»êÀÚ¸¦ ¾Ë¾Æº¸´Â ÆíÀÌ ¼Ó½Ã¿øÇÒÁöµµ ¸ð¸£°Ú½À´Ï´Ù:

Ruby¿¡¼­ÀÇ »óÅ ¿¬»êÀÚ
a == b a ¿Í b°¡ °ªÀÌ °°À»¶§ Âü.
a === b a¿Í b´Â °ªÀÌ °°À¸¸é¼­ ÀÚ·áÇüµµ °°À»¶§ Âü.
a != b a¿Í b´Â °°Áö ¾ÊÀ»¶§ Âü.
a <> b
a !== b a¿Í b°¡ °ªÀÌ ´Ù¸£¸é¼­ ÀÚ·áÇüµµ ´Ù¸¦ °æ¿ì Âü
a < b aÀÇ °ªÀÌ bº¸´Ù ÀÛÀ¸¸é Âü.
a > b aÀÇ °ªÀÌ bº¸´Ù Å©¸é Âü.
a <= b a°¡ bº¸´Ù À۰ųª °°À¸¸é Âü.
a >= b a°¡ bº¸´Ù Å©°Å³ª °°À¸¸é Âü.
¼¼¹ø°·Î ÁÖ¸ñÇÒ »çÇ×Àº ³í¸® ¿¬»êÀÚÀÔ´Ï´Ù. À̰͵éÀº »óŸ¦ ¿¬¼âÀûÀ¸·Î ºñ±³Çϱâ À§ÇØ »ç¿ëÇϴ Ưº°ÇÑ ¿¬»êÀڷμ­ À̸¦ ÅëÇØ Æ¯º°ÇÑ »óÅÂÀÇ °ªÀ» ±¸ÇÕ´Ï´Ù. Ruby´Â µÎ°¡Áö ¹æ½ÄÀÇ ¿¬»êÀÚ¸¦ Áö¿øÇÕ´Ï´Ù: C-style °ú ¹®ÀÚ¿­ ¹æ½ÄÀÔ´Ï´Ù. C-style ¿¬»êÀڴ óÀ½ ¿¹Á¦¿¡¼­¿Í °°½À´Ï´Ù. && ´Â "±×¸®°í(and)", || ´Â "¶Ç´Â(or)" °ú °°Àº ½ÄÀÔ´Ï´Ù. ¹®ÀÚ¿­ ¹æ½Ä ¿¬»êÀÚ´Â »ó¡ÀûÀ¸·Î µ¿ÀÏÇÏ°Ô °£Áֵ˴ϴÙ.: and, or, ±×¿ÜÀÇ °Íµé.... ÀÌ Ç¥¸¦ ÅëÇØ ±× µÎ°¡Áö ÇüÅÂ¿Í ¾²ÀÓ»õ¿¡ ´ëÇØ Á¤¸®Çß½À´Ï´Ù.

Ruby ¿¡¼­ÀÇ »óÅ ¿¬°á ¿¬»êÀÚ
&& and ¾ç Á¶°ÇÀÌ ´Ù ÂüÀ϶§.
</td><td > or ¾ç Á¶°ÇÁß ÇÑÂÊÀÌ ÂüÀ϶§.
! not ÀÌ ¿¬»êÀÚ¸¦ ºÙÀÓÀ¸·Î ÀÎÇØ ÂüÀº °ÅÁþÀÌ µÇ°í °ÅÁþÀº ÂüÀÌ µÊ (i.e. it works like !=).


1.1.2. 3Áß ¿¬»êÀÚ


¿©±â¼­ ÀÌÁ¦ if ¹®¿¡ °üÇÑ °íÀüÀûÀÎ ¹æ¹ýÀ» ¹è¿ì°Ô µË´Ï´Ù. ÇÊÀÚ´Â ÀÌ °£´ÜÇÑ ¿¹½Ã ÀϺθ¦ ÅëÇØ º¸¿©ÁÖ°í ½Í½À´Ï´Ù. ÀÌ 3Áß ¿¬»êÀÚ¶ó´Â³ðÀº (Àâ´ã: 3Áß ¿¬»êÀÚ´Â 3ºÎºÐÀ¸·Î ³ª´µ¾îÁ³±â ¶§¹®¿¡ ÀÌ·¸µí °£´ÜÇÏ°Ô À̸§ÀÌ ºÙ¾ú½À´Ï´Ù) ±»ÀÌ if - else ºí·ÏÀ» ¸¸µéÁö ¾Ê¾Æµµ ½ÇÇàÇÒ ¼ö ÀÖµµ·Ï ¼³°èµÇ¾ú½À´Ï´Ù. ´ÙÀ½ÀÇ ¿¹¸¦ ÅëÇØ if ¹®À» 3Áß ¿¬»êÀÚ·Î ¹Ù²Ù´Â ¹æ¹ýÀ» º¸¿©µå¸®µµ·Ï ÇÏ°Ú½À´Ï´Ù:

if ('Yes' == 'No') then
   puts "Wrong!"
else
   puts "Yeah, baby!"
end
¡æ Yeah baby!

('Yes' == 'No') ? (puts "Wrong!") : puts ("Yeah, baby!")
¡æ Yeah baby!

3Áß ¿¬»êÀÚ´Â ¹°À½Ç¥ ´ÙÀ½¿¡ ÄÝ·ÐÀ¸·Î ±¸ºÐµÇ´Â »óŹ®À¸·Î ±¸¼ºµÇ¾î ÀÖ½À´Ï´Ù (µîÈ£ ¿¬»êÀÚ´Â if ¹®¿¡¼­ ¾²´Â ¹æ¹ýÀ̶û °°½À´Ï´Ù). °ýÈ£´Â ÄÝ·ÐÀ¸·Î ±¸ºÐµÇ´Â ¾ç ÀÚÀÇ °æ¿ì¿¡´Â º°·Î Áß¿äÇÑ °ÍÀÌ ¸øµË´Ï´Ù¸¸, °¡µ¶¼ºÀ» Çâ»ó½ÃÅ°°í °¢°¢ÀÇ °¥·¡¸¦ ´ÙÁ߶óÀÎÀ¸·Î ¼³°èÇÒ ¼ö ÀÖµµ·Ï ÇÕ´Ï´Ù. (i.e. ¿©´Â °ýÈ£¸¦ ½ÃÀÛ ÄÚµåÁÙÀÇ ¾Õ¿¡ ³õ°í ´Ý´Â °ýÈ£¸¦ ³¡³ª´Â ÄÚµåÁÙÀÇ ¸¶Áö¸·¿¡ ³Ö½À´Ï´Ù). ÀÌ·¯ÇÑ ´ÙÁ߶óÀÎ ÇൿÁö½Ã´Â º°·Î ÃßõÇÏ°íÇ ¹æ¹ýÀÌ ¾Æ´Õ´Ï´Ù. ¿Ö³Ä¸é ÀÌ·¯ÇÑ ¹æ¹ýÀ» »ç¿ëÇÔÀ¸·Î ÀÎÇØ ¾à°£ Äڵ带 ¸ø ¾Ë¾Æ¸Ô°Ô²ûÇϸç, if ¹®ÀÇ ºñ±³ °á°ú¿¡ ´ëÇÑ Å¸ÀÌÇÎÀ¸·ÎºÎÅÍ Çعæ½ÃÄÑÁÖÁö ¾Ê½À´Ï´Ù.

1.1.3. Unless

·çºñ¿¡¼­ unless »óŹ®Àº "¹ÝÀüµÈ" ¹®ÀÔ´Ï´Ù. ¿Ö "¹ÝÀüµÈ"À̶õ Ç¥ÇöÀ» ½è³Ä Çϸé if¿¡¼­´Â ÂüÀÏ°æ¿ì ÁÖ Äڵ带 ½ÇÇàÇÏÁö¸¸ unless ¹®¿¡¼­´Â else¹® ¾Æ·¡ÀÇ Äڵ带 ½ÇÇàÇϱ⠶§¹®ÀÔ´Ï´Ù. ¿¹¸¦ µéÀÚ¸é:

unless ("I am true." == "I am true."):
    puts "Something wonky!"
else
    puts "All is alright!"
end
¡æ All is alright!

"I am true." ´Â "I am true."¿Í °°À¸¹Ç·Î Á¶°ÇÀº ÂüÀÌ µË´Ï´Ù. ÁÖ ÄÚµå´Â ½ÇÇàÀÌ ¾ÈµÇÁö¸¸ else °¥·¡ ÀÌÇÏ ±¸¹®Àº ½ÇÇàµË´Ï´Ù. ÀÌ ±¸Á¶´Â ÄÚµå»ó¿¡¼­ if¹®ÀÇ °¡µ¶¼º Çâ»óÀ» Á¦¿ÜÇÏ°í´Â ´õÀÌ»ó Á¦°øµÇ´Â °ÍÀÌ ¾ø½À´Ï´Ù. (e.g. ³×°ÅƼºêÇÑ if¹®À» ¼öµµ¾øÀÌ °¥°Ü¾²´Â °ÍÀ» ¸·±â À§Çؼ­, ÀÌ´Â "ÀÌ°Ô ÀÌ°ÍÀÌ ¾Æ´Ï¶ó¸é ÀÌ°ÍÀ» Çضó"¶ó°í ÇÏ´À´Ï Â÷¶ó¸® "ÀÌ°Ô ¾Æ´ÑÇÑ ÀÌ°ÍÀ» Çضó"¶ó°í ¸»ÇÏ´Â°Ô ´õ ½±°í ÆíÇÒ °ÍÀÔ´Ï´Ù. ±àÁ¤ÀûÀÎ [http] chi¸¦ À¯ÁöÇÏ´Â °ÍÀº ·çºñÀÇ °¨¼ºÀû °Ç°­¿¡ ÀÖ¾î ¸Å¿ì Áß¿äÇÕ´Ï´Ù).

(°ýÈ£ ¾ÈÀ» ¹ø¿ªÇÏ°í º¸´Ï Çò°¥¸®´Â±º¿ä... µ¥Ã¼ ¹«½¼ ¸»Àΰí -_-;;)
if ¹®À» ÀÛ¼ºÇÒ ¶§ Á¶°Ç¹®À» ±àÁ¤ÇüÀ¸·Î ¾²¶ó´Â ¸»ÀÎ °Í °°³×¿ä. ºÎÁ¤ÇüÀÌ µé¾î°¥ °æ¿ì¿¡´Â Â÷¶ó¸® unless¸¦ ¾²¶ó´Â °ÍÀ̱¸¿ä. Àúµµ if¹®¿¡ °É¸° ºÎÁ¤Çü Á¶°Ç ¶§¹®¿¡ Çò°¥¸° ÀûÀÌ ¸¹³×¿ä ^^ ±×¸®°í chi°¡ ¹®¸Æ»ó Computer Human InteractionÀ» ¶æÇÏ´Â °Í °°¾Æ ¸µÅ© °É¾ú½À´Ï´Ù. --¾Æ¹«°³

1.1.4. »óŹ® º¯Çü

if¹®Àº ¶ÇÇÑ º¯ÇüÀÚ¶ó´Â ¸Å·ÂÀûÀÎ ¹®¹ýÀû Á¶¹Ì·á¸¦ Á¦°øÇÕ´Ï´Ù. À̸¦ »ý¼ºÇÏ´Â °ÍÀ¸·Î ÇÏ¿©±Ý if ¹®ÀÌ Á¶°Ç¹® ¸¶Áö¸·¿¡ Ç¥±âÇÒ ¼ö ÀÖµµ·Ï Çã¿ëÇÏ°í, if¹®ÀÌ ½ÇÇàµÇµçÁö ¾Æ´ÏµçÁö°£¿¡ ÀÏ´Ü Á¶°ÇºÎÅÍ ÆÇ´ÜÇϵµ·Ï ÇÕ´Ï´Ù. ´ÙÀ½À» º¸½Ã¸é.... :

thelma_louise = 13
puts "She's less than 15 alright!" if thelma_louise < 15
puts "She ain't more than 12, though!" if thelma_louise < 12
¡æ She's less than 15 alright!

º¸½Ã´Â ¹Ù¿Í °°ÀÌ ÁÙ ¸¶Áö¸·¿¡ if¹®À» À§Ä¡½ÃÄ×À»¶§µµ Äڵ带 ½ÇÇàÇϱâ Àü¿¡ ¸ÕÀú µÚ¿¡ Á¦½ÃµÈ Á¶°Ç ÆǴܹ®À» ÅëÇØ °ªÀ» ºñ±³ÇÑ ´ÙÀ½¿¡ ÀÌ°ÍÀÌ ÂüÀÎ °æ¿ì ÇØ´ç if¹® ¾Õ¿¡ À§Ä¡ÇÑ ½ÇÇ๮À» ½ÇÇà½Ãŵ´Ï´Ù. À§¿Í °°Àº ÄÚµå´Â ¾Æ·¡¿Í °°ÀÌ Á¤¸®ÇÒ ¼ö ÀÖ½À´Ï´Ù.:

if thelma_louise < 15:
   puts "She's less than 15 alright!"
end

ÀÌ·¸°Ô ÇÏ´Â °ÍÀÌ º¸´Ù °£°áÇÏ°í ¿ì¾ÆÇÑ ÇØ°á ¹æ¹ýÀÔ´Ï´Ù. ÀÌ°ÍÀÌ ÁøÁ¤ Ruby´Ù¿î °ÍÀÌÁö¿ä. ÀÌ ÄÚµå´Â ´ÙÀ½°ú °°ÀÌ begin/end ºí·ÏÀ» Ç¥½ÃÇÒ ¼öµµ ÀÖ½À´Ï´Ù:

begin
   puts "It's so true!"
end if (true == true)
¡æ It's so true!

ÀÌ·¯ÇÑ ¹æ¹ýÀº 500ÁÙ¿¡ ´ÞÇÏ´Â if ¹®À¸·Î ÀÌ·ç¾îÁø °Å´ëÇÑ ÄÚµåÀÇ ÀϺθ¦ Á¦¾îÇϱâ À§Çؼ­ Á¦½ÃµÈ ¸Å¿ì ¿ì¾ÆÇÑ ÇØ°áÃ¥ ÀÔ´Ï´Ù. (ƯÈ÷ end Å°¿öµå¸¦ ÅëÇØ ¾î´À ½ÃÁ¡¿¡¼­ if¹®À» ²÷¾î¾ß ÇÏ´ÂÁö ¸í·áÈ­ µË´Ï´Ù.).

1.1.5. case ±¸¹®

¸¸¾à ÀÌ°É ¾Ë¾ÆäÁö ¸øÇß´Ù¸é, Ÿ¼ö°¡ ÀûÀº ±ò²ûÇÑ ÄÚµåÀÇ ¸ðµç °ÍÀÌ ¿©±âÀÖ´Ù°í º¸°Ú½À´Ï´Ù (´©°¡ ÀÌ°Ô ¾Æ´Ï·¡µå¶ó..?). ´ëºÎºÐÀÇ º¯º¯Ä¡ ¸øÇÑ Äڵ忡 °°Àº ¼ö¸¦ ÀÚ²ÙÀÚ²Ù Áý¾î³Ö´Â if¿Í elsif µûÀ§º¸´Ù´Â ÀÌ°ÍÀÌ Á¡Á¡ »ç¶÷¼º°ÝÀ» ±ÞÇÏ°Ô ¸¸µé°í ³ªÁß¿¡´Â ÄÚµù¼Óµµ¸¦ Çâ»ó½Ãŵ´Ï´Ù. ±¸¹®ÀûÀ¸·Î ÄÚµå»ó¿¡ »ðÀÔÇϱ⠾ÆÁÖ ¸Å·ÂÀûÀÎ ÀÌ°ÍÀ» case ±¸¹®À̶ó ÇÕ´Ï´Ù. case ±¸¹®Àº ·çºñ¿¡¼­ µÎ ÇüÅ·ΠÁ¸ÀçÇÕ´Ï´Ù. ù¹ø° À¯ÇüÀº "Ç¥ÁØ" À¯ÇüÀÔ´Ï´Ù: "target" º¯¼ö¸¦ ³õ°í ÀÌ º¯¼ö¿¡ ´ëÇØ ´Ù¾çÇÑ °ªÀ» ³õ°í ºñ±³¸¦ ÇÏ°Ô µË´Ï´Ù.:

the_tao = 1234

case the_tao
    when 666: puts "That's such a lie!"
    when 1337
        puts "Liarrrr!"
    when 32767 then
        puts "Whatevurrrr!"
    else
        puts "You are harmonized with the Tao."
end
¡æ You are harmonized with the Tao.

ÀÌ·¯ÇÑ case ±¸¹®ÀÇ À¯ÇüÀº °¢°¢ÀÇ °æ¿ìµé¿¡ Á¦½ÃµÈ ´Ù¾çÇÑ °ª¿¡ ´ëÇÏ¿© °£´ÜÇÏ°Ô ºñ±³Çغ¾´Ï´Ù. case±¸¹®Àº when À̶ó´Â Å°¿öµå ´ÙÀ½¿¡ ¹®ÀÚ¿­ÀÌ ¿À°í ±× ´ÙÀ½ ÄÝ·ÐÀÌ µû¶ó¿À´Â ÇüÅ·ΠÀÌ·ç¾îÁ® ÀÖ½À´Ï´Ù. (¾î¶² À¯ÇüÀÌ ¾î¶² ÁÙÀÇ ´ÙÀ½¿¡ ½ÃÀ۵ǴÀ³Ä ÇÏ´Â ÀÌ·± µûÀ§ÀÇ °ÍÀº Áß¿äÇÑ °ÍÀÌ ¾Æ´Õ´Ï´Ù.); when Å°¿öµå´Â °¢°¢ÀÇ °æ¿ì¸¦ ºÐ¸®Çϸç, "end" Å°¿öµå´Â case±¸¹®À» Á¾·áÇϱâ À§ÇØ ÇÊ¿äÇÕ´Ï´Ù. °¢°¢ÀÇ °æ¿ì¿¡ ´ëÇØ À§¿¡¼­ºÎÅÍ Âß °Ë»çÇÕ´Ï´Ù. ¸¸¾à ÀÏÄ¡Á¡ÀÌ ¹ß°ßµÇ¸é ÇØ´ç °¡Áö¸¦ µû¶ó ½ÇÇàµÇ°í end·Î µû¶ó°¡¼­ ¹Ù·Î ºí·ÏÀ» ºüÁ®³ª¿À°Ô µË´Ï´Ù. ¸¸¾à ¾î¶² Á¶°Çµµ ÀÏÄ¡ÇÏ´Â °ÍÀÌ ¾ø´Ù¸é else Ç׸ñÀ» ½ÇÇàÇÕ´Ï´Ù. (if ºí·Ï°ú Á» ºñ½ÁÇÏ´Ù°í º¸½Ã¸é µÇ°Ú½À´Ï´Ù).

´ÙÀ½¿¡ ³ª¿À´Â µÎ¹ø° case ±¸¹®ÀÇ À¯ÇüÀº if ¹®°ú ºñ½ÁÇÏ°Ô »óŹ®À» ¸¸µé°í case¿¡ ¸í½ÃµÇ´Â ºñ±³´ë»óÀ» ¾ø¾Ö¼­ À¯¿¬¼ºÀ» °¡¹ÌÇÏ¿´½À´Ï´Ù. ½ÉÁö¾î´Â ºñ±³ ´ë»óÀÌ ¾ø¾îÁø´Ù ÇÏ´õ¶óµµ ¿©ÀüÈ÷ óÀ½ ³ª¿Â À¯Çüó·³ case¹®À» ±¼·Á¸ÔÀ» ¼ö ÀÖ½À´Ï´Ù.

enlightenment = 42

case
    when enlightenment > 60:
        puts "You are too hasty, grasshopper."
    when enlightenment < 40 or enlightenment == nil:
        puts "You are like the sloth, my friend. Diligence is key!"
    when enlightenment == 42:
        puts "Hello, Enlightened One."
    else
        puts "Yeah, not quite, pal. Maybe next time."
end
¡æ Hello, Enlightened One.

º¸½Ã´Â ¹Ù¿Í °°ÀÌ ÀÌ°Ç Ã¹¹ø° À¯Çü¿¡¼­Ã³·³ ÇÑ°¡Áö º¯¼ö¸¦ ´ë»óÀ¸·Î ºñ±³¿¬»êÀ» ÇÏÁö¸¸ ù¹ø° À¯Çü º¸´Ù °­·ÂÇÑ ºñ±³ ¿¬»êÀ» Áö¿øÇÕ´Ï´Ù. »ó´ÜÀÇ case¿¡¼­ ½ÃÀÛÇÒ¶§ ù¹ø° ºñ±³¿¬»ê¿¡¼­ ÀÏ´Ü ¿À¸¥ÂÊÀ¸·Î ±¸¹®À» ½ÇÇàÇÏ°í ³»·Á°©´Ï´Ù. ¹«½¼ ¾ê±â³Ä...¸é °æ¿ì°¡ ÀÏÄ¡ÇÒ¶§ (i.e. °æ¿ì°¡ ÂüÀ̵DZâ À§ÇØ ÇÊ¿äÇÑ ¸ðµç Á¶°ÇÀº ÁÙÁÙÀÌ ¿«ÀÎ »óŹ®°ú ¸¸³³´Ï´Ù.) °¡Áö¿¡ µþ¸° ±¸¹®À» ½ÇÇàÇÏ°í case±¸¹®À» ºüÁ®³ª¿É´Ï´Ù.

µÎ À¯Çü¿¡ ÀÖ¾î °øÅëÁ¡ÀÌ ÀÖ´Ù¸é "ÆøÆ÷¼ö À¯Çü"ÀÇ ½ÇÇà ±¸Á¶¸¦ °¡Áö°í ÀÖ´Ù´Â °ÍÀÔ´Ï´Ù. ´ëºÎºÐÀÇ ÇÁ·Î±×·¡¹Ö ¾ð¾î¿¡¼­µµ ¾Æ½Ã°ÚÁö¸¸ case ºí·ÏÀº ¾î¶² Å°¿öµå °°Àº°Ô ÇÊ¿äÇÕ´Ï´Ù. ( Á¾Á¾ break °°Àº °ÍµéÀÌ ±×·¸Áö¿ä :) ) ÀÌ°Ç °á±¹ ¹«½¼ ¾ê±â³Ä¸é ¾îÇø®ÄÉÀ̼ÇÀÌ ÀÌ Å°¿öµå¸¦ ¸¸³ª±â Àü±îÁö Á¶°ÇÀ» °Ë»çÇÏ°í ºí·ÏÀÌ ³¡³ª´Â ÁöÁ¡±îÁö ½ÇÇàÇÏ´Ù°¡ break°°Àº°É ¸¸³ª¸é ¾Æ¾ê case¸¦ ºüÁ® ³ª¿À°í break¸¦ ¾È ¸¸³ª¸é ±× ´ÙÀ½ case¾ÈÀÇ ±¸¹®À» ½ÇÇàÇÕ´Ï´Ù. ¿¹¸¦ µé¾î ´ÙÀ½ÀÇ C¾ð¾î ÄÚµåÀÇ ÀϺθ¦ º¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.:

int my_number = 42;

switch(my_number) {

    case 41:
        printf("A little higher...");
    break;

    case 42:
        printf("You have found the answer!\n");

    default:
        printf("And have fallen into a hole in the C compiler!\n");
    break;

}


¡æ You have found the answer! ¡æ And have fallen into a hole in the C compiler!

If it's not evident from the code, the break keyword will exit the switch block if it is encountered. This keyword is not needed in Ruby since it doesn't have fall through, but since C-style languages do, the default block (like Ruby's else in case statements) gets executed because there was no break in the block that evaluated to true (i.e. case 42). As helpful as the fall through mechanism is, it can really be quite annoying when the lack of one break statement causes your entire application to come to a screeching halt.

1.2. ¹Ýº¹±¸¹®

¹Ýº¹±¸¹®Àº Ÿ¼ö¸¦ ÁÙ¿©ÁÖ°í °°Àº ȤÀº À¯»çÇÑ ´Ù¸¥ Äڵ尡 ¿©·¯¹ø ¹Ýº¹µÇ¾î ½ÇÇàµÇ´Â ´ëºÎºÐÀÇ ÀÛ¾÷À» µµ¿ÍÁÖ¸ç ÇÊ¿äÇÑ µ¿ÀÛ¿¡ µû¸¥ »óÅ¿¡ ´ëÇØ ÀÀ´äÇØÁÝ´Ï´Ù. ¹Ýº¹±¸¹®Àº ¿©·¯¹ø¿¡ °ÉÃÄ ÀԷµǴ ³»¿ë¿¡ ´ëÇØ ¾î¶² Á¶°ÇÀ» ¸¸³ª±â Àü±îÁö ȤÀº °¢°¢ÀÇ ¼öÁý¹°¿¡ ´ëÇÑ ¿ä¼Ò¸¦ ó¸®ÇÏ´Â ±¸¹®ÀÇ ¹Ýº¹¿¡ ÀÖ¾î ÇÙ½ÉÀûÀÔ´Ï´Ù. ÀÌ ¼½¼ÇÀ» ÅëÇØ ¿ì¸®´Â À̵é À¯ÇüÀ» ´Ù ±îº¼ °ÍÀÔ´Ï´Ù.

1.2.1. Á¶°Ç ¹Ýº¹

Á¶°Ç ¹Ýº¹Àº Á¦½ÃµÈ Á¶°Ç¹®À» ¹ÙÅÁÀ¸·Î ½ÇÇàÇÕ´Ï´Ù (if³ª unless ºí·Ï°ú °°Àº °÷¿¡¼­ °°Àº Á¶°Ç¹®µéÀÌ »ç¿ëµÈ °æ¿ì). ÀÌ·¯ÇÑ ¹Ýº¹ À¯ÇüÀº µÎ°¡Áö·Î Á¸ÀçÇÕ´Ï´Ù: while °ú untilÀÌ ±×°ÍÀÌÁÒ. while ¹Ýº¹Àº ´ÜÁö while Á¶°ÇÀÌ ÂüÀÏ °æ¿ì¿¡¸¸ ½ÇÇàÇÒ °ÍÀÔ´Ï´Ù.:

x = 0
while(x < 10):
print x.to_s # print is like puts without a \n at the end
x += 1
end
¡æ 0123456789

°¢°¢ÀÇ ¹Ýº¹µ¿ÀÛ ¼öÇà½Ã, Á¶°ÇÀ» Ç×»ó °Ë»çÇϸç ÀÌ°ÍÀÇ °á°ú°¡ ÂüÀÎ °æ¿ì¿¡´Â ºí·Ï ¾ÈÀÇ ±¸¹®À» ½ÇÇàÇÕ´Ï´Ù. ±×°Ô ¾Æ´Ï¶ó¸é ºí·ÏÀ» ºüÁ®³ª°¡°í ±× ´ÙÀ½ ±¸¹®À» ½ÇÇàÇÕ´Ï´Ù.

while.jpg
[JPG image (14.83 KB)]

Figure 8: The while loop: 1973³âºÎÅÍ ½ÃÀÛµÈ Ç¥ÁØ.


µ¹·Á ¸»ÇÏÀÚ¸é, while¹®Àº ¹Ýº¹±¸¹® ²À´ë±â¿¡ if ±¸¹®À» ³õ°í if ±¸¹®ÀÌ °ÅÁþÀÌ µÉ¶§±îÁö ÁÖ Äڵ带 ½ÇÇàÇÏ´Â °Í°ú ¸¶Âù°¡Áý´Ï´Ù. ±×·¯³ª until ¹Ýº¹±¸¹®Àº if ´ë½Å unless¸¦ À§Ä¡ÇÑ °Í°ú °°Àº °á°ú ¾ç»óÀ» º¸ÀÔ´Ï´Ù:

x = 0
until(x >= 10)
print x.to_s
x += 1
end
¡æ 0123456789

°¢°¢ÀÇ ¹Ýº¹ °úÁ¤¿¡¼­ Á¶°ÇÀº ´ÜÁö while ¹Ýº¹±¸¹®°ú ºñ½ÁÇÏ°Ô °Ë»çµÇÁö¸¸, until ±¸¹®¿¡¼­´Â Á¶°ÇÀÌ ¸¸Á·ÇÏÁö ¾Ê´Â µ¿¾È, Áï if ±¸¹®ÀÌ °ÅÁþÀÌ µÇ´Â µ¿¾È ½ÇÇàµË´Ï´Ù.


until.jpg
[JPG image (55.47 KB)]

Figure 9: until±¸¹®ÀÌ while±¸¹®º¸´Ù °ü½ÉÀÌ ´ú Çؼ­ whileÀ» ½Ã»ùÇϴ°Š°°Àºµ¥...?


unlessÁ¶°Ç°ú °ÅÀÇ °°°Ô, until ¹Ýº¹±¸¹®¿¡¼­´Â Á¶°ÇÀÌ °°À¸¸é ºí·Ï ±¸¹®À» ³Ñ¾î°©´Ï´Ù.

ÀÌ ¹Ýº¹±¸¹®µéÀº ¹Ýº¹Àû ¼öÇà¿¡ ¸Å¿ì À¯¿ëÇÕ´Ï´Ù(°°Àº ÀÛ¾÷À» ¿­¹ø ¹Ýº¹ÇѴٰųª, ¼ø¼­´ë·Î µ¥ÀÌÅ͸¦ Ãâ·ÂÇѴٰųª, ¿¹Á¦¿¡¼­¿Í°°ÀÌ °è¼Ó ÇÒ °æ¿ì)¸¸, ÇÔÁ¤ÀÌ ¼û¾î ÀÖ½À´Ï´Ù. óÀ½ ±¸¹®À» ½ÇÇàÇϱâÀü Á¶°ÇÀÌ ºí·°¾ÈÀÇ ±¸¹®À» ¼öÇàÇϱâ À§ÇÑ »óŸ¦ ¸¸³ªÁö ¸øÇÑ´Ù¸é(whileÀ» À§ÇÑ ÂüÁ¶°ÇÀ̳ª untilÀ» À§ÇÑ °ÅÁþÁ¶°Ç °°Àº°Í...) ¹Ýº¹±¸¹®Àº Àý´ë·Î ½ÇÇàµÇÁö ¾Ê½À´Ï´Ù. °ªÀ» Á¦¾îÇÏ´Â ¹Ýº¹±¸¹® ¹Û¿¡ ¶Ç´Ù¸¥ ¹Ýº¹±¸¹®ÀÌ ±× °ªÀ» Á¦¾îÇÏ´Â °æ¿ìÀÇ °¡´É¼ºÀ» ÀÎÁöÇÏ´Â °ÍÀº ·çÇÁ¸¦ ½ÇÇàÇÏ°í ³­ ´ÙÀ½ÀÇ ÀÀ¿ëÇÁ·Î±×·¥ ½ÇÇà¿¡ ÀÖ¾î Áß¿äÇÕ´Ï´Ù. ÀÌ Á¶°ÇÀº ¾î¶² ¹®Á¦¸¦ ¾ß±âÇÒ ¼öµµ ÀÖ°í ±×·¸°Ô ÇÒ °ÍÀÔ´Ï´Ù. ±×·¡¼­ º¯¼ö¿¡ ´ëÇØ Áø¸®(Âü°ÅÁþ)¸¦ ¹Ýµå½Ã ¹Ì¸®¹Ì¸® È®ÀÎÇØ¾ß ÇÕ´Ï´Ù.

¾î¶»°Ôµç ±×·°Àú·° ºÁÁÙ¸¸ÇÑ ´õ¿í ¶Ç¶óÀÌ°°Àº ¸Ó¸®¸¦ Ä¡Äѵ鸸ÇÑ ¶Ç ´Ù¸¥ ÇÔÁ¤Àº Á¶°Ç¹®ÀÌ ¸ØÃßÁö ¾ÊÀ» ¼ö ÀÖ´Ù´Â °ÍÀÔ´Ï´Ù. (while ¹Ýº¹¹®ÀÌ °ÅÁþ¼Ò°Ç¿¡ µµ´ÞÇÏÁö ¾Ê´Â´Ù¸é...?) ±×·¡¼­ ¹«ÇÑ ¹Ýº¹¿¡ ºüÁ®µé°Ô ÇÏ°í ÀÌ°ÍÀ» È¥¶õ½º·¯¿òÀÇ Ã¢Á¶·Î À¯µµÇÏ°Ô µË´Ï´Ù. °á±¹ ½ÇÁ¦ ÀÌ·¯ÇÑ ÀÏÀÌ ÀϾ´Ù¸é ¸¸¹°À» Ãà¾àÇÒ ¼öµµ ÀÖÁö¸¸ Àü ÀÌ°É °ËÁõÇÏÁö ¸øÇÕ´Ï´Ù. ±×·¡¼­ ÀÌ°É ¾î¶»°Ô Çϸé ÀÌ·¯ÇÑ »çÅ·κÎÅÍ ¿¹¹æÇÒ ¼ö ÀÖÀ»±î¿ä? Ãʺ¸Àڵ鿡°Ô´Â, °¡´ÉÇÑÇÑ Á¶°Ç¹®À» À¯¿¬ÇÏ°Ô »ç¿ëÇÏ´ÂÁö È®ÀÎÇ϶ó°í ÇÏ°í ½Í½À´Ï´Ù. (¿¹¸¦ µéÀÚ¸é >= ¿¬»êÀÚ°¡ == ¿¬»êÀÚº¸´Ù ³ªÀ» ¼ö Àִµ¥ ÀÌ°ÍÀº ¼ö¸¦ Á¡Á¡ Å©°Ô ¼¼¾î³ª°¥¶§ °íÀÇÀûÀ¸·Î °èȹÇß´ø ¼ö ÀÌ»óÀ» ³Ñ±â´Â °ÍÀ» ¸·±â À§ÇÔÀÔ´Ï´Ù. ÀÌ°Ç ¸ðµç »óȲ¿¡¼­ ÃÖ¼±ÀÇ ¹æÃ¥ÀÌ µÉ¼ø ¾øÁö¸¸, ÃæºÐÈ÷ °í·ÁÇÒ¸¸ÇÕ´Ï´Ù.) ±×¸®°í »óÅ°ªÀ» ÅëÇØ ÄÚµå·Î »óÈ£ÀÛ¿ë µ¿ÀÛÀ» ÃëÇÏ¿© È®ÀÎÇϽñ⠹ٶø´Ï´Ù(»óÅ¿¡ ¿µÇâÀ» ÁÖ±â À§ÇÑ ±× ¾î¶² ÇàÀ§¿¡ ´ëÇØ È®ÀÎÇÑ´Ù¸é ¾Æ¸¶ ´ëºÎºÐÀÇ »ç¶÷µéÀÌ ÀÌ°É ±î¸Ä°Ô ÀØ°í ÀÖ´Ù´Â »ç½Ç¿¡ ¹«Ã´ ³î¶ó½Ç°Ì´Ï´Ù).

¸¸¾à ÃÊ°í¼ö über‐1337 ÇÁ·Î±×·¡¸Ó¿´´Ù¸é Àڽſ¡°Ô ÀÌ·¸°Ô ¸»ÇßÀ»Áöµµ ¸ð¸£°Ú½À´Ï´Ù. "Yo, ¶Ç¶óÀÌ! ³­ ±×µý°Å¿¡ °ÆÁ¤ÇÏÁö ¾Ê¾Æ ¿Ö³Ä¸é ³ª´Â ±×... Áö¶ö ¹æÁ¤¸ÂÀ» for ·çÇÁ(fo' loops fo' rizzle)... ¸¦ ¾²°Åµç~~!" ¾Æ¸¶ ¸Â´Â ¸»ÀÏÁöµµ ¸ð¸£°í ½ÇÁ¦·Îµµ ·çºñ¸¦ ´Ù¸¥ ¾ð¾î¿¡¼­ ¼ö¿ëÇϵµ·Ï ³»¹ö·Á µÎ¾úÀ» °ÍÀÔ´Ï´Ù. ³×!! ·çºñ´Â ºñ·Ï ÀüÅëÀû °³³äÀÇ "for loop" ¸¦ Á¦°øÇÏ´Â°Ç ¾Æ´ÏÁö¸¸, ¾î·µç for Å°¿öµå »ç¿ëÀ» Á¦°øÇÕ´Ï´Ù.

(¿øÀÛÀÚÀÇ ¸»Åõ°¡ Á» °ÅĨ´Ï´Ù... ¹ø¿ª»ó ÀÌÇظ¦ -_-)

1.2.2. ·çÇÁ¿Í ºí·ÏÀÇ ¹Ýº¹

°³¹ßÀڷμ­ÀÇ ³» Àλý¿¡ ÀÖ¾î, ¸»µµ ¾ÈµÇ´Â ÄÚµåÀÇ ÀϺθ¦ ²Ï ¸¹ÀÌ º¸¾Æ¿Ô½À´Ï´Ù. ÅؽºÆ® ÆÄÀϷκÎÅÍ ¿À´Â ¸ðµç °ÍÀ» »ç¶÷µé¿¡°Ô ±×µéÀÇ ÀÀ¿ëÇÁ·Î±×·¥ °³¹ß °úÁ¤¿¡ ÀÖ´Â µ¿¾È "Àû¾îµµ 17¹ø ¹ÙÄû¸¦ ´Ù½Ã ¹ß¸íÇϵµ·Ï ÇÏ´Â °Í°ú °°ÀÌ" ¾öû³­ µ¥ÀÌÅ;çÀÌ È帣´Â µ¥ÀÌÅͺ£À̽º¿Í °°ÀÌ »ç¿ëÇϵµ·Ï ÇÑ´Ù´Â °ÍÀÔ´Ï´Ù. (Á¤¸®ÇÏÀÚ¸é, ÅؽºÆ® ÆÄÀÏÀ» ´Ù·é´Ù´Â °Í ÀÚü°¡ ±×¸® È¿À²ÀûÀÎ °ÍÀÌ ¾Æ´Ï¶ó´Â °ÍÀÔ´Ï´Ù. ¿ªÀÚ ÁÖ.) (¸ðµç °³¹ßÀÚ, ÇÁ·Î±×·¡¸Ó, ÇØÄ¿µé·ÎºÎÅÍ ³ª¿Â ¾ê±é´Ï´Ù: ´ëºÎºÐÀÇ ¾ð¾î°¡ ³¯Â¥/½Ã°£ Á¦¾î·çƾÀ» °¡Áö°í Àִµ¥, ÀÌ ¾ê±â´Â °á±¹ and/or ¸ðµâÀ» »ç¿ëÇÒ ÇÊ¿ä°¡ ¾ø´Ù´Â °ÍÀÔ´Ï´Ù. Âü °í¸¿³×¿ä). ÀÌ »ç¶÷µéÀº ´ÜÁö Á¤Á÷ÇÏ°Ô ±×µé ÀڽŰú ±×¿Ü ´Ù¸¥ »ç¶÷µéÀ» À§ÇØ ´õ ¾î·Æ°Ô...°í»ýÇؼ­ ¸¸µç °ÍÀÔ´Ï´Ù. ÀÌ·¯ÇÑ ¹æ¹ýÀÇ Á¾·ù·Î ÇÏ¿©±Ý Àú´Â ´ÜÁö ¾ð¾îÀÇ È¯°æÀ» Á»´õ ³´°Ô Çϱâ À§ÇØ ¾ð¾î»óÀÇ »óÅ·çÇÁ¸¦ "ÈÉÃÄ"¾²´Â »ç¶÷µéÀ» º¼¶§ Á¦°¡ ´À³¢´Â ¹ÙÀÔ´Ï´Ù. ¿¹Á¦¸¦ Çѹø º¸µµ·Ï ÇÏÁö¿ä

my_array = [13, 1, 4, 5, 29]

i = 0

while (i < my_array.length)
    print my_array[i].to_s + " "
    i += 1
end

¡æ 13 1 4 5 29

µ¿ÀÛÀÌ¾ß ÇÏ°ÚÁö¸¸ ÀÌ°Ç ÃÖ»óÀÇ Á¤´äÀÌ ¾Æ´Õ´Ï´Ù. (ƯÈ÷ ·çºñ¿¡¼± ´õ¿í ±×·¸½À´Ï´Ù). ·çºñ´Â ÀÌ·¯ÇÑ ÁýÇÕ¿ä¼Ò¸¦ ¾ÈÀüÇÏ°í, ½±°í ´É·üÀûÀ¸·Î ¹Ýº¹Çϴ ¦¦ÀÌÀÇ ¹æ¹ýÀ» Á¦°øÇÕ´Ï´Ù. _-_); ÀÌ°ÍÀÌ ·çÇÁ ¹Ýº¹ÀÔ´Ï´Ù. À̰͵鿡 À־ ù¹ø°´Â for ·çÇÁÀÔ´Ï´Ù. ·çºñ¿¡¼­ÀÇ for ·çÇÁ´Â ´ÙÀ½¿¡ Á¤¸®µÈ ¹Ù¿Í °°ÀÌ C³ª C·ÎºÎÅÍ ÆÄ»ýµÈ ¾ð¾î°è¿­¿¡¼­ÀÇ for·çÇÁ¿Í´Â ºñ½ÁÇÏÁö ¾Ê½À´Ï´Ù. ±×·¯³ª ÀÌ·¯ÇÑ Á¡À¸·Î ÀÎÇØ Àú´Â ÀÌ°ÍÀÌ ¹«ÇÑÇÑ À¯¿ë¼ºÀ» Áö´Ï°í ÀÖ´Ù°í º¾´Ï´Ù.

·çºñ¿¡¼­ÀÇ for ·çÇÁ´Â Python¿¡¼­ÀÇ for·çÇÁ¿Í ¸Å¿ì (°ÅÀÇ ÀÌ»óÀûÀ¸·Î) À¯»çÇÏ´Ù°í º¾´Ï´Ù. ÄÝ·º¼Ç(ÁýÇÕ?) À̶ó´Â °ÍÀÌ Á¦°øµÇ°í for·çÇÁ·Î ÇÏ¿©±Ý º¯´ö½º·¯¿î °¢°¢ÀÇ ¾ÆÀÌÅÛÀ» ¹Ýº¹ÀûÀ¸·Î Àâ¾Æ³À´Ï´Ù.

for.jpg
[JPG image (25.28 KB)]

Figure 10: ¾î¶² ÁýÇÕÀû ¿ä¼Ò°¡ »ç¿ëµÉ¶§ ¹Ýº¹ÀÚ´Â »óÅ·çÇÁº¸´Ù ½±°í ¾ÈÀüÇÏ°Ô ´Ù·ì´Ï´Ù.


´ÙÀ½ ÄÚµå ¿¹Á¦¸¦ Âü°íÇغ¾½Ã´Ù! ¿ì¸®´Â ÀÌÁ¦ ¦¼ö ¹è¿­À» ¸¸µé°í ¹Ýº¹ÀÚ¸¦ »ç¿ëÇÏ¿© À̸¦ Ãâ·ÂÇÒ °ÍÀÔ´Ï´Ù.

my_evens = [2,4,6,8,10,12,14]

for my_int in my_evens
   print my_int.to_s + ", "
end
¡æ 2, 4, 6, 8, 10, 12, 14,

ÀÌ ¾ê±â´Â °á±¹ "my_evens¿¡ ÀÖ´Â °¢°¢ÀÇ °´Ã¼¸¦ À§ÇØ my_intfmf I want to create a local variable my_int for each object in my_evens and do something with it." Every time the loop iterates, the next value in my_evens is copied into the variable my_int; this variable allows you to manipulate the item in the collection easily (and, since the original object is copied into that variable, you can manipulate it without risk of bit twiddling in the original collection).

The for loop is really just salacious syntactic sugar for an iterator; an iterator is simply a block of code that iterates over a collection provided by the each method of a class. For example, let's look at the above example in the iterator form:

my_evens = [2,4,6,8,10,12,14]

my_evens.each do |my_int|
print my_int.to_s + ", "
end
¡æ 2, 4, 6, 8, 10, 12, 14,

An iterator is formed by creating a begin/end block (discussed above) but using do/end as the initiator; you are essentially creating a special type of begin/end block that will be iterated for each item in the collection. This type of loop naturally guards against the variable discord that you may encounter by incrementing an index using something like a while loop (i.e. you go over the value you want if using the == operator, your value never gets above the needed value if using a < operator, etc.) while also providing a more natural way to access the value you wanted. Unfortunately this convenience comes at the price of flexibility. In a while or until loop you can alter how quickly your conditional reaches the state that allows the loop to break (also called step) by adding more than 1 to the value you're keeping track or something like that; this ability is severely limited in an iterator loop (i.e. it doesn't exist unless you want to use the next method somehow to make it happen). Keep this in mind when you decide which type of loop to use; it can greatly affect the performance and stability of your application.

1.2.3. Statement Modifiers

Loops offer a construct very similar to the if statement's modifier. Loops are great ways to save time and code, but they aren't very "natural"; I mean when repeating a task (such as banging your head into a mirror out of sheer frustration) you typically don't think in the form of a loop construct.

while(self.conscious == true)
   self.head_bang("mirror")
end

That's simply not how our mind usually works, and fortunately, Ruby, in all of its readable glory, has decided to bless us with the loop statement modifier construct. For example:

self.head_bang while self.conscious == true

This construct is not only more natural to read and say, but it is also more concise; you still have all the same functionality but in much less code and hassle. This construct works with both while and until loops (but not any of the iterating loops); it also works with begin/end blocks which allows you to do post-test loops (i.e. loops that always execute at least once before checking the conditional as opposed to the pre-test loops that we've been using). For example, if you wanted to output the English translation of "Tora! Tora! Tora!" you could write:

counter = 0

begin
   print translate("Tora! ")
   counter += 1
end until counter == 3
¡æ Briefcase! Briefcase! Briefcase!

In this setup, the block will always execute at least once regardless of the truth of the attached conditional (unlike if you simply use the while or until loop). Do keep in mind when using this construct that you are still, at its core, using the while and until loop, which means that it is still susceptible to the same pitfalls and problems with variables. Make sure to test your application thoroughly for any potential problems related to this.

1.2.4. Controlling Loops

Does it seem that loops have your application in a strangle hold? They do seem to be hulking, domineering, unstoppable behemoths that stop only when they darn well feel like it. Well, actually they probably just seem like really useful constructs that are hard to control without artificial bit twiddling in the conditional. For example:

my_x = 115
my_y = 40
temp = 0

while(my_x < 150)
    if (my_x % my_y) == 0: # if the quotient is even
        temp = my_x
        my_x = 151
    else
        my_x += 1
    end
    puts my_x
end

my_x = temp
puts my_x
¡æ 120

That's a bit dangerous if you ask me (and if you're reading this, you just might); artificially altering the conditional value can lead to some craziness in your application (i.e. accidentally skipped code, the variable being used outside of the loop without the temp value being stored back into it, etc.). In Ruby, there are various keywords that allow you control the flow of loops. The next keyword allows you to skip the current iteration, while the break keyword allows you to exit the current loop completely. Let's look at our previous example again:

my_x = 115
my_y = 40

while(my_x < 150)
    my_x += 1
    puts my_x
    if (my_x % my_y) == 0: # if the quotient is even
        break
    else
        next
    end
end

puts my_x
¡æ 120

The usage of the next keyword in this example is rather inordinate (i.e. I should have simply let the loop iterate again rather than forcing it), but I wanted to demonstrate how the next keyword works. This loop works just as before, except more concise and less bloated.



Figure 11: Break. I always get a great degree of satisfaction from breaking a loop. Take that sucker!


The break keyword breaks the loop and continues on to the code after the loop just as if the conditional had been met. The next keyword skips past all remaining code (which in this example wasn't much) to the end of the loop and continues on to the next iteration. But, wait! It gets fancier:

my_x = 115
my_y = 40

while(my_x < 150)
    my_x += 1
    puts my_x
    break if (my_x % my_y) == 0 
    next
end

puts my_x
¡æ 120

You can use a conditional modifier with the break or next keywords! And, yes, the next keyword isn't necessary, but I wanted to demonstrate two things. Firstly, the keyword's usage (I know you probably understand it by now, but another example never hurt anybody!). Secondly, all code after break is skipped. The loop does not iterate again because the conditional attached to break was satisfied.

1.3. ¿¹¿Ü

ÀÚ, ±×·¡¼­ ÀÌÁ¦ ÇÁ·Î±×·¥À» ¸¶¹«¸® ÇÏ·Á´Âµ¥, °©ÀÚ±â ÀÌ°Ô ¹ÌÄ£ ¿¡·¯°¡ ¹ú¶±¹ú¶±°Å¸®µå´Ï¸¸... ÀÌ°Ô ´ç½ÅÀÌ ¾î¶»°Ô ÇØ¾ß ÇÒÁö ¸ð¸£°Ô °¡·Î¸·°í Àִ٠Ĩ½Ã´Ù. À½.. ´Ù¸¥ Ä£±¸µéº¸´Ù ¿©µå¸§ÀÌ ¾öû³ª¼­ »çȸÀûÀ¸·Î ¿Õµû¸¦ ´çÇعö¸° -_- ¢±ÄÀº 13»ìÂ¥¸® ²¿¸¶ ¾ÖµéÀÌ "YO d00D I"M 1337!!11"¿Í °°Àº ¹æ½ÄÀ¸·Î ¼ýÀڷθ¸ ÀÌ·ç¾îÁø ÇÁ·Î±×·¥¿¡ Ä¡¸íÀû Æı«¸¦ ÀÔÈú ¹®ÀÚ¿­?À» ÀÔ·ÂÇÏ±æ ¿øÇÑ´Ù ÇսôÙ. ÇÁ·Î±×·¥ÀÌ ±úÁö°Ô ±×³É ³»¹ö·Á µÎ½Ã°Ú½À´Ï±î? ¿µ¿øÇÑ Àç¿Í ¿¬±â·Î ³²±æ ¹Ù¶ø´Ï±î? ¾Æ¸¶ º°·Î ȯ¿µÇϽÃÁö ¾ÊÀ» °Ì´Ï´Ù. ¿Ö³Ä¸é ½Ã°ñ¶ß±â ¸¶À»·ÎºÎÅÍ ¿Â "TO01337ANdY"... IA? µÎ·Á¿ö ÇÏÁö ¸¶¼¼¿ä. ¾î¸° ÃʽÉÀÚ ÀÔ´Ï´Ù! ·çºñ´Â ÀÌ¹Ì ´ç½ÅÀÌ ¹ß°ßÇÑ °ÍÀ» Áö³æ½À´Ï´Ù... ±×°Ô ¹¹³Ä¸é... ±×°Ô ¹¹³Ä¸é...

¿¡·¯¿Í ÀÌ¿Í °ü·ÃµÈ ´Ù¸¥ ¹®Á¦¸¦ ÇÁ·Î±×·¥ ³»¿¡¼­ ÀüÇüÀûÀÎ ¹æ¹ýÀ» ÅëÇØ ´Ù·ç±â À§ÇØ ¿¹¿Ü¶ó´Â °£´ÜÇÑ ¹æ¹ýÀ» Á¦°øÇÕ´Ï´Ù. ¸ðµç ¿¹¿Ü´Â ExceptionÀ̶ó´Â ±â¹Ý Ŭ·¡½º·ÎºÎÅÍ »ó¼ÓµË´Ï´Ù. ·çºñ´Â ´ç½ÅÀÌ »ç¿ëÇϵµ·Ï (¶Ç´Â ¾îÇø®ÄÉÀ̼ǻóÀÇ ¹®Á¦¸¦ Á¦¾îÇϵµ·Ï) ¼ö¸¹Àº ³»Àå ¿¹¿Ü °´Ã¼¸¦ Á¦°øÇÕ´Ï´Ù. ÇöÀç »ç¿ë°¡´ÉÇÑ ³»Àå ¿¹¿Ü Ŭ·¡½º¿¡ °üÇÑ Ç¥°¡ ¾Æ·¡¿¡ ÀÖ½À´Ï´Ù.

1.3.1. Handling Exceptions

¿¹¿Ü°¡ ¹ß»ýÇßÀ»¶§ À̸¦ Á¦¾îÇϱâ À§Çؼ­´Â º¹±¸ ºí·ÏÀ» »ý¼ºÇØ¾ß ÇÕ´Ï´Ù. Á¦½ÃµÈ ¿¹Á¦¸¦ Àß ºÁÁֽðí, ¹è°íÇ µ¶¼ö¸®Ã³·³ À̸¦ È® ÀÚ±âÀÇ °ÍÀ¸·Î ³¬¾Æä½Ã±â ¹Ù¶ø´Ï´Ù. (¾Æ´Ï¸é ·çºñ¿¡¼­ ¾î¶»°Ô ÇÏ´ÂÁö Á¤¸»·Î ¾Ë°í ½ÍÀº »ç¶÷󷳿ä):

def method_of_doom
    my_string = "I sense impending doom."
    my_string.ah_ha_i_called_a_nonexistent_method
end

method_of_doom
¡æ ! NoMethodError

ÀÌÁ¦ ¿¹¿Ü ¼Ó¼ºÀ» Á¦¾îÇغ¾´Ï´Ù.:

def method_of_doom
   my_string = "I sense impending doom."
   my_string.ah_ha_i_called_a_nonexistent_method
rescue Exception:
   puts "Uhh...there's a problem with that there method."
end

method_of_doom
¡æ Uhh...there's a problem with that there method.

º¹±¸ ºí·ÏÀ» »ý¼ºÇϱâ À§ÇØ ´ÜÁö Exception Ŭ·¡½º ¾Õ¿¡ rescue¶ó´Â Å°¿öµå¸¦ Ãß°¡Çϱ⸸ ÇÏ¸é µË´Ï´Ù. (·çºñ´Â ¼ö¸¹Àº ³»Àå ¿¹¿Ü¸¦ Á¦°øÇÕ´Ï´Ù. Àüü ¸ñ·ÏÀ» Âü°íÇÏ½Ã°í ½ÍÀ¸½Ã´Ù¸é ·çºñ ¹®¼­¸¦ È®ÀÎÇغ¸¼¼¿ä). Á¦°¡ ¿Ö ExceptionÀ» ½è³Ä¸é ÀÌ°ÍÀÌ ÀϺΠȤÀº ¸ðµç ¿¹¿Ü¸¦ ÀâÁö¸¸, ÀÌ Á¦½ÃµÈ Çϳª¸¸À¸·Îµµ ´Ù¸¥ ¹æ½ÄÀ¸·Î ´Ù¸¥ ÇüÅÂÀÇ ¿¹¿Ü¸¦ Á¦¾îÇϱâ À§ÇØ ¼ö¸¹Àº º¹±¸ ºí·ÏÀ» Á¤ÀÇÇÒ ¼ö ÀÖ°í, ȤÀº ´ÜÀÏ º¹±¸ ºí·ÏÀ» ÅëÇØ ´Ù¾çÇÑ ¿¹¿Ü À¯ÇüÀ» Á¦¾îÇÒ ¼ö ÀÖ½À´Ï´Ù.

def method_of_doom
   my_string = "I sense impending doom."
   my_string.ah_ha_i_called_a_nonexistent_method
rescue NoMethodError:
   puts "You're missing that method, fool!"
rescue Exception:
   puts "Uhh...there's a problem with that there method."
end

method_of_doom
¡æ You're missing that method, fool!

´ÙÇü¼º º¹±¸ ºí·ÏÀ» Á¤ÀÇÇß´Ù¸é ¾Æ¸¶ ·çºñ´Â ¹ß»ýÇÑ ¿¹¿Ü¿Í °°Àº ÇüÅÂÀÇ ¿¹¿Ü¸¦ Á¦¾îÇÒ ¼ö Àִ óÀ½ ºÎºÐÀÇ ¿¹¿Ü º¹±¸ ºí·ÏÀ» µû¶ó°¥ °ÍÀÔ´Ï´Ù. ÀÌ ¿¹Á¦¿¡¼­ Exception ºí·ÏÀÌ NoMethodErrorÀ§¿¡ À§Ä¡ÇÏ°í ÀÖ¾ú´Ù¸é óÀ½ÀÇ ¿¹Á¦¿Í °°Àº °á°ú¸¦ º¼ ¼ö ÀÖ°Ô µÉ °ÍÀÔ´Ï´Ù. ¸¸¾à ÇöÀç½ÃÁ¡¿¡¼­ »ç¿ë ºÒ°¡´ÉÇÑ º¹±¸ ºí·ÏÀÌ ¹ß°ßµÇ¾ú´Ù¸é (i.e. ÄÚµåÀÇ ÇöÀç ºí·Ï¿¡¼­µçÁö ÇöÀç ½ÇÇà Æ÷ÀÎÅÍ°¡ À§Ä¡ÇÏ°í ÀÖ´Â ¸Þ¼Òµå¶óµçÁö µîµî), ·çºñ´Â ¾î¿ï¸®´Â º¹±¸ ºí·ÏÀ» ãÀ» ¼ö¸¸ ÀÖ´Ù¸é À̸¦ º¸±â À§ÇØ ½ºÅà »ó´ÜÀ¸·Î È£ÃâÇÏ°Ô µÉ °ÍÀÔ´Ï´Ù (i.e. it works its way from the offending method up to the method called it to the method that called it and so on). ¸ÞÀÎ ÀÀ¿ëÇÁ·Î±×·¥ ¸Þ¼Òµå¸¦ Ž»öÇϱâ Àü¿¡ ÀûÀýÇÑ º¹±¸ ºí·ÏÀ» ãÁö ¸øÇß´Ù¸é ÀÀ¿ëÇÁ·Î±×·¥Àº ½º·¹µå·ÎºÎÅÍ ºüÁ®³ª°¡°Ô µÇ°í ¾Æ¸¶ ´ÙÀ½°ú °°Àº ÇÑÁÙ "¿¹¿Ü Çڵ鷯¸¦ »©¸Ô´Â ÀÌ·± ¸ÛûÀÌ¿¡°Ô Âü À¯°¨ÀÌ´Ù!" À» ³»¹ñÀ¸¸ç Á¤ÀûÀ» ±ú´Â ½Ã²ô·¯¿î Ãæµ¹ µûÀ§°¡ µÚµû¸£°Ô µÉ°Ì´Ï´Ù... ůů...

º¹±¸ ºí·ÏÀº »ó¼¼È÷ ±â¼úµÈ ´Ù¾çÇÑ ¿¡·¯¸¦ Àâ±âÀ§ÇØ ´Ù¾çÇÑ À̸§ÀÌ ¸¸µé¾îÁ³À»¶§ ÀÌ¿¡ ´ëÇØ Á¤ÀÇÇÒ ¼ö ÀÖ½À´Ï´Ù. ´Ù½ÃÇѹø ¾ÕÀÇ ¿¹Á¦¸¦ ÀοëÇغ¸µµ·Ï ÇÏ°Ú½À´Ï´Ù:

def method_of_doom
   my_string = "I sense impending doom."
   my_string.ah_ha_i_called_a_nonexistent_method
rescue NoMethodError => e:
   puts "PROBLEM: " + e.to_s
rescue Exception:
   puts "Uhh...there's a problem with that there method."
end

method_of_doom
¡æ PROBLEM: undefined method and so on...

Leveraging this ability makes it far easier to know exactly what's going on, where, and sometimes how to fix it.

A rescue block also provides a few other features that aid in making sure that even if your application does choke, it will still hopefully run as smoothly as possible. The first of these features is the else caluse that you can tag on to a rescue block. This block will execute if there are no exceptions raised. For example:

def no_problemo
   x = 0
   x += 19
rescue Exception
   puts "Oh noes!"
else
   puts "All clear!"
end

no_problemo
¡æ All clear!

This is a useful feature, but be careful what you put in there. The rescue block in the enclosing code won't catch any exceptions raised in the else clause, so you may need to catch them later up the call stack or relegate the else block to menial tasks to avoid the risk of causing worse problems.

The second feature offered by rescue blocks is the ensure clause; this clause holds code that is always executed (i.e. regardless of the presence exceptions or not). For example:

def dance_a_jig
   "I'm a dancin'!"
   "Do si do!"
   rebel_yell = "yee haw!".upcase!
rescue Exception
   print "I fell down, dang it!"
else
   print rebel_yell
ensure
   print " That's all folks!"
end

dance_a_jig
¡æ YEE HAW! That's all folks!

The ensure clause is always executed no matter what; this construct is very useful for closing files that you have been reading from or writing to, closing open network connections, or making sure all your resource handles have been cleaned up. If you put these sorts of things in ensure clauses, they will always get done and cut down on problems you may have with resource access if your application crashes.

1.3.2. Rescue Statement Modifier

»óÅÂÁ¦¾î¿Í ·çÇÁ¹®°ú °ÅÀÇ À¯»çÇÏ°Ô, rescue ºí·Ï ±¸¹®Àº »óÅ ¼öÁ¤ÀÚ·Î ¾²ÀÏ ¼ö ÀÖ½À´Ï´Ù:

not_an_object.do_something rescue puts "Crash!"
¡æ Crash!

rescue ¸¦ ÅëÇؼ­ ¾î¶² Á¾·ùÀÇ ¿¹¿ÜÀÎÁö Á¤ÀÇÇÒ ¼ö ¾øÁö¸¸, ¾Æ¹«¸®Çصµ "Çü½ÄÀûÀ¸·Î" rescue ºí·ÏÀÇ ¿ÞÂÊ¿¡ ³õÀÎ °É·Î ÇÏ´Â °ÍÀÌ ³´°ÚÁö¿ä. ¶ÇÇÑ ÀÌ·¯ÇÑ ±¸Á¶¸¦ ÅëÇØ °ªÀ» ÇÒ´çÇÒ ¼ö ÀÖ½À´Ï´Ù.

my_value = not_an_object.give_a_value rescue "Burn!"
puts my_value
¡æ Burn!

ÀÌ·± ½Î±¸·Áƽ(!)ÇÑ ¿¹Á¦·Î´Â ¹°·Ð ½ÇÁ¦ »óȲÀÇ °æ¿ì¸¦ º¸¿©ÁÖÁö ¸øÇÏÁö¸¸ ÀÌ·¯ÇÑ ±¸Á¶´Â ´ëºÎºÐÀÇ ¾î¶² ¿¹¿Ü»óȲ¿¡¼­µµ °ªÀ» ¿Ã¹Ù¸£°Ô ¼öÁ¤ÇÒ ¼ö ÀÖµµ·Ï ¾à°£¾¿ Á¶ÀýÇÒ °æ¿ì À¯¿ëÇÏ°Ô ¾²ÀÏ ¼ö ÀÖ½À´Ï´Ù(µå¹°Áö¸¸ °¡´ÉÇÑ °æ¿ì¿¡¿ä).

1.3.3. Retry

¶§·Ð do-over(Á» ½±°Ô Ç®ÀÌÇÏÀÚ¸é µ¿ÀÛÁß µ¿ÀÛ Áö½Ã¶ó ÇØ¾ß ÇÏ°ÚÁÒ? °¡·É... ÀÏÀÌ À߸øµÇ¾î °¡°í ÀÖ´Â °æ¿ì "µ¿À۱׸¸! ¿ªÇÒÀ» ÀçºÐ´ãÇϱâ·Î ÇÑ´Ù" ¶ó°í ¸í·É ³»¸®µíÀÌ...) °¡ ÇÊ¿äÇÒ ¶§°¡ ÀÖ½À´Ï´Ù. ÀÌ ¹Ýº¹ ÇüÅ´ ÀǵµÇϴ´ë·Î µ¿ÀÛÇÏÁö ¾Ê°Å³ª ÀÌ·¯ÇÑ »óȲ¿¡¼­ ¾î¶°ÇÑ ÀÚ±ØÀ» Á־ º¯¼ö°¡ ¿©ÀüÈ÷ ÇØ°áµÇÁö ¾ÊÀ» ¶§°¡ ÀÖ½À´Ï´Ù.(°¡·É ¸»µµ ¾ÈµÇ´Â °á°ú°¡ °è¼Ó Ãâ·ÂµÇ°í ÀÌ·± »óȲ¿¡¼­ Çì¾î³ªÁö ¸øÇÒ¶§Áö¿ä.) ¾î¶»µç°£¿¡ ÀÌ·² ¶§´Â ´õ ³ªÀº ´ÙÀ½ ´Ü°è·Î °£´ÜÇÏ°Ô ³Ñ¾î°¡´Â Äڵ带 ÅëÇÑ ¹Ýº¹µ¿ÀÛÀÌ °¡´ÉÇÑ ºÎºÐÀ» ÇÊ¿ä·Î ÇÕ´Ï´Ù. ÀÌ µ¿ÀÛÀÌ ¹Ù·Î retry Å°¿öµå°¡ À§Ä¡ÇÑ °÷ ¾È¿¡¼­ ÀÌ·ç¾îÁý´Ï´Ù. °¡»óÀÇ À¥ºê¶ó¿ìÁ®¸¦ ¸¸µé¾ú´Ù Ĩ½Ã´Ù.

def make_request
if (@http11)
   self.send('HTTP/1.1')
else
    self.send('HTTP/1.0')
end
rescue ProtocolError
    @http11 = false
    retry
end
HTTP 1.1Çì´õ¸¦ º¸³ÂÁö¸¸ ¼­¹ö¿¡¼­´Â À̸¦ ÀνÄÇÏÁö ¸øÇÏ°í ProtocolError¸¦ ÅäÇسÀ´Ï´Ù. »óȲÀ» Á¢°í ´Ü¼ø ¿¹¿Ü/¿¡·¯·Î ¸¶¹«¸®ÇÏ´Â ´ë½Å¿¡ HTTP 1.1¸¦ ¾²Áö ¸øÇÏ°Ô ÇÏ¿© ÀÌ »óȲÀ» Â÷´ÜÇÕ´Ï´Ù. Àç ½Ãµµ ¿äûÀ» ÇÏ¿© À¥ ºê¶ó¿ìÀú·Î ÇÏ¿©±Ý ´É·Â²¯ HTTP 1.0 À¸·Î ´ë½Å ÀüȯÇÏ°Ô ÇÕ´Ï´Ù. ÁÁÁÒ? rescue¿Í retry¸¦ »ç¿ëÇÔÀ¸·Î ÀÎÇØ, ¿¹¿Üµé°ú »óȲ Â÷´ÜÀÇ Àç½Ãµµ¸¦ ¾ß±âÇÏ´Â ¿¡·¯µéÀ» ¼öÁ¤ÇÏ°Ô²û ½ÃµµÇÏ°Ô ÇÒ ¼ö ÀÖ½À´Ï´Ù. ±Ùµ¥ ÀÌ°É Àß ÁöÄѺ¸¸é, ¸¸¾à ÀÌ ¹®Á¦°¡ ÇØ°áµÇÁö ¾Ê°í °è¼Ó ÇÁ·Î±×·¥¿¡ ÀÇÇØ ¹ð¹ð µµ´Â ¹«ÇÑ·çÇÁ »óÅ¿¡ ºüÁ®µé°Ô µÈ´Ù¸é ÀÌ°ÍÀÌ ´õ¿í ÀÌ»óÇÑ ¹®Á¦¸¦ ¾ß±âÇÒ ¼öµµ ÀÖ´Ù´Â°É ¾Æ½Ç ¼ö ÀÖ°Ô µÉ °ÍÀÔ´Ï´Ù.

1.3.4. ¿¹¿Ü ¹ß»ý

±×·¡¼­ ÀÌÁ¦ ¿¹¿Ü¸¦ ¾î¶»°Ô ´Ù·ç´ÂÁö ¹è¿î´Ù´Â°Ç, ÇÊÀÚ°¡ »ý°¢Çϱâ·Î´Â Áö±ÝÀÌ ¹Ù·Î ´ç½Å¸¸ÀÇ ¿¹¿Ü¸¦ È°¿ëÇϱâ À§ÇØ ÁÁÀº ¶§°¡ ¾Æ´Ñ°¡ ½Í½À´Ï´Ù. ¿¹¿Ü¸¦ ¹ß»ýÇÏ´Â °ÍÀº ·çºñ ÀÚüÀûÀ¸·Î ¹®Á¦°¡ ¹ß»ýÇÏÁö ¾ÊÀ»Áöµµ ¸ð¸£´Â ÀÌ·± ÀÌ»óÇÑ "Áß¿äÇÑ" ¹®Á¦°¡ ¹ß»ýÇÏ¿´À»¶§¿¡ ´ëºñÇÏ¿© ÇÊ¿äÇÕ´Ï´Ù. (¸»ÀÌ Á» ¾î·Á¿îµ¥... °á±¹ ¸»Àå³­ ÀÔ´Ï´Ù. -_-;) ¿¹¸¦ µéÀÚ¸é, À½.. ´ÙÀ½ Person Ŭ·¡½º¿¡ Æ÷ÇÔµÈ ¸Þ¼Òµå¸¦ º¸µµ·Ï ÇÏÁö¿ä.

def define_gender(gender)
   if (gender.upcase != 'FEMALE') && (gender.upcase != 'MALE')
      raise "You specified something wonky for gender!"
   end
end

my_guy = Person.new
my_guy.define_gender("nobody knows")
¡æ ! RuntimeError ("You specified something wonky for gender!")

ºñ·Ï ÀÌ°Ô Ruby ÀÚüÀûÀ¸·Î ¹®Á¦°¡ µÇÁø ¾Ê°ÚÁö¸¸, ¸í¹éÇÏ°Ô µûÁø´Ù¸é ¼ºº°À» À§ÇØ ºñÁ¤»óÀûÀ¸·Î Á¤ÀÇÇÏ´Â °ÍÀ» ¿øÄ¡ ¾ÊÀ» °ÍÀÔ´Ï´Ù. (°ÔÀ̳ª ·¹Áîºñ¾ðÀ̳ª -_-; ¹°·Ð ±×·¸°Ô ÇÑ´Ù¸é Âü ¿ô±ä ÀÏÀÌ µÇ¾î ¹ö¸®°ÚÁÒ). ¸¸¾à ¾î¶² »óÅ°¡ ¹ß»ýÇÏ¿´À»¶§, ¸Þ¼¼Áö¸¦ ÅëÇØ µþ·Á³ª¿À´Â ÁÙ »ó¿¡ Å°¿öµå¸¦ ¶³±Å³õÀ» ¼ö ÀÖ°í, ÀÌ·¸°Ô ÇÔÀ¸·Î Çؼ­ ·çºñ°¡ »õ·Î¿î RuntimeError¸¦ ¹ß»ý½ÃÄѼ­ ·±Å¸ÀÓ ¿¡·¯¿¡ °üÇÑ ¸Þ¼¼Áö¸¦ ¼Ò½º»ó¿¡ Á¦°øÇÑ ´ë·Î ¼³Á¤ÇÕ´Ï´Ù. ±×°Ô ¹Ù·Î raise Å°¿öµå°í (ÀÌ°Ô ¾Æ´Ï¶ó °í»óÇÏÁö ¸øÇÑ ¿þÀÏÁî Áö¹æ »çÃÌ ¸»´ë·Î¶ó¸é, fail Å°¿öµå Âë µÇ°ÚÁÒ) À̳ðÀº ´Ù¾çÇÑ ¹æ¹ýÀ¸·Î È£ÃâµË´Ï´Ù:

raise "I crashed! This message should be more informative!"
raise
raise NoMethodError, "That method ain\'t here!", caller

óÀ½ ¹æ½ÄÀº ¾Æ¸¶ ²Ï Ä£¼÷ÇÑ ¹æ¹ýÀÏ °ÍÀÔ´Ï´Ù. (½ÅÁ¾ RuntimeError¿¡ ´ëÇÑ ¸Þ¼¼Áö¸¦ Á¦°øÇÏÁÒ); µÎ¹ø° ¹æ½ÄÀº Á¤ÇØÁø ¿¹¿Ü°¡ ¾ø´Ù¸é ÇöÀç ¿¹¿Ü¸¦ ¶Ç ¹ß»ý½ÃÄѼ­ ÄÝ ½ºÅÿ¡ ½×¾ÆµÎ°í ³Ñ¾î°¡µçÁö ¾î¶² ¸Þ¼¼Áöµµ ³²±âÁö ¾Ê°í ¿¹¿Ü¸¦ ¹ß»ý½ÃÄÑ Á¾·áÇعö¸³´Ï´Ù. ¸¶Áö¸· ¹æ½ÄÀº °ÅÀÇ º¯ÇÔ¾øÀÌ ´ëºÎºÐ ¾²ÀÏÅÙµ¥ ¿Ö³Ä¸é ÀÌ Çü½ÄÀ¸·Î ÇÏ¿©±Ý ¿¹¿Ü À¯Çü°ú ¸Þ¼¼Áö ±×¸®°í ½ºÅà ÃßÀû °´Ã¼¸¦ Á¤ÀÇÇÒ ¼ö Àֱ⠶§¹®ÀÔ´Ï´Ù. (Á¾Á¾ ÀÌ°É ´ÜÁö È£ÃâÀÚ, Ä¿³ÎÂüÁ¶ÀÚ, È£ÃâÀÚ ¸Þ¼Òµå¶ó ÇÏÁÒ). ÁÁÀº ÇÁ·Î±×·¥ÀÇ ½À°üÀº °¡´ÉÇÑÇÑ ¸ðµç Á¤ÀÇµÈ ¿¹¿Ü°¡ Æ¢¾î³ª¿É´Ï´Ù. Ç×»ó RuntimeErrors ¿¹¿Ü°¡ ´øÁ®Áö´Â°Ô ¾Æ´Ï¶ó ÇÊ¿ä·Î ÇÏ´Â °Í¿¡ ¸ÅÄ¡½ÃÅ°±â À§ÇÑ ¿¹¿Ü À¯ÇüÀ̳ª ¿Ã¹Ù¸¥ À¯ÇüÀÌ ¾Æ´Ñ °´Ã¼°¡ Á¦°øµÇ¾úÀ»¶§ TypeError ¿¹¿Ü¸¦ ´øÁ®º¼ ¼öµµ ÀÖÁÒ. (¾ê±â°¡ Á» ¾î·ÆÁÒ? ÀÌ ¼Ò½ºÄÚµåÀÇ °æ¿ì GenderError¸¦ ¹ß»ý½ÃÅ°´Â °ÍÀÌ ´õ ÁÁ°Å³ª ¿¹½Ã¿¡¼­¿Í °°ÀÌ RuntimeErrorº¸´Ù ´õ ÁÁÀº ±× ¾î¶² ¿¹¿Ü¸¦ ´øÁ®º¼ ¼ö ÀÖ´Ù´Â °ÍÀÔ´Ï´Ù. ¿¹¿ÜÀÇ ¸íȮȭ...).

1.3.5. ³ª¸¸ÀÇ ¿¹¿Ü ¸¸µé±â

±×·¡¼­ ¾î¶»°Ô »ç¿ëÀÚ Á¤ÀÇ ¿¹¿Ü Çü½ÄÀ» ¸¸µé ¼ö ÀÖÀ»±î¿ä? ·çºñ¿¡¼­´Â ÁøÂ¥·Î ÂÉ²Þ °£´ÜÇÕ´Ï´Ù. ÀÏ´Ü ¼³¸í º¸±â Àü¿¡ ¿¹Á¦¸ÕÀú ½ÇÇàÇغ¸µµ·Ï ÇÏ°Ú½À´Ï´Ù. ¾ó··!!:

class GenderError < RuntimeError
    attr :what_they_put

    def initialize(their_input)
        @what_they_put = their_input
    end
end

»õ·Î¿î ¿¹Á¦¸¦ ¸¸µé±â À§ÇØ, ¿¹¿Ü Ŭ·¡½ºÀÇ ÀϺθ¦ »ó¼Ó¹Þ½À´Ï´Ù. (e.g. ÇÊÀÚÀÇ °æ¿ì´Â RuntimeError·ÎºÎÅÍ »ó¼Ó ¹Þ¾ÒÁö¸¸, µ¶ÀÚÀÇ °æ¿ì¿¡´Â TypeError, NoMethodError, ¶Ç´Â ´Ù¸¥ ¿¹¿Ü Ŭ·¡½º·ÎºÎÅÍ »ó¼Ó¹ÞÀ» ¼ö ÀÖ½À´Ï´Ù). Çؼ­ Äڵ带 »õ·Î °íÃļ­ ¿¬½À»ï¾Æ ¶Ç ´Ù¸¥ »õ·Î¿î ¿¹¿Ü¸¦ ¸¸µé¾î º¸µµ·Ï ÇÏ°Ú½À´Ï´Ù:

class Person
   def define_gender(gender)
      if (gender.upcase != 'FEMALE') && (gender.upcase != 'MALE')
         raise GenderError.new(gender), "Invalid input!"
      end
   end

   def initialize(gender)
      self.define_gender(gender)
   rescue GenderError => bad
      puts "You gave me some bad input: " + bad.what_they_put
      raise
   end
end

my_guy = Person.new("Who knows?")
¡æ You gave me some bad input: Who knows? ¡æ ! GenderError ("Invalid input!")

define_gender¿¡¼­ ¿¹¿Ü¸¦ ÀÏÀ¸ÄѼ­ initialize¿¡¼­ ó¸®ÇÑ ´ÙÀ½ ½ºÅÃÀÇ À§ÂÊÀ¸·Î ³Ñ±ä °Í¿¡ ÁÖ¸ñÇϼ¼¿ä. initialize¾È¿¡¼­ ¿ì¸®°¡ »õ·Î ¸¸µç ¿¹¿Ü Ŭ·¡½º ³»ÀÇ ¼Ó¼º(attribute)¸¦ Ãâ·ÂÇß½À´Ï´Ù. Áï, ¿¹¿Üµéµµ ¸ðµÎ ÀüºÎ °´Ã¼À̸ç Ŭ·¡½º¿¡¼­ »ý¼ºµÇ±â ¶§¹®¿¡ ¿©·¯ºÐÀº ÀڽŸ¸ÀÇ ¿¹¿Ü¸¦ ¸¸µé¶§ ¸Þ½îµå¿Í ¼Ó¼ºÀ» Áý¾î³ÖÀ» ¼ö ÀÖ½À´Ï´Ù. ³»°¡ »ç¿ëÀÚ ÀԷ°ªÀ» ÀúÀåÇß´ø °Íó·³ ¸»ÀÔ´Ï´Ù. ÀÌ´Â ¿¹¿Ü Çڵ鷯·Î ´õ ¸¹Àº µ¥ÀÌÅ͸¦ Á¦°øÇÏ°íÀÚ Çϰųª ¿¹¿Ü¿¡¼­ º¹±¸ÇÏ°íÀÚ ÇÏ´Â ¸Þ½îµå¸¦ ¿¹¿Ü Ŭ·¡½º¿¡¼­ Á¦°øÇÏ°í ½ÍÀ» ¶§ À¯¿ëÇÕ´Ï´Ù.

1.3.6. Throw ¿Í Catch

C# À̳ª Java ¸¦ ´Ù·ç´Â ÇÁ·Î±×·¡¸Ó¶ó¸é, ÀÌ ºÐ¾ß¿¡ ÀÖ¾î ¾à°£ Ä£¼÷Çϸ®¶ó ¿¹»óµÇ´Â ¹Ù·Î ÀÌ°Í¿¡ ´ëÇØ ´ÜÁö ÈïºÐÇϱ⿡ ±×Ä¡°ÚÁö¸¸, µÕÁöÀÇ À̸§³­ ¾ËÀÇ °¹¼ö¸¦ ¼¼Áø ¸øÇÒ °ÍÀÔ´Ï´Ù.(°©ÀÚ±â ÀÌ°Ô ¹º ¼Ò¸®¿© -_-?) ·çºñ¿¡¼­ catch ºí·ÏÀº ÀÎÀÚ¿Í °°Àº ½Äº°ÀÚ¸¦ ¹Þ°í throw¸¦ ÅëÇØ ÀÌ ½Äº°ÀÚ¸¦ °á°ú·Î¼­ °è¼ÓµÉ Äڵ忡 ´øÁ®¹ö¸± ¼ö ÀÖ½À´Ï´Ù. ·çºñ´Â ½ºÅÿ¡¼­ ¸Â´Â catch ±¸¹®ÀÌ ¾î¶²°ÍÀÎÁö¸¦ ã°í, ÀÌ°Ô ¹ß°ßµÇ¾ú´Ù¸é, ·çºñ´Â ¸ØÃß°í ÀÏ¹Ý Ã³¸® °úÁ¤¿¡ ÁøÀÔÇÏ°í catchºí·Ï¿¡¼­ ºüÁ®³ª°©´Ï´Ù. ¾ê±â·Î ÇÏÀÚ¸é ´õ ¾î·Æ±â ¶§¹®¿¡ ´ÙÀ½ ¿¹Á¦¸¦ Âü°íÇϵµ·Ï ÇÏ°Ú½À´Ï´Ù:

princess = DamselInDistress.new
catch :hes_a_failure do
    # YAY! Someone's here to save her...
    print "My prince is here! "

    # OH NO! The villain has eaten his liver! He dies!
    princess.is_saved = false

    if (princess.is_saved == true)
        puts "Hooray!"
    else
        puts "Poo! Not again!"
throw :hes_a_failure

end

puts "I'm going to sleep until the next guy..."
# Nap...
end
¡æ My prince is here! Poo! Not again!

catch ºí·ÏÀº catch Å°¿öµå°¡ À§Ä¡ÇÑ °÷¿¡¼­ºÎÅÍ ½ÃÀ۵˴ϴÙ. ½Äº°ÀÚ°¡ À§Ä¡ÇÏ°í ³­ÈÄ do Å°¿öµå°¡ ÇÑÁÙ¿¡ ÀÖ½À´Ï´Ù. ½Äº°ÀÚ´Â throw Å°¿öµå¿Í °°ÀÌ »ç¿ëµÇ¸ç ½Éº¼ ȤÀº ¹®ÀÚ¿­ µÑ´Ù°¡ µÉ ¼ö ÀÖ½À´Ï´Ù. ÀÌ ÄÚµå´Â throw ±¸¹®¿¡ ºÙ¾îÀÖ´Â ½Äº°ÀÚÀÇ °ªÀÌ ÀÏÄ¡ÇÒ¶§±îÁö ½ÇÇàµÇ¸ç (i.e. throw½Äº°ÀÚ´Â catchÀÇ ½Äº°ÀÚ¿Í µ¿ÀÏÇÕ´Ï´Ù) ÀÌ¿¡ ¼ÀÇÑ °á°ú°¡ µé¾î°¡°Ô µË´Ï´Ù. (¸¸¾à 1ÀÌ ¼¼¾îÁ³À»¶§) ÀÌÀÇ °æ¿ì¿¡´Â throw ±¸¹®À» ¸¸³­ ÀÌÈÄ·Î ±× ¾î¶² ±¸¹®µµ ½ÇÇàÇÏÁö ¾ÊÀ¸¸ç catch ºí·ÏÀ¸·ÎºÎÅÍ ºüÁ®³ª°¡°Ô µË´Ï´Ù. ¿¹¸¦ µé¾î, ÀÌ °°Àº ÀÏÀÇ °æ¿ì¿¡´Â "My prince is here!" ¿Í "Poo! Not again!" °¡ Ãâ·ÂµÇ°ÚÁö¸¸ "I'm going to sleep until the next guy..." ´Â ÀÌ¿Í °°Àº °æ¿ì°¡ ¾Æ´Ò¶§ Ãâ·ÂµÉ °ÍÀÔ´Ï´Ù. ºí·ÏÀ¸·ÎºÎÅÍ ºüÁ®³ª°£ ÈÄ¿¡µµ ¸ÅĪµÇ´Â throw ±¸¹®À» (i.e. throw :hes_a_failure) ¸¸³ª°Ô µË´Ï´Ù. ÀÌ·¯ÇÑ ±¸Á¶´Â ÁßøµÈ ·çÇÁ·Î ÇÏ¿©±Ý ±íÀÌ ¹¯Çô Àְųª ¿¡·¯¸¦ ¹ß»ýÇÏ´Â ÄÚµåÀÇ º¹ÀâÇÑ ºí·ÏÀ¸·Î ºÎÅÍ ºüÁ®³ª°¥¶§ ¸Å¿ì À¯¿ëÇÏ¸ç ½ÉÁö¾î´Â ÀÌ·¯ÇÑ ÄÚµåÀÇ Áß´ÜÀ» ºü¸£°í ½±°Ô Çϱâ À§ÇØ ÇÊ¿äÇÕ´Ï´Ù.

1.4. This Chapter

¹» ¹è¿ü³ª ¾Ë¾Æº¼±î¿ë?...

  • if/unless ¹®°ú »óÅ ¿¬»êÀÚ¿¡ ´ëÇØ...
  • ·çÇÁ ±¸ºÐ, µÎ »óÅÂ¿Í ¹Ýº¹, ´õ¿í È¿°úÀûÀ¸·Î Á¦¾îÇÏ´Â ¹æ¹ý.
  • ¿¹¿Ü¿Í »ç¿ë¹æ¹ý.

ID
Password
Join
"It seems strange to meet computer geeks who're still primarily running Windows... as if they were still cooking on a wood stove or something." - mbp


sponsored by andamiro
sponsored by cdnetworks
sponsored by HP

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2011-04-20 16:09:00
Processing time 0.0143 sec