2008年12月アーカイブ

#!/usr/bin/ruby

filename=ARGV[1]
text=File.readlines(filename)
text.each_with_index do |line,i|
if line =~ /#{ARGV[0]}/
print "#{i+1}: #{line}"
end
end

1.
#!/usr/bin/ruby
class Counter
  def initialize
    @count=0
  end

  def count
    @count
  end

  def setCount(num=0)
    @count=num
  end

  def getCount
    puts @count
  end

  def increase
    @count+=1
  end

  def decrease
    @count-=1
    if @count<=0
      @count=0
    end
  end
end

oita=Counter.new
oita.setCount(10)
oita.getCount
oita.increase
oita.getCount
oita.increase
oita.getCount

実行結果
10
11
12

2.
#!/usr/bin/ruby

require "07-1"
class NewCounter < Counter
  def increase(num=1)
    @count+=num
  end

  def decrease(num=1)
    @count-=num
    if @count<=0
      @count=0
    end
  end

  def time(num)
    @count*=num
  end

  def div(num)
    @count/=num
  end
end

oita=NewCounter.new
oita.setCount(100)
oita.getCount
oita.time(3)
oita.getCount
oita.div(2)
oita.getCount

実行結果
10
11
12
100
300

このアーカイブについて

このページには、2008年12月に書かれたブログ記事が新しい順に公開されています。

前のアーカイブは2008年11月です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。