ruby中的全局域变量

Ruby全局域变量是在整个应用中都可以访问到的,包括:$/ 、$\ 、$, 、$; 、$* 、$$ 、$:等等。

 

一、$/

 

输入记录分隔符。默认值为"\n"。

 

irb(main):076:0> $/ #初始的输入分割符   
=> "\n"   
irb(main):077:0> gets   
=> "\n"   
irb(main):078:0> "test" 
#输入回车之后,默认插入"\n",输入结束   
=> "test"   
irb(main):079:0> $/="@" 
#修改输入符为"@"   
=> "@"   
irb(main):080:0> gets 
#输入回车之后没有结束读取进程,直到输入"@"之后结束   
test   
@   
=> "test\n@" 
 

二、$\

 

输出记录分隔符。print会在最后输出该字符串。 默认值为nil,此时不会输出任何字符。

 

irb(main):082:0> print "abc"   
abc=> nil   
irb(main):083:0> $\="@"   
=> "@"   
irb(main):084:0> print "abc"   
abc@=> nil

 

三、$,

 

默认的切分字符。若Array.join中省略了参数时或在print的各个参数间将会输出它。 默认值为 nil ,等同于空字符串。

 

irb(main):087:0> ["a","b","c"].join   
=> "abc"   
irb(main):088:0> $,="," 
#修改切分字符为","   
=> ","   
irb(main):089:0> ["a","b","c"].join #输出结果改变   
=> "a,b,c"  
 

四、$; 

 

当String#split中省略参数时的切分字符。默认值为nil,此时将进行特殊的分割。

 

irb(main):090:0> $;   
=> nil   
irb(main):091:0> "abc".split 
#默认的切分字符情况下,把整个字符当成一个元素   
=> ["abc"]   
irb(main):092:0> $;="b"   
=> "b"   
irb(main):093:0> "abc".split 
#以"b"为切分字符情况下,把整个字符当成两个元素   
=> ["a", "c"] 
 

五、$*

 

传递给Ruby脚本的参数,ARGV的别名。

 

六、$$

 

当前运行中的Ruby进程的pid。

 

irb(main):094:0> $$   
=> 5167 
 

七、$:

 

包含一个数组,其内容是load或require加载文件时用的搜索目录列表。

 

irb(main):095:0> $:   
=> ["/Users/gaohf/NetBeansProjects/RailsApplication1/app/controllers", "/Users/gaohf/NetBeansProjects/RailsApplication1/app/helpers", "/Users/gaohf/NetBeansProjects/RailsApplication1/app/models", "/Users/gaohf/NetBeansProjects/RailsApplication1/app/views", "/Users/gaohf/NetBeansProjects/RailsApplication1/config", "/Users/gaohf/NetBeansProjects/RailsApplication1/db", "/Users/gaohf/NetBeansProjects/RailsApplication1/lib", "/Users/gaohf/NetBeansProjects/RailsApplication1/log", "/Users/gaohf/NetBeansProjects/RailsApplication1/public", "/Users/gaohf/NetBeansProjects/RailsApplication1/test", "/Users/gaohf/NetBeansProjects/RailsApplication1/script", "/Users/gaohf/NetBeansProjects/RailsApplication1/doc", "/Users/gaohf/NetBeansProjects/RailsApplication1/vendor", "/Users/gaohf/NetBeansProjects/RailsApplication1/test", "/Library/Ruby/Site/1.8", "/Library/Ruby/Site/1.8/powerpc-darwin10.0", "/Library/Ruby/Site/1.8/universal-darwin10.0", "/Library/Ruby/Site", "/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8", "/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8/universal-darwin10.0", "/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby", "/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8", "/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/powerpc-darwin10.0", "/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/universal-darwin10.0", "."]
 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(C++,c,脚本,C#,Ruby)