From: Robert Klemme Date: 2012-12-28T08:27:41+09:00 Subject: Re: numeric? 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/