Top > Ruby

文字列処理 Edit

正規表現とパターンマッチング Edit

gsubにブロックを与える Edit

・<%= ~ %> の部分を評価して置き換える。

x = 123
a = "This is <%= 5+6 %> and <%= 'The' + 're'%>, <%=Time.now%>. x=<%=x%>"
a.gsub!(/<%=(.*?)%>/) { eval($1).to_s }
puts a

パイプ Edit

IO.popen("pkg_info -asc", "r") do |fp|
  while fp.gets
    line = $_
    if line =~ /^Information for/
      name        = line.split[2].chop
      size        = "?"
      comment = "?"
    elsif line =~ /^Comment:/
       fp.gets
       comment = $_
    elsif line =~ /^Package Size:/
       fp.gets
       size = $_.split[0]
       printf "%-30s %8s %s", name, size, comment
    end
  end
end

配列のシャッフル Edit

arr = ["A","Q","K","J"]
p arr.sort_by{rand}

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/40894

URLをGET Edit

require 'net/http'
Net::HTTP.version_1_2
Net::HTTP.get_print 'www.example.com', '/index.html'


URL B I U SIZE Black Maroon Green Olive Navy Purple Teal Gray Silver Red Lime Yellow Blue Fuchsia Aqua White

Reload   New Lower page making Edit Freeze Diff Upload Copy Rename   Front page List of pages Search Recent changes Backup Referer   Help   RSS of recent changes