[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Any guides for good coding in Ruby?

arfinmail

3/22/2005 9:09:00 PM

Is there some kind of class to format numbers? Something to let you
transform:

1 => "0001"
1234 => "1,234.00"

I needed to display the day and month in a 00 format and the way I did
it was adding a method to the Integer class

class Integer
def addZero
return "0" + self.to_s if self.to_s.length == 1
return self.to_s
end
end
t = Time.now
p "The date is: #{t.year}#{t.month.addZero}#{t.day.addZero}"

What do you guys think of this solution?


On a side note, could you guys give a few rules of thumb you follow in
order to make your code clean and readable?

73 Answers

James Gray

3/22/2005 9:26:00 PM

0

On Mar 22, 2005, at 3:09 PM, Arfin wrote:

> Is there some kind of class to format numbers? Something to let you
> transform:
>
> 1 => "0001"
> 1234 => "1,234.00"

I'm not aware of a core Ruby tool for commiying numbers, but it's not
hard to roll a solution:

def commify( number )
        text = number.to_s.reverse
        text.gsub!(/(\d\d\d)(?=\d)(?!\ d*\.)/, '\1,')
        text.reverse
end

> I needed to display the day and month in a 00 format and the way I did
> it was adding a method to the Integer class
>
> class Integer
> def addZero
> return "0" + self.to_s if self.to_s.length == 1
> return self.to_s
> end
> end
> t = Time.now
> p "The date is: #{t.year}#{t.month.addZero}#{t.day.addZero}"
>
> What do you guys think of this solution?

Kernel#sprintf and it's shortcut cousin String#% will handle this for
you. Here's an example:

"%02d" % 1 => "01"

Also look into Time#strftime for tasks like the above.

> On a side note, could you guys give a few rules of thumb you follow in
> order to make your code clean and readable?

Of course, there are loads of rules here and every person that gives
you a list will be different. Common sense ideas like, "Use good
variable names," apply in Ruby as they do everywhere else.

One of Ruby's great strengths is readability, I think. Play to your
strengths. Give the extra five keystrokes to make things utterly
clear, but don't be verbose if a concise line of code is obvious.

As always, common sense is needed.

James Edward Gray II




Nikolai Weibull

3/22/2005 9:27:00 PM

0

* Arfin (Mar 22, 2005 22:10):
> I needed to display the day and month in a 00 format â?¦

printf("%02d", 1),
nikolai

--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: minimalistic.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


dblack

3/22/2005 9:30:00 PM

0

Bill Guindon

3/22/2005 9:33:00 PM

0

On Wed, 23 Mar 2005 06:09:50 +0900, Arfin <arfinmail@yahoo.com> wrote:
> Is there some kind of class to format numbers? Something to let you
> transform:
>
> 1 => "0001"
> 1234 => "1,234.00"
>
> I needed to display the day and month in a 00 format and the way I did
> it was adding a method to the Integer class
>
> class Integer
> def addZero
> return "0" + self.to_s if self.to_s.length == 1
> return self.to_s
> end
> end
> t = Time.now
> p "The date is: #{t.year}#{t.month.addZero}#{t.day.addZero}"
>
> What do you guys think of this solution?

Might be easier with "printf" - although if you're not familiar with
it, might take a bit of work to get the format right.

> On a side note, could you guys give a few rules of thumb you follow in
> order to make your code clean and readable?

Some helpful stuff here:
http://www.caliban.org/ruby/rubyg...

Happy Rubying
--
Bill Guindon (aka aGorilla)


Florian Gross

3/22/2005 9:34:00 PM

0

Arfin wrote:

> Is there some kind of class to format numbers? Something to let you
> transform:
>
> 1 => "0001"

"%04d" % 1

This is pretty much the same as sprintf() in C.

But in this case you can also do:

"1".rjust(4, "0")

> 1234 => "1,234.00"

I'd do that like this, even though there's other versions:

def number_format(number, padding_char = ",")
result = number.to_s
while pos = result.rindex(/\d{4}(?=\D|\Z)/)
result[pos + 1, 0] = padding_char
end
return result
end

irb(main):001:0> number_format 100_000_000
=> "100,000,000"
irb(main):002:0> number_format 10_000_000_000
=> "10,000,000,000"
irb(main):003:0> number_format "foo 10000000000 bar 50 qux 5000"
=> "foo 10,000,000,000 bar 50 qux 5,000"

Nikolai Weibull

3/22/2005 9:42:00 PM

0

* Arfin (Mar 22, 2005 22:10):
> Is there some kind of class to format numbers? Something to let you
> transform:

> 1 => "0001" 1234 => "1,234.00"

Btw, the ruby-lisp module (check http://raa.ruby...) has a method
for outputting numbers in this format,
nikolai

--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: minimalistic.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


martinus

3/22/2005 10:22:00 PM

0

> I try to follow the style that is predominant in the Ruby parts of the
> Ruby source. Also, see:

I also do this, but there is one thing I cannot stand: every ruby lib
uses 2 spaces for indentation! IMHO it should be tabs, and *only* tabs.
This is much better, because in almost every editor it is possible to
set the tab width to whatever one likes. I hate 2 spaced sourcecode,
because I usually set one tab == 8 spaces, and use a non-fixed font.

martinus

Patrick Bennett

3/22/2005 10:35:00 PM

0

Martin Ankerl wrote:

>> I try to follow the style that is predominant in the Ruby parts of the
>> Ruby source. Also, see:
>
>
> I also do this, but there is one thing I cannot stand: every ruby lib
> uses 2 spaces for indentation! IMHO it should be tabs, and *only*
> tabs. This is much better, because in almost every editor it is
> possible to set the tab width to whatever one likes. I hate 2 spaced
> sourcecode, because I usually set one tab == 8 spaces, and use a
> non-fixed font.
>
I dislike 2 spaces as well, but tabs are worse. It's *extremely* and I
mean *extremely* rare for me to see a source file that uses tabs correctly.
Tabs can *never* be used for anything other than indenting the first
non-tab character on a line. That's it. As soon as they're used
anywhere else on a line following non-tab characters to 'line up' code,
then you've broken the ability for other users to use different tab
settings and view your code the same way. The *only* way of assuring a
consistent representation of source code is to use spaces and only
spaces for indentation and column alignment. Modern editors make using
spaces for tabs a non-issue anyway.

Patrick Bennett



vruz

3/22/2005 10:42:00 PM

0

> On a side note, could you guys give a few rules of thumb you follow in
> order to make your code clean and readable?


An rpa guide to decent API design:

http://rpa-base.rubyforge.org/wiki/wiki.cgi?Goo...

cheers,
vruz


James Gray

3/22/2005 10:48:00 PM

0

On Mar 22, 2005, at 4:24 PM, Martin Ankerl wrote:

> I also do this, but there is one thing I cannot stand: every ruby lib
> uses 2 spaces for indentation! IMHO it should be tabs, and *only*
> tabs. This is much better, because in almost every editor it is
> possible to set the tab width to whatever one likes. I hate 2 spaced
> sourcecode, because I usually set one tab == 8 spaces, and use a
> non-fixed font.

Amen! I don't know who thought up the two space thing, but they were
out of line. :) In Ruby, you don't even need the extra characters to
keep lines short, generally. I'm set tabs to four spaces wide, but
it's definitely tabs all the way.

And while we're getting things off our chest, it really bugs me when
people don't keep their code within the 80 character boundary
guideline. I've been reading all the links posted in this thread and
they've all recommended it, but I can sure tell you from running Ruby
Quiz that not everyone is listening. ;)

James Edward Gray II