Array object but to_a is needed to access data

L

Luc Evers

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
 
R

Ron Fox

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 said:
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
 
L

Luc Evers

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


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top