# My coding style

**URL:** <https://www.ruby-forum.com/t/my-coding-style/53222>\
**Category:** Ruby\
**Created:** [January 29, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/my-coding-style/53222 "2006-01-29T19:22:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@dirk](https://www.ruby-forum.com/u/dirk)\
**Post date:** [January 29, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/my-coding-style/53222/1 "2006-01-29T19:22:51Z")

</div>

hi all,  
i was wondering if my rubyness has increased above the ‘total  
newbie’-level…  
so below is some code in my coding style, can you give comments on how  
it  
looks and how effective it is.  
greetings Dirk.

def find\_factors( number )  
factors = []  
1.upto( number ) do | factor |  
if number%factor == 0  
factors \<\< factor  
end  
end  
factors  
end

def find\_prime\_numbers( max )  
factor\_list = []  
1.upto( max ) do | number |  
factor\_list \<\< find\_factors( number )  
end  
factor\_list  
end

def output\_factor\_list( factor\_list , detail=nil )  
output = “”  
if detail  
factor\_list.each\_with\_index do | factors , number |  
factors.length == 2 ? output \<\< “\*” : output \<\< " "  
output \<\< (number+1).to\_s + ’ ’ \*  
(factor\_list.length.to\_s.length-(number+1).to\_s.length) + "{ "  
factors.each do | factor |  
output \<\< "#{factor} "  
end  
output \<\< “}\n”  
end  
else  
output \<\< "{ "  
factor\_list.each\_with\_index do | factors , number |  
output \<\< "#{(number+1).to\_s} " if factors.length == 2  
end  
output \<\< “}”  
end  
output  
end

print output\_factor\_list( find\_prime\_numbers( ARGV[0].to\_i ) , ARGV[1] )

---

<div class="post-metadata">

**Author:** ![dirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@dirk](https://www.ruby-forum.com/u/dirk)\
**Post date:** [January 29, 2006, 8:11pm UTC](https://www.ruby-forum.com/t/my-coding-style/53222/2 "2006-01-29T20:11:10Z")

</div>

Hi –

On Mon, 30 Jan 2006, Dirk M. wrote:

> hi all,  
> i was wondering if my rubyness has increased above the ‘total  
> newbie’-level…  
> so below is some code in my coding style, can you give comments on how it  
> looks and how effective it is.

Traditional Ruby indentation is two spaces. One space is a bit of a  
squeeze 🙂

When it comes to stylistic things, my favorite source of information  
is the standard library. You can learn a lot by grepping around.

> def find\_factors( number )

The space after the opening parenthesis is rare:

1.8$ grep "( " `find . -name "*.rb"` | wc -l  
1641  
1.8$ grep “(” `find . -name "*.rb"` | wc -l  
37951

(and if you filter out ‘rexml’ and ‘yaml’ it drops to 544 🙂

> factors = []  
> 1.upto( number ) do | factor |

The space after the first | is non-existent in the standard library:

1.8$ grep “do |” `find . -name "*.rb"` | wc -l  
1153  
1.8$ grep “{|” `find . -name "*.rb"` | wc -l  
1512  
1.8$ grep "do | " `find . -name "*.rb"` | wc -l  
0  
1.8$ grep "{| " `find . -name "*.rb"` | wc -l  
0  
1.8$ grep "{ | " `find . -name "*.rb"` | wc -l  
0

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming May 1, 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![dirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@dirk](https://www.ruby-forum.com/u/dirk)\
**Post date:** [January 29, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/my-coding-style/53222/3 "2006-01-29T20:23:18Z")

</div>

hi,

> Traditional Ruby indentation is two spaces. One space is a bit of a  
> squeeze 🙂

i actually use tabs, but those didn’t seem to be copied properly…

i was actually aiming for comments on the program flow and the way i  
build  
up my work, but i’ll try to use less whitespace in inapropriate places  
😉  
greetings, Dirk.

---

<div class="post-metadata">

**Author:** ![dirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@dirk](https://www.ruby-forum.com/u/dirk)\
**Post date:** [January 29, 2006, 8:53pm UTC](https://www.ruby-forum.com/t/my-coding-style/53222/4 "2006-01-29T20:53:29Z")

</div>

On Jan 29, 2006, at 1:10 PM, removed\_email\_address@domain.invalid wrote:

> > def find\_factors( number )
> 
> The space after the opening parenthesis is rare:

I sure like it though. Makes those argument lists stand out. All my  
libraries are just like this, but it’s true that I don’t have  
anything in the standard library. 🙂

James Edward G. II

---

<div class="post-metadata">

**Author:** ![dirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@dirk](https://www.ruby-forum.com/u/dirk)\
**Post date:** [January 29, 2006, 8:25pm UTC](https://www.ruby-forum.com/t/my-coding-style/53222/5 "2006-01-29T20:25:09Z")

</div>

Hi Dirk,

I have translated your code a bit. You can make it much shorter if you  
use blocks:

This method selects all factors of a number. Here, I first created a  
list of values from 1 to n (1…n). Then only the factors are selected  
(the non-factors are filtered).

def find\_factors(n)  
(1…n).select{|factor| n % factor == 0}  
end

This method creates a list of numbers from 1 to max (1…max). Then it  
replaces each number n in the list with find\_factors(n). That is what  
map does:

def find\_prime\_numbers(max)  
(1…max).map{|n| find\_factors(n)}  
end

The output method is still very tricky, and not elegant. I first changed  
factor\_list.length.to\_s.length to Math.log10(list.length).ceil. Then I  
moved it out of the loop, because it is more efficient (it will only be  
computed once, and not again in every iteration). I think you know what  
log10 is? Well, log10 and then ceil (round up) returns the number of  
numbers in a number. So 23 has 2 numbers, and 1234 has 4.

Then I replaced the output variable with inject, and I made some minor  
changes like:

factors.length == 2 ? output \<\< “\*” : output \<\< " "

to:

output + (factors.length == 2 ? ‘\*’ : ’ ')

And

factors.each do | factor |  
output \<\< "#{factor} "  
end

to:

factors.join(’ ')

and in non-detail mode, I used select and join again.

def output\_factor\_list(list, detail = false)  
if detail  
spaces = Math.log10(list.length).ceil  
number = 0  
list.inject(’’) do |output, factors|  
number += 1  
output + (factors.length == 2 ? ‘\*’ : ’ ‘) +  
number.to\_s + (’ ’ \* (spaces - Math.log10(number + 1).ceil))  
+  
‘{ ’ + factors.join(’ ') + " } \n"  
end  
else  
‘{ ’ + list.select{|factors| factors.length == 2}.join(’ ') + ’  
} ’  
end  
end

Everything combined:

def find\_factors(n)  
(1…n).select{|factor| n % factor == 0}  
end

def find\_prime\_numbers(max)  
(1…max).map{|n| find\_factors(n)}  
end

def output\_factor\_list(list, detail = false)  
if detail  
spaces = Math.log10(list.length).ceil  
number = 0  
list.inject(’’) do |output, factors|  
number += 1  
output + (factors.length == 2 ? ‘\*’ : ’ ‘) +  
number.to\_s + (’ ’ \* (spaces - Math.log10(number + 1).ceil))  
+  
‘{ ’ + factors.join(’ ') + " } \n"  
end  
else  
‘{ ’ + list.select{|factors| factors.length == 2}.join(’ ') + ’  
} ’  
end  
end

print output\_factor\_list(find\_prime\_numbers(200), false)

I think most of the changes are from imperative to functional.  
Inject/map/select /join really make things simpler, but they are hard to  
understand if you haven’t used them.

I hope this was helpful,

Jules

---

<div class="post-metadata">

**Author:** ![dirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@dirk](https://www.ruby-forum.com/u/dirk)\
**Post date:** [January 29, 2006, 9:11pm UTC](https://www.ruby-forum.com/t/my-coding-style/53222/6 "2006-01-29T21:11:42Z")

</div>

Jules J. wrote:

> Hi Dirk,  
> […lot’s of stuff i also did 🙂 …]  
> The output method is still very tricky, and not elegant. I first changed  
> factor\_list.length.to\_s.length to Math.log10(list.length).ceil. Then I  
> moved it out of the loop, because it is more efficient (it will only be  
> computed once, and not again in every iteration). I think you know what  
> log10 is? Well, log10 and then ceil (round up) returns the number of  
> numbers in a number. So 23 has 2 numbers, and 1234 has 4.

## What about:

def find\_factors(number)  
(1…number).select{|factor| (number % factor).zero?}  
end

def find\_prime\_numbers(max)  
(1…max).map{|number| find\_factors(number)}  
end

def output\_factor\_list( factor\_list , detail=nil )  
if detail  
(1…factor\_list.size).map do |number|  
(factor\_list[number - 1].length == 2 ? ‘\*’ : ’ ‘) +  
number.to\_s.ljust(factor\_list.length.to\_s.length) +  
‘{ ’ + factor\_list[number - 1].join(’ ‘) + ‘}’  
end.join("\n")  
else  
"{ "+factor\_list.select{|factors| factors.length == 2}.join(’  
‘)+’}’  
end  
end

## puts output\_factor\_list( find\_prime\_numbers( 20 ) , true)

> I think most of the changes are from imperative to functional.  
> Inject/map/select /join really make things simpler, but they are hard to  
> understand if you haven’t used them.

It’s not that hard, and you will feel like someone cut of your right  
hand if you have to live without them again.

cheers

Simon

---

<div class="post-metadata">

**Author:** ![dirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@dirk](https://www.ruby-forum.com/u/dirk)\
**Post date:** [February 3, 2006, 11:41pm UTC](https://www.ruby-forum.com/t/my-coding-style/53222/7 "2006-02-03T23:41:39Z")

</div>

Jules, this was very helpful. Thanks for taking the time  
writing this up!

Hans

---

<div class="post-metadata">

**Author:** ![dirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@dirk](https://www.ruby-forum.com/u/dirk)\
**Post date:** [February 4, 2006, 1:54am UTC](https://www.ruby-forum.com/t/my-coding-style/53222/8 "2006-02-04T01:54:08Z")

</div>

Hans,

It’s nice you found this helpful. I might also, if I had some idea to  
what  
you are referring. This posting style, where there is no previous post  
attached, thus giving a context for your post, is worthless to all but  
two  
of you.

I’m looking for whatever it was Jules did that so nice. Haven’t found it  
so far.

t.

On Fri, 03 Feb 2006 14:41:55 -0800, Hans G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Jules, this was very helpful. Thanks for taking the time  
> writing this up!
> 
> Hans

–

# ================================================ Tom C., MS MA, LMHC Private practice Psychotherapist Bellingham, Washington, U.S.A: (360) 920-1226 \<\< [TC.BestMindHealth.com](http://TC.BestMindHealth.com) / [BestMindHealth.com](http://BestMindHealth.com) \>\> \<\< removed\_email\_address@domain.invalid \>\>

---

<div class="post-metadata">

**Author:** ![dirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@dirk](https://www.ruby-forum.com/u/dirk)\
**Post date:** [February 13, 2006, 3:14am UTC](https://www.ruby-forum.com/t/my-coding-style/53222/9 "2006-02-13T03:14:04Z")

</div>

Jules J. wrote:

> Hi Dirk,  
> .  
> .  
> .  
> print output\_factor\_list(find\_prime\_numbers(200), false)
> 
> I think most of the changes are from imperative to functional.  
> Inject/map/select /join really make things simpler, but they are hard to  
> understand if you haven’t used them.
> 
> I hope this was helpful,
> 
> Jules

Thanks Jules,

Your “coding style” seems very clean, elegant and understandable. I’m  
very new to Ruby and I’ve had somewhat of a hardtime getting going with  
it.

I just wanted to say thanks for the detailed explination of what looks  
to me like ideal ruby code.

Hector
