I hate Ruby's special global variables

Posted 2006. 12. 12. 01:12
I think the reason why Ruby is not so readable is because Ruby has special variables like $$.

$! latest error message
$@ location of error
$_ string last read by gets
$. line number last read by interpreter
$& string last matched by regexp
$~ the last regexp match, as an array of subexpressions
$n the nth subexpression in the last match (same as $~[n])
$= case-insensitivity flag
$/ input record separator
$\ output record separator
$0 the name of the ruby script file
$* the command line arguments
$$ interpreter's process ID
$? exit status of last executed child process

Ruby programmers who often use these awkward looking special variables may feel comfortable, but I hate that I have to memory the difference between $! and $@. What is the rule to memorize the meanings of these variables? Even worse, some variables names are confusing. For example, Ruby naming convention states that variables starting with $ are global variables, but $_ and $~ are closer to local variables.

Ruby emphasizes the principles of least surprise, but special variables really surprised me. [For those of you who are not familar with the principles of least surprise: Read"Applying the Rule of Least Surprise"  in The Art of Unix Programming written by Eric  Raymond.]

Ruby is a pure objected-oriented language and special variables do not fit well in OO concepts. Why don't we make a Process clsss and define pid and args methods to retrieve process ID and command line arguments instead of ugly $$ and $* variables?
  1. iloveC

    | 2009.09.15 14:41 | PERMALINK | EDIT | REPLY |

    저도 루비를 배워볼까 했었는데..
    "배워볼까 했었는데"로 끝난 저의 이유와 같군요..
    요거 스크랩좀 해갈게요 ㅎㅎㅎ

Write your message and submit
« PREV : 1 : ··· : 173 : 174 : 175 : 176 : 177 : 178 : 179 : 180 : 181 : ··· : 244 : NEXT »