Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > my coding style

Reply
Thread Tools

my coding style

 
 
Dirk Meijer
Guest
Posts: n/a
 
      01-29-2006
------=_Part_8362_9999763.1138558936898
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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 =3D []
1.upto( number ) do | factor |
if number%factor =3D=3D 0
factors << factor
end
end
factors
end

def find_prime_numbers( max )
factor_list =3D []
1.upto( max ) do | number |
factor_list << find_factors( number )
end
factor_list
end

def output_factor_list( factor_list , detail=3Dnil )
output =3D ""
if detail
factor_list.each_with_index do | factors , number |
factors.length =3D=3D 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 =3D=3D 2
end
output << "}"
end
output
end

print output_factor_list( find_prime_numbers( ARGV[0].to_i ) , ARGV[1] )

------=_Part_8362_9999763.1138558936898--


 
Reply With Quote
 
 
 
 
dblack@wobblini.net
Guest
Posts: n/a
 
      01-29-2006
Hi --

On Mon, 30 Jan 2006, Dirk Meijer 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
http://www.velocityreviews.com/forums/(E-Mail Removed)

"Ruby for Rails", from Manning Publications, coming May 1, 2006!
http://www.manning.com/books/black


 
Reply With Quote
 
 
 
 
Dirk Meijer
Guest
Posts: n/a
 
      01-29-2006
------=_Part_8758_3815124.1138562417418
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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.

------=_Part_8758_3815124.1138562417418--


 
Reply With Quote
 
Jules Jacobs
Guest
Posts: n/a
 
      01-29-2006
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

--
Posted via http://www.ruby-forum.com/.


 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      01-29-2006
On Jan 29, 2006, at 1:10 PM, (E-Mail Removed) 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 Gray II




 
Reply With Quote
 
Simon Kröger
Guest
Posts: n/a
 
      01-29-2006
Jules Jacobs 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


 
Reply With Quote
 
Hans Granqvist
Guest
Posts: n/a
 
      02-03-2006
Jules, this was very helpful. Thanks for taking the time
writing this up!

Hans

--
Posted via http://www.ruby-forum.com/.


 
Reply With Quote
 
Tom Cloyd
Guest
Posts: n/a
 
      02-04-2006
Hans,

It's nice you found this helpful. I might also, if I had some idea to wha=
t =20
you are referring. This posting style, where there is no previous post =20
attached, thus giving a context for your post, is worthless to all but tw=
o =20
of you.

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

t.

On Fri, 03 Feb 2006 14:41:55 -0800, Hans Granqvist <(E-Mail Removed)> =20
wrote:

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




--=20

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< TC.BestMindHealth.com / BestMindHealth.com >>
<< (E-Mail Removed) >>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D


 
Reply With Quote
 
Hector Quiroz
Guest
Posts: n/a
 
      02-13-2006
Jules Jacobs 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

--
Posted via http://www.ruby-forum.com/.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
general coding issues - coding style... calmar Python 11 02-21-2006 10:36 AM
Coding style for CPLD vs FPGA Analog Guy VHDL 3 03-14-2005 08:26 AM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
Coding style to prioritize certain inputs Willem Oosthuizen VHDL 5 09-04-2003 05:17 PM
style for coding latches Paul Baxter VHDL 7 08-15-2003 06:19 PM



Advertisments