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

**URL:** <https://rubytalk.org/t/array-object-but-to-a-is-needed-to-access-data/42369>\
**Category:** ruby-talk\
**Created:** [20 November 2007 14:51 UTC](https://rubytalk.org/t/array-object-but-to-a-is-needed-to-access-data/42369 "2007-11-20T14:51:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luc\_Evers](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@Luc\_Evers](https://rubytalk.org/u/Luc_Evers)\
**Post date:** [20 November 2007 14:51 UTC](https://rubytalk.org/t/array-object-but-to-a-is-needed-to-access-data/42369/1 "2007-11-20T14:51:56Z")

</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  
> &nbsp;&nbsp;&nbsp;@hash = Hash.new  
> &nbsp;&nbsp;&nbsp;@action = Array.new  
> end
> 
> def chkurg(line)  
> &nbsp;&nbsp;&nbsp;&nbsp;case line  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when /%FAN-3-FAN\_FAILED/  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@action \<\< 'urgent' \<\< 'mail' \<\< 'sms'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@hash[:alarm] = 'testurgent'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@hash[:action] = @action  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# @hash = { :alarm =\> 'urgent',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# :action =\> @action }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when /CONTROLLER-2-FIRMWARE/  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action \<\< 'urgent' \<\< 'mail' \<\< 'sms'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@hash = { :alarm =\> 'urgent',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:action =\> @action }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"noState"  
> &nbsp;&nbsp;&nbsp;&nbsp;endclass Filter  
> &nbsp;&nbsp;&nbsp;&nbsp;include State  
> end
> 
> class Filter  
> &nbsp;&nbsp;&nbsp;&nbsp;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|  
> &nbsp;&nbsp;log.backward(number).tail { |line|  
> &nbsp;&nbsp;filter.hash.clear  
> &nbsp;&nbsp;filter.action.clear  
> &nbsp;&nbsp;filter.chkurg(line)  
> &nbsp;&nbsp;&nbsp;case filter.hash[:alarm]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 'testurgent'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts filter.hash[:action].to\_a[0]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "urgent: #{line}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 'reject'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "reject: #{line}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "mail: #{line}"  
> &nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;}  
> end
> 
> end  
> end  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Ron\_Fox](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@Ron\_Fox](https://rubytalk.org/u/Ron_Fox)\
**Post date:** [20 November 2007 17:55 UTC](https://rubytalk.org/t/array-object-but-to-a-is-needed-to-access-data/42369/2 "2007-11-20T17:55:01Z")

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

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  
> > &nbsp;&nbsp;&nbsp;@hash = Hash.new  
> > &nbsp;&nbsp;&nbsp;@action = Array.new  
> > end
> > 
> > def chkurg(line)  
> > &nbsp;&nbsp;&nbsp;&nbsp;case line  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when /%FAN-3-FAN\_FAILED/  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@action \<\< 'urgent' \<\< 'mail' \<\< 'sms'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@hash[:alarm] = 'testurgent'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@hash[:action] = @action  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# @hash = { :alarm =\> 'urgent',  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# :action =\> @action }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when /CONTROLLER-2-FIRMWARE/  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action \<\< 'urgent' \<\< 'mail' \<\< 'sms'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@hash = { :alarm =\> 'urgent',  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:action =\> @action }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"noState"  
> > &nbsp;&nbsp;&nbsp;&nbsp;endclass Filter  
> > &nbsp;&nbsp;&nbsp;&nbsp;include State  
> > end
> > 
> > class Filter  
> > &nbsp;&nbsp;&nbsp;&nbsp;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|  
> > &nbsp;&nbsp;log.backward(number).tail { |line|  
> > &nbsp;&nbsp;filter.hash.clear  
> > &nbsp;&nbsp;filter.action.clear  
> > &nbsp;&nbsp;filter.chkurg(line)  
> > &nbsp;&nbsp;&nbsp;case filter.hash[:alarm]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 'testurgent'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts filter.hash[:action].to\_a[0]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "urgent: #{line}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 'reject'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "reject: #{line}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "mail: #{line}"  
> > &nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;}  
> > end
> > 
> > end  
> > end

---

<div class="post-metadata">

**Author:** ![Luc\_Evers](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@Luc\_Evers](https://rubytalk.org/u/Luc_Evers)\
**Post date:** [23 November 2007 19:05 UTC](https://rubytalk.org/t/array-object-but-to-a-is-needed-to-access-data/42369/3 "2007-11-23T19:05:37Z")

</div>

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  
&nbsp;&nbsp;&nbsp;@hash = Hash.new  
&nbsp;&nbsp;&nbsp;@action = Array.new  
end

def chkurg(line)  
&nbsp;&nbsp;&nbsp;&nbsp;case line  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when /%FAN-3-FAN\_FAILED/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@action \<\< 'urgent' \<\< 'mail' \<\< 'sms'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@hash[:alarm] = 'testurgent'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@hash[:action] = @action  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when /CONTROLLER-2-FIRMWARE/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action \<\< 'urgent' \<\< 'mail' \<\< 'sms'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@hash = { :alarm =\> 'urgent',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:action =\> @action }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"noState"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end  
end

Attachments:  
[http://www.ruby-forum.com/attachment/1028/tail.rb](http://www.ruby-forum.com/attachment/1028/tail.rb)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
