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/.

Works for me once I clean up the issues in your paste.. specifically:
This nonsense:

> endclass Filter
> include State
> end

towards the end of the State module def.. at least I hope these are
paste errors.

Ron.

Luc Evers wrote:

···

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

Ron Fox wrote:

Works for me once I clean up the issues in your paste.. specifically:
This nonsense:

> endclass Filter
> include State
> end

towards the end of the State module def.. at least I hope these are
paste errors.

Ron.

Ron,

Yes, these are paste faults.
I'll send the complete tail.rb + below the state.rb
And the program works but can you tell me the
(filter.hash[:action].to_a[0])
the to_a need?

tail.rb in attach

state.rb ==>

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
     when /CONTROLLER-2-FIRMWARE/
         action << 'urgent' << 'mail' << 'sms'
         @hash = { :alarm => 'urgent',
                    :action => @action }
     else
         "noState"
    end
end
end

Attachments:
http://www.ruby-forum.com/attachment/1028/tail.rb

···

--
Posted via http://www.ruby-forum.com/\.