From: Luc Evers Date: 2007-11-20T23:51:56+09:00 Subject: Array object but to_a is needed to access data Hi, Below a progam (test version) that shall analyse syslog messages. The program works fine but I don't understand why to_a is needed. (filter.hash[:action].to_a[0]) filter.hash[:action].class -> answer array filter.hash[:action][0] -> don't work filter.hash[:action].to_a[0] -> ok --------------------------------------------------------------------- module State attr_reader :hash, :action def initialize @hash = Hash.new @action = Array.new end def chkurg(line) case line when /%FAN-3-FAN_FAILED/ @action << 'urgent' << 'mail' << 'sms' @hash[:alarm] = 'testurgent' @hash[:action] = @action # @hash = { :alarm => 'urgent', # :action => @action } when /CONTROLLER-2-FIRMWARE/ action << 'urgent' << 'mail' << 'sms' @hash = { :alarm => 'urgent', :action => @action } else "noState" endclass Filter include State end class Filter include State end filter = Filter.new() filename = ARGV.pop or fail "Usage: #$0 number filename" number = (ARGV.pop || 0).to_i.abs File::Tail::Logfile.open(filename) do |log| log.backward(number).tail { |line| filter.hash.clear filter.action.clear filter.chkurg(line) case filter.hash[:alarm] when 'testurgent' puts filter.hash[:action].to_a[0] puts "urgent: #{line}" when 'reject' puts "reject: #{line}" next else puts "mail: #{line}" end } end end end -- Posted via http://www.ruby-forum.com/.