# Seeking the Ruby way

**URL:** <https://www.ruby-forum.com/t/seeking-the-ruby-way/53376>\
**Category:** Ruby\
**Created:** [February 2, 2006, 10:57pm UTC](https://www.ruby-forum.com/t/seeking-the-ruby-way/53376 "2006-02-02T22:57:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [February 2, 2006, 10:57pm UTC](https://www.ruby-forum.com/t/seeking-the-ruby-way/53376/1 "2006-02-02T22:57:46Z")

</div>

I’m just getting my feet wet with Ruby and would like some advice on how  
you  
“old-timers” would write the following script using Ruby idioms.

The intent of the script is to parse a CSV file that contains 2 fields  
per  
row, sorted on the second field. There may be multiple rows for field 2.  
I  
want to get a list of all of the unique values of field2 that has more  
than  
1 value for the 1st 6 characters of field 1.

Here’s what I did:

require ‘csv’

last\_account\_id = ‘’  
last\_adv\_id = ‘’  
parent\_co\_ids = []  
cntr = 0  
first = true  
CSV::Reader.parse(File.open(‘e:\tmp\20060201\bsa.csv’, ‘r’)) do |row|  
if row[1] == last\_account\_id  
parent\_co\_ids \<\< last\_adv\_id[0, 6] unless  
parent\_co\_ids.include?(last\_adv\_id[0, 6])  
else  
if !first  
parent\_co\_ids \<\< last\_adv\_id[0, 6] unless  
parent\_co\_ids.include?(last\_adv\_id[0, 6])  
if parent\_co\_ids.size \> 1  
puts “#{last\_account\_id} - (#{parent\_co\_ids.join(’,’)})”  
cntr = cntr + 1  
end  
parent\_co\_ids.clear  
else  
first = false  
end  
end  
last\_account\_id = row[1]  
last\_adv\_id = row[0]  
end  
puts “Found #{cntr} accounts with multiple parent companies”

Thanks in advance!

Todd B.

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [February 2, 2006, 11:24pm UTC](https://www.ruby-forum.com/t/seeking-the-ruby-way/53376/2 "2006-02-02T23:24:53Z")

</div>

On Fri, 3 Feb 2006, Todd B. wrote:

> require ‘csv’  
> else  
> end  
> end  
> last\_account\_id = row[1]  
> last\_adv\_id = row[0]  
> end  
> puts “Found #{cntr} accounts with multiple parent companies”
> 
> Thanks in advance!
> 
> Todd B.

harp:~ \> cat a.rb  
require “csv”  
require “yaml”

path = ARGV.shift  
sum = Hash::new{|h,k| h[k] = 0}  
count = lambda{|row| sum[row.last.to\_s[0,6]] += 1}  
CSV::open(path,“r”){|row| count[row]}  
y sum.delete\_if{|k,v| v == 1}

harp:~ \> cat in.csv  
0,aaaaaa\_\_\_  
1,aaaaaa\_\_\_  
2,aaabbb\_\_\_  
3,aaabbb\_\_\_  
4,aaabbb\_\_\_  
5,aaaccc\_\_\_

## harp:~ \> ruby a.rb in.csv

aaaaaa: 2  
aaabbb: 3

hth. regards.

-a

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [February 3, 2006, 12:10am UTC](https://www.ruby-forum.com/t/seeking-the-ruby-way/53376/3 "2006-02-03T00:10:11Z")

</div>

On 2/2/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require “csv”  
> require “yaml”
> 
> path = ARGV.shift  
> sum = Hash::new{|h,k| h[k] = 0}  
> count = lambda{|row| sum[row.last.to\_s[0,6]] += 1}  
> CSV::open(path,“r”){|row| count[row]}  
> y sum.delete\_if{|k,v| v == 1}

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

I think at a minimum, given 2) and 3), I’d just replace the block to  
CSV::open with count itself:

count = lambda{|row| sum[row.last.to\_s[0,6]] += 1}  
CSV::open(path,“r”, &count)

Then, since count isn’t used anywhere else, I’d join those together:

CSV::open(path,“r”){|row| sum[row.last.to\_s[0,6]] += 1}

After those transformations:

galadriel:~ lukfugl$ cat a.rb  
require “csv”  
require “yaml”

path = ARGV.shift  
sum = Hash::new{|h,k| h[k] = 0}  
CSV::open(path,“r”){|row| sum[row.last.to\_s[0,6]] += 1}  
y sum.delete\_if{|k,v| v == 1}

galadriel:~ lukfugl$ cat in.csv  
0,aaaaaa\_\_\_  
1,aaaaaa\_\_\_  
2,aaabbb\_\_\_  
3,aaabbb\_\_\_  
4,aaabbb\_\_\_  
5,aaaccc\_\_\_

## galadriel:~ lukfugl$ ruby a.rb in.csv

aaaaaa: 2  
aaabbb: 3

Just seems a little clearer to me over having an extra one-time use  
lambda.

Jacob F.

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [February 3, 2006, 12:37am UTC](https://www.ruby-forum.com/t/seeking-the-ruby-way/53376/4 "2006-02-03T00:37:18Z")

</div>

Jacob F. wrote:

> sum = Hash::new{|h,k| h[k] = 0}

And for some reason, I tend to write

sum = Hash.new(0)

when dealing with an immediate value. (But maybe it’s a better practice  
to use Ara’s form, so that if you ever replace 0 with, say, a matrix,  
you don’t reuse the same object for each key in the hash.)

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [February 3, 2006, 12:58am UTC](https://www.ruby-forum.com/t/seeking-the-ruby-way/53376/5 "2006-02-03T00:58:22Z")

</div>

On Fri, 3 Feb 2006, Jacob F. 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. i wrote how to count before writing the csv  
open  
line. when i wrote it ended up with something like

CSV::open(path,“r”){|row| p row; count[row]}

during editing - as i always seem to for debugging 😉

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.

> I think at a minimum, given 2) and 3), I’d just replace the block to  
> CSV::open with count itself:

> count = lambda{|row| sum[row.last.to\_s[0,6]] += 1}  
> CSV::open(path,“r”, &count)
> 
> Then, since count isn’t used anywhere else, I’d join those together:
> 
> 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.  
eg.  
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?

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!

anyhow - same goes with ‘count’: it’s all good until you start cutting  
and  
pasting - then you want vars not wicked expressions to move around.

> Just seems a little clearer to me over having an extra one-time use lambda.

**iff** you are good at reading ruby 😉

cheers.

-a

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [February 3, 2006, 1:19am UTC](https://www.ruby-forum.com/t/seeking-the-ruby-way/53376/6 "2006-02-03T01:19:32Z")

</div>

On 2/2/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Fri, 3 Feb 2006, Jacob F. 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 F.

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [February 3, 2006, 3:41am UTC](https://www.ruby-forum.com/t/seeking-the-ruby-way/53376/7 "2006-02-03T03:41:21Z")

</div>

Todd B. wrote:

> I’m just getting my feet wet with Ruby and would like some advice on how you  
> “old-timers” would write the following script using Ruby idioms.
> 
> The intent of the script is to parse a CSV file that contains 2 fields per  
> row, sorted on the second field. There may be multiple rows for field 2. I  
> want to get a list of all of the unique values of field2 that has more than  
> 1 value for the 1st 6 characters of field 1.

— input data -----  
123456ab,900  
123456cd,900  
123456ef,909  
012345gh,909  
— end of input -----

— Using a hash of arrays:

require ‘csv’

h = Hash.new{ [] }  
CSV::Reader.parse(File.open( ARGV.first )) { |row|  
h[row.last] |= [row.first[0,6] ] }  
p h.delete\_if{|k,v| v.size == 1 }

— output -----  
{“909”=\>[“123456”, “012345”]}  
— end of output -----

— Using a hash of hashes:

require ‘csv’

h = Hash.new{|h,k| h[k] = {} }  
CSV::Reader.parse(File.open( ARGV.first )) { |row|  
h[row.last][row.first[0,6] ] = 8 }  
p h.delete\_if{|k,v| v.size == 1 }

— output -----  
{“909”=\>{“012345”=\>8, “123456”=\>8}}  
— end of output -----

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [February 3, 2006, 11:19am UTC](https://www.ruby-forum.com/t/seeking-the-ruby-way/53376/8 "2006-02-03T11:19:39Z")

</div>

William J. wrote:

> h = Hash.new{ [] }

I wonder how this works since the Hash never stores these arrays.

> CSV::Reader.parse(File.open( ARGV.first )) { |row|  
> h[row.last] |= [row.first[0,6] ] }  
> p h.delete\_if{|k,v| v.size == 1 }
> 
> — output -----  
> {“909”=\>[“123456”, “012345”]}  
> — end of output -----

Is this really the output of the script above?

```
robert
```

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [February 3, 2006, 11:40am UTC](https://www.ruby-forum.com/t/seeking-the-ruby-way/53376/9 "2006-02-03T11:40:53Z")

</div>

Robert K. wrote:

> There are two possible interpretations of what you state here:
> 
> 1. You want all values for row2 that occur more than once.

Just remembered that the file is sorted. Then this implementation of  
case  
1 is even more efficient as it does not store values in mem and works on  
arbitrary large files:

require ‘csv’

last = nil  
CSV::Reader.parse(ARGF) do |row|  
last, k = row[1], last  
puts k if last == k  
end

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [February 3, 2006, 11:25am UTC](https://www.ruby-forum.com/t/seeking-the-ruby-way/53376/10 "2006-02-03T11:25:47Z")

</div>

Todd B. wrote:

> I’m just getting my feet wet with Ruby and would like some advice on  
> how you “old-timers” would write the following script using Ruby  
> idioms.
> 
> The intent of the script is to parse a CSV file that contains 2  
> fields per row, sorted on the second field. There may be multiple  
> rows for field 2. I want to get a list of all of the unique values of  
> field2 that has more than 1 value for the 1st 6 characters of field 1.

There are two possible interpretations of what you state here:

1. You want all values for row2 that occur more than once.

2. You want all values for row2 that have more than one distinct row1  
value.

Implementations:

ad 1.

require ‘csv’

h = Hash.new(0)  
CSV::Reader.parse(ARGF) {|row| h[row[1]] += 1}  
h.each {|k,v| puts k if v \> 1}

ad 2.

require ‘csv’  
require ‘set’

h = Hash.new {|h,k| h[k] = Set.new}  
CSV::Reader.parse(ARGF) {|row| h[row[1]] \<\< row[0]}  
h.each {|k,v| puts k if v.size \> 1}

Note: CSV::Reader can use ARGF which makes it easy to read from stdin as  
well as multiple files.

Kind regards

```
robert
```
