From: William James Date: 2008-02-21T09:39:56+09:00 Subject: Re: IP Address to Decimal - inject sucks On Feb 20, 4:10 pm, Ryan Davis wrote: > On Feb 20, 2008, at 11:50 , Alex Shulgin wrote: > > > On Feb 20, 3:38 am, Tiago Pinto wrote: > > >> What do you think? There's a way to do this? Is this just a silly > >> question? > > >> I know, we should be working, but we both love Ruby. > > > Everybody replied already, still I think left-shift deserves > > mentioning: > > > "127.0.0.1".split('.').inject(0) { |s,x| (s << 8) + x.to_i } > > => 2130706433 > > It does, and it is the only inject form I consider valid. What you consider valid is of no importance. > However, > converting it to map leads to a 20-25% reduction in time. Using map for this is invalid if anything is. > > Ara's pack/unpack is always what I intuitively reach for in a problem > like this, Pack/unpack is far less intuitive than inject (to a normal programmer). > but it turns out to be more expensive than a simple map/ > left-shift form. IPAddr is very powerful, but there is a cost to that > power (parsing every IP form under the sun). If I needed anything more > than JUST converting to/from ints I'd use IPAddr. We don't care what you'd use. What made you think that we do? Premature optimization is moronic. So it's probable that nothing can dissuade you. [...] > for i in 0..max do Don't come back until you know how to speak Ruby.