Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the magic $-prefixed variables in Ruby? [closed]

Tags:

variables

ruby

I've seen magic variables like this used in Ruby. $_ $' $0

Is there a complete reference for what all of them mean and how they are set?

like image 632
maček Avatar asked Sep 19 '10 17:09

maček


People also ask

Do you have to declare variables in Ruby?

Variables must be declared in most languages in order to specify their type, modifiability (i.e., whether they are constants), and scope; since type is not an issue, and the rest is evident from the variable name as you are about to see, we do not need variable declarations in ruby.

How are variables declared and used in Ruby?

Ruby Local Variables When an uninitialized local variable is referenced, it is interpreted as a call to a method that has no arguments. Assignment to uninitialized local variables also serves as variable declaration. The variables start to exist until the end of the current scope is reached.


2 Answers

Their name is global variables. There are several different references.

You can get a full list by calling the method Kernel#global_variables

puts global_variables 

Ruby also includes a file called "English.rb" in the standard library which provides an in-depth explanation of several global variables.

Also, there's (an archived version of) "Cryptic Ruby Global Variables and Their Meanings".

Finally, the Ruby Programming wikibook has a "Predefined Variables" reference.

like image 51
Simone Carletti Avatar answered Oct 21 '22 07:10

Simone Carletti


They are called "global variables" (complete list at the bottom of the page): http://www.rubyist.net/~slagell/ruby/globalvars.html

like image 21
remi Avatar answered Oct 21 '22 08:10

remi