Ruby勉強

インターネットの企画職な人がプログラミングを勉強するブログ

Ruby DATA __END__ - Silver/Gold試験対策

DATAとEND。全て大文字

while DATA.gets
  puts $_ if $_ =~ /Ruby/
end #=> Ruby the prgoramming language
__END__
Java the prgoramming language
Ruby the prgoramming language
Python the prgoramming language

下記とほぼ同じ

DATA.each do |str|
  puts str if str =~ /Ruby/
end
__END__
Java the prgoramming language
Ruby the prgoramming language
Python the prgoramming language

String#=~とRegexp#=~があるので、 selfが文字列、otherが正規表現の場合だけでなくその逆でもマッチが検査できる

self =~ other

instance method String#=~ (Ruby 2.1.0)