2006-02-27から1日間の記事一覧

String#squeeze

Rubyでtrをするようなものですね。 p "Google".squeeze #=> "Gogle" p "Yahoo!".squeeze #=> "Yaho!"ところで、refeでは、refe s squeezeでString#squeezeを表示してくれるのがうれしいです。

YARV

最近あちこちでYARVという単語を見かけます。Google先生に尋ねてみたら、世界最速のRuby仮想マシンを作るプロジェクトということがわかりました。 Google: YARV YARV: Yet Another Ruby VM

net/http, open-uri

Webページを取得する簡単な方法。 require "net/http" response, content = Net::HTTP.new("www.google.co.jp", 80).get("/", nil) puts response puts contentid:walf443さんから、open-uriを用いる方法を教えていただきました。open-uriを使うと、ファイル…

MutexとConditionVariable

RubyのMutexとConditionVariableを使ってProducer-Consumerを作ってみました。ブロックするキューBlockingQueueクラスです。 Mutexクラスはリエントラントではないのでちょっと注意が必要でした。 require 'thread' class BlockingQueue def initialize @que…

wcを作る

Rubyでwcを作ってみました。 files = 0 total_lines = total_words = total_chars = 0 ARGV.each do |pattern| Dir.glob(pattern) do |file| open(file, "r") do |f| f.binmode files += 1 lines = words = chars = 0 while line = f.gets lines += 1 chars …

1/0と1/0.0

Rubyで1/0はZeroDivisionErrorになるけれど、1/0.0はInfinityになります。 begin p 1/0 rescue => e p e #=> #<ZeroDivisionError: divided by 0> end begin p 1/0.0 #=> Infinity rescue => e p e end</zerodivisionerror:>