[완료] ruby의 -n 옵션에 대한 질문..

leonid의 이미지

ruby -n 옵션은 다음과 같이 정의된다고 합니다..

-n
' Assumes ``while gets; ...; end'' loop around your program. For example, a simple grep command might be implemented as:
 
% ruby -n -e "print if /wombat/"  *.txt

그래서

while gets; ...; end

에서 ... 부분에
end; while gets

를 넣기로 하고

#!ruby -n
end; while gets

하고 돌렸는데 unexpected kEND라면서 안되네요.

그런데

while gets; end; while gets; end

를 통째로 써놓고 옵션 없이 실행시키면 정상적으로 잘 됩니다.

왜그럴까요..?

jg의 이미지

추측컨대 ruby는 단순히 while gets(); ... end 를
덧붙여 실행하는 것이 아닌 것 같습니다.
다른 방법을 강구하는 것이 좋을 것 같습니다.

$Myoungjin_JEON=@@=qw^rekcaH lreP rehtonA tsuJ^;$|++;{$i=$like=pop@@;unshift@@,$i;$~=18-length$i;print"\r[","~"x abs,(scalar reverse$i),"~"x($~-abs),"]"and select$good,$day,$mate,1/$~for 0..$~,-$~+1..-1;redo}

leonid의 이미지


한시간동안 루비 소스를 뒤진 끝에 알아냈습니다. --;

루비에서 -n이나 -p 옵션을 사용했을 때

앞뒤에 정말로 while gets; ...; end 를 붙이는 게 아니고

인터프리터 자체적으로(C언어로 만들어졌죠..) while루프를 돌리는 거였습니다. 속도를 높이기 위한거라네요..

그러나 -a 옵션은 코드 앞부분에 $F=$_.split을 정말로 넣고,

-p 옵션도 코드 뒷부분에 print $_을 넣고,

-l 옵션도 코드 앞부분에 $_.chop!을 넣습니다.

aero의 이미지

Perl에서 -n 옵션은 설명을 보면
-n assume "while (<>) { ... }" loop around program
라고 되어있습니다. Ruby가 어느정도 Perl의 특징을 계승한 언어라서 command line option도
비슷하게 따라간것 같군요.

Perl에서는 진짜 -n 옵션이 코드에 while (<>) { ... } 를 싸는 형식으로 작동하는지
테스트 해봤습니다.

다음 코드는

perl -e 'while (<>) { print $_."\n"} BEGIN{print "1\n"}'

이것과 같아야 합니다.
perl -ne 'print $_."\n"} BEGIN{print "1\n"'

BEGIN블록은 while (<>) 루프 외부의 코드가 실행되는지 확인하기 위해 넣었습니다.
제대로 실행된다면 실행시 1이 먼저 찍혀야겠죠.

실행해보니 정말 그렇게 작동하는군요...

>perl -ne 'print $_."\n"} BEGIN{print "1\n"'
1
test input
test input

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.