# Array object but to\_a is needed to access data

**URL:** <https://www.ruby-forum.com/t/array-object-but-to-a-is-needed-to-access-data/120778>\
**Category:** Ruby\
**Created:** [November 20, 2007, 3:51pm UTC](https://www.ruby-forum.com/t/array-object-but-to-a-is-needed-to-access-data/120778 "2007-11-20T15:51:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucevers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b19c9b/32.png) [@lucevers](https://www.ruby-forum.com/u/lucevers)\
**Post date:** [November 20, 2007, 3:51pm UTC](https://www.ruby-forum.com/t/array-object-but-to-a-is-needed-to-access-data/120778/1 "2007-11-20T15:51:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lucevers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b19c9b/32.png) [@lucevers](https://www.ruby-forum.com/u/lucevers)\
**Post date:** [November 21, 2007, 11:58am UTC](https://www.ruby-forum.com/t/array-object-but-to-a-is-needed-to-access-data/120778/2 "2007-11-21T11:58:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lucevers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b19c9b/32.png) [@lucevers](https://www.ruby-forum.com/u/lucevers)\
**Post date:** [November 23, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/array-object-but-to-a-is-needed-to-access-data/120778/3 "2007-11-23T20:05:37Z")

</div>

Ron F. 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
