首页 > 代码库 > ruby 中的变量

ruby 中的变量

1、变量和常量:

全局变量用$;   实例变量用@;   类变量用@@;

Local Variable:  name fish_and_chips x_axis thx1138 _x _26
Instance Variable:   @name @point_1 @X @_ @plan9
Class Variable:  @@total @@symtab @@N @@x_pos @@SINGLE
Global Variable:   $debug $CUSTOMER $_ $plan9 $Global
Class Name: String ActiveRecord MyClass
Constant Name:   FEET_PER_MILE DEBUG

2、数组

a = [ ‘ant‘, ‘bee‘, ‘cat‘, ‘dog‘, ‘elk‘ ]
a[0]  # => "ant"
a[3]  # => "dog"
# this is the same:
a = %w{ ant bee cat dog elk }
a[0]  # => "ant"
a[3]  # => "dog"