From: Brandon Weaver Date: 2012-12-28T09:00:00+09:00 Subject: Re: numeric? --047d7b338dd367bc0e04d1de5725 Content-Type: text/plain; charset=ISO-8859-1 I've already sorted them into classes and objects, so instance variables are created for every antenna and other such things. On Thu, Dec 27, 2012 at 5:27 PM, Robert Klemme wrote: > On Thu, Dec 27, 2012 at 11:32 PM, Brandon Weaver > wrote: > > Sorry, I wasn't very clear. I was typing most of that from my Phone. > > > > I'm writing scripts for a wireless ISP, and we use several different > types > > of antennas. They're monitored in several different ways, > > SSH/Telnet/SNMP/etc. Annoyingly they all return different values for > null, > > but they're always string based, so a numeric evaluation will quickly > yield > > whether the device is working or down. I could write a more correct > solution > > in the sense that it's less of a hack with individual classes for every > > antenna, but the goal is that if we ever get a new type of equipment > that it > > will take minimal coding to get a framework up and running for it. > > What's wrong then to use Float() as suggested? > > begin > f = Float(from_device) > printf "Read value %20.5f\n", f > rescue ArgumentError => e > # not a float > end > > An alternative would be to convert non numeric values to nil - if you > want safe null handling: > > f = Float(from_device) rescue nil > > > One such example is min, max, and avg for multiple fields such as Signal > to > > Noise, Retransmit, and etc. I used instance_variable_get to dynamically > > evaluate fields. There are ways in which I could probably use more of > > Enumerator, and I still need to sort that out some more. > > You could use Structs and then: > > irb(main):001:0> S=Struct.new :a, :b, :c > => S > irb(main):002:0> S.members > => [:a, :b, :c] > irb(main):003:0> S.new.members > => [:a, :b, :c] > irb(main):004:0> s = S.new 1,2,3 > => # > irb(main):005:0> s.members > => [:a, :b, :c] > irb(main):006:0> s.each_pair {|k,v| printf "%p=%p\n", k, v} > :a=1 > :b=2 > :c=3 > => # > > Kind regards > > robert > > -- > remember.guy do |as, often| as.you_can - without end > http://blog.rubybestpractices.com/ > > --047d7b338dd367bc0e04d1de5725 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable I've already sorted them into classes and objects, so instance variable= s are created for every antenna and other such things.

On Thu, Dec 27, 2012 at 5:27 PM, Robert Klemme <sho= rtcutter@googlemail.com> wrote:
On Thu, Dec 27, 2012 at 11= :32 PM, Brandon Weaver
<keystonelemur@gmail.com&= gt; wrote:
> Sorry, I wasn't very clear. I was typing most of that from my Phon= e.
>
> I'm writing scripts for a wireless ISP, and we use several differe= nt types
> of antennas. They're monitored in several different ways,
> SSH/Telnet/SNMP/etc. Annoyingly they all return different values for n= ull,
> but they're always string based, so a numeric evaluation will quic= kly yield
> whether the device is working or down. I could write a more correct so= lution
> in the sense that it's less of a hack with individual classes for = every
> antenna, but the goal is that if we ever get a new type of equipment t= hat it
> will take minimal coding to get a framework up and running for it.

What's wrong then to use Float() as suggested?

begin
=A0 f =3D Float(from_device)
=A0 printf "Read value %20.5f\n", f
rescue ArgumentError =3D> e
=A0 # not a float
end

An alternative would be to convert non numeric values to nil - if you
want safe null handling:

f =3D Float(from_device) rescue nil

> One such example is min, max, and avg for multiple fields such as Sign= al to
> Noise, Retransmit, and etc. I used instance_variable_get to dynamicall= y
> evaluate fields. There are ways in which I could probably use more of<= br> > Enumerator, and I still need to sort that out some more.

You could use Structs and then:

irb(main):001:0> S=3DStruct.new :a, :b, :c
=3D> S
irb(main):002:0> S.members
=3D> [:a, :b, :c]
irb(main):003:0> S.new.members
=3D> [:a, :b, :c]
irb(main):004:0> s =3D S.new 1,2,3
=3D> #<struct S a=3D1, b=3D2, c=3D3>
irb(main):005:0> s.members
=3D> [:a, :b, :c]
irb(main):006:0> s.each_pair {|k,v| printf "%p=3D%p\n", k, v}<= br> :a=3D1
:b=3D2
:c=3D3
=3D> #<struct S a=3D1, b=3D2, c=3D3>

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blo= g.rubybestpractices.com/


--047d7b338dd367bc0e04d1de5725--