From: Jacob Fugal Date: 2006-02-03T09:17:48+09:00 Subject: Re: Seeking the Ruby way On 2/2/06, ara.t.howard@noaa.gov wrote: > On Fri, 3 Feb 2006, Jacob Fugal wrote: > > I'm curious why you decided to make `count` its own lambda when: > > > > 1) It's only ever used once > > 2) The block that uses it has only one statement, namely the call to `count` > > 3) count and the block to CSV::open have the same signature > > it's for abstraction only. > basically i find > > {{{{}}}} > > tough to read sometimes and factor out things using lambda. it's rare that it > acutally ends up being the the only thing left as in this case - but here you > are quite right that it can be compacted. Yeah, I agree. I often use similar abstraction techniques for readability. My brain just has the tendency to refactor code inwards as well as outwards when an abstraction seems extraneous. > > CSV::open(path,"r"){|row| sum[row.last.to_s[0,6]] += 1} > > but i disagree here. people, esp nubies will look at that and say - what? > whereas reading > > count = lambda{|row| sum[row.last.to_s[0,6]] += 1} > > ... count[row] ... > > is pretty clear. i often us variable as comments to others and myself. Again, agreed. In this case though I don't think the abstraction of naming sum[...] += 1 as count is a necessary one. If I were to refactor part of the complex expression sum[row.last.to_s[0,6]] += 1 to improve readability, it would be the index: identifier_prefix = lambda{ |row| row.last.to_s[0,6] } ... sum[identifier_prefix[row]] += 1 ... > what does this do: > > password = "#{ sifname }_#{ eval( ((0...256).to_a.map{|c| c.chr}.sort_by{rand}.select{|c| c =~ %r/[[:print:]]/})[0,4].join.inspect ) }" > > hard to say huh? Ick, yes, I'd definitely split that into chunks. :) > how about this? > > four_random_printable_chars = eval( ((0...256).to_a.map{|c| c.chr}.sort_by{rand}.select{|c| c =~ %r/[[:print:]]/})[0,4].join.inspect ) > password = "#{ sifname }_#{ four_random_printable_chars }" > > ugly (yes i'm hacking like crazy today) but at least anyone reading it (most > importantly me) knows what i'm trying to do if not how! If you say so... ;) Jacob Fugal