본문 바로가기
Programming

Ruby - 변수명 표기법 및 $와 관련된 기본 전역변수

by leanu 2009. 4. 22.

루비 인터프리터는 변수병의 첫글자를 보고 (클래스변수는 두글자)를 보고 변수의 종류를 판단한다.

변수명 표기법


First Charactor Variable type Defalut Value Example
[a-z] or _ Local variable 참조 전에 대입해야 함 localVal = 3
[A-Z] Constant 참조 전에 대입해야 함 Const = "TEST_CONST"
@ Instance variable nil @Instance = "instance"
$ Global variable nil $global += 1
@@ Class Variable 참조 전에 대입해야 함 @@classVal



$ 관련 기본 전역 변수


$! 최근 에러 메세지
$@ 에러 위치
$_ local scope, gets 를 이용해 최근에 읽은 문자열
$. interpreter로 읽은 최근 줄 번호
$& regexp (regular expresstion) 으로 매치된 최근 문자열
$~ local scope, MatchData 객체 (최근 매치에 대한 모든 정보를 담고 있음)
$n n 은 숫자, 최근 match 된 스트링중 패턴의 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
$? 마지막에 종료한 child process 의 종료 status


댓글