Ruby 1.9.2 リファレンスマニュアル > ライブラリ一覧 > library English

library English

要約

特殊変数 $! などに英語名の別名 ($ERROR_INFO など)をつけます。

例:

p $/  #=> "\n"
p $RS #=> nil

require 'English'
p $RS #=> "\n"

追加・再定義されるメソッド

Kernel$$ARGV [added by English]

$* の別名

require "English"
p $ARGV
# end of sample.rb

ruby sample.rb 31 /home/hoge/fuga.txt
#=> ["31", "/home/hoge/fuga.txt"]
Kernel$$CHILD_STATUS [added by English]

$? の別名

require "English"

out = `wget http://www2.ruby-lang.org/ja/LICENSE.txt -O - 2>/dev/null`

if $CHILD_STATUS.to_i == 0
  print "wget success\n"
  out.split(/\n/).each { |line|
    printf "%s\n", line
  }
else
  print "wget failed\n"
end
Kernel$$DEFAULT_INPUT [added by English]

$< の別名

require "English"
while line = $DEFAULT_INPUT.gets
  p line
end
# end of sample.rb

ruby sample.rb < /etc/passwd
# => "hoge:x:500:501::/home/hoge:/bin/bash\n"
     ...
Kernel$$DEFAULT_OUTPUT [added by English]

$> の別名

require "English"

dout = $DEFAULT_OUTPUT.dup
$DEFAULT_OUTPUT.reopen("out.txt", "w")
print "foo"
$DEFAULT_OUTPUT.close
$DEFAULT_OUTPUT = dout
p "bar" # => bar
p File.read("out.txt") #=> foo
Kernel$$ERROR_INFO [added by English]

$! の別名

require "English"
class SomethingError < StandardError; end

begin
  raise SomethingError
rescue
  p $ERROR_INFO.backtrace #=> ["sample.rb:5"]
  p $ERROR_INFO.to_s #=> "SomethingError"
end
Kernel$$ERROR_POSITION [added by English]

$@ の別名

require "English"
class SomethingError < StandardError; end

begin
  raise SomethingError
rescue
  p $ERROR_POSITION #=> ["sample.rb:5"]
end
Kernel$$FS [added by English]
Kernel$$FIELD_SEPARATOR [added by English]

$; の別名

require "English"

str = "hoge,fuga,ugo,bar,foo"
p str.split #=> ["hoge,fuga,ugo,bar,foo"]
$FIELD_SEPARATOR = ","
p str.split #=> ["hoge", "fuga", "ugo", "bar", "foo"]
Kernel$$IGNORECASE [added by English]

$= の別名

require "English"

$IGNORECASE=true

str_l = "FOOBAR"
str_s = "foobar"

if str_l == str_s
  p "#{str_l} equal to #{str_s}" #=> "FOOBAR equal to foobar"
end
Kernel$$INPUT_LINE_NUMBER [added by English]
Kernel$$NR [added by English]

$. の別名

1 e
2 f
3 g
4 h
5 i
# end of a.txt

require "English"

File.foreach(ARGV.at(0)){|line|
  # read line
}
p $INPUT_LINE_NUMBER
# end of sample.rb

ruby sample.rb a.txt
#=> 5
Kernel$$RS [added by English]
Kernel$$INPUT_RECORD_SEPARATOR [added by English]

$/ の別名

require "English"

$INPUT_RECORD_SEPARATOR = '|'
array = []
while line = DATA.gets
  array << line
end
p array #=> ["ugo|", "ego|", "fogo\n"]

__END__
ugo|ego|fogo
Kernel$$LAST_MATCH_INFO [added by English]

$~ の別名

require "English"

str = "<a href=http://www2.ruby-lang.org/ja/LICENSE.txt>license</a>"

if /<a href=(.+?)>/ =~ str
  p $LAST_MATCH_INFO[0] #=> "<a href=http://www2.ruby-lang.org/ja/LICENSE.txt>"
  p $LAST_MATCH_INFO[1] #=> "http://www2.ruby-lang.org/ja/LICENSE.txt"
  p $LAST_MATCH_INFO[2] #=> nil
end
Kernel$$LAST_PAREN_MATCH [added by English]

$+ の別名

require "English"

r1 = Regexp.compile("<img src=(http:.+?)>")
r2 = Regexp.compile("<a href=(http|ftp).+?>(.+?)</a>")

while line = DATA.gets
  [ r1, r2 ].each {|rep|
    rep =~ line
    p $+
  }
end
__END__
<tr> <td><img src=http://localhost/a.jpg></td> <td>ikkou</td> <td><a href=http://localhost/link.html>link</a></td> </tr>
#enf of sample.rb

$ ruby sample.rb
"http://localhost/a.jpg"
"link"
Kernel$$LAST_READ_LINE [added by English]

$_ の別名

1 e
2 f
3 g
4 h
5 i
# end of a.txt

ruby -rEnglish -ne'p $LAST_READ_LINE' a.txt
#=>
"1 e\n"
"2 f\n"
"3 g\n"
"4 h\n"
"5 i\n"
Kernel$$LOADED_FEATURES [added by English]

$" の別名

require "English"
require "find"

p $LOADED_FEATURES #=> ["English.rb", "find.rb"]
Kernel$$MATCH [added by English]

$& の別名

require "English"

str = 'hoge,foo,bar,hee,hoo'

/(foo|bar)/ =~ str
p $MATCH     #=> "foo"
Kernel$$OFS [added by English]
Kernel$$OUTPUT_FIELD_SEPARATOR [added by English]

$, の別名

require "English"

array = %w|hoge fuga ugo bar foo|
p array.join #=> "hogefugaugobarfoo"
$OUTPUT_FIELD_SEPARATOR = ","
p array.join #=> "hoge,fuga,ugo,bar,foo"
Kernel$$ORS [added by English]
Kernel$$OUTPUT_RECORD_SEPARATOR [added by English]

$\ の別名

require "English"

print "hoge\nhuga\n"
$OUTPUT_RECORD_SEPARATOR = "\n"
print "fuge"
print "ugo"
# end of sample.rb

ruby sample.rb
hoge
huga
fuge
ugo
Kernel$$PID [added by English]
Kernel$$PROCESS_ID [added by English]

$$ の別名

require "English"

      p sprintf("something%s", $PID) #=> "something5543" など
Kernel$$POSTMATCH [added by English]

$' の別名

require "English"

str = 'hoge,foo,bar,hee,hoo'

/foo/ =~ str
p $POSTMATCH #=> ",bar,hee,hoo"
Kernel$$PREMATCH [added by English]

$` の別名

require "English"

str = 'hoge,foo,bar,hee,hoo'

/foo/ =~ str
p $PREMATCH  #=> "hoge,"
Kernel$$PROGRAM_NAME [added by English]

$0 の別名

require "English"

p $PROGRAM_NAME
#end of sample.rb

ruby sample.rb  #=> "sample.rb"
ruby ./sample.rb #=> "./sample.rb"
ruby /home/hoge/bin/sample.rb #=> "/home/hoge/bin/sample.rb"

Classes

Sublibraries