Any guides for good coding in Ruby?

* Sascha Ebach (Mar 23, 2005 01:30):

Nice to have so many experts here :slight_smile: I have actually been fighting
around with this issue. I have found that using tabs always gives me
headaches somewhere. For example when I paste code examples into emails.

What do I have to set in vim if I always want two spaces (also when I
press the tab key)? No exceptions. I essentially want this:

class PageController
  def edit
    case @request.method
      when :get
        @page = Page.find(@params['id'])
      when :post
    ...
  end
end

What? You want tab to equal two spaces, regardless of column? Then,

  :imap <Tab> <Space><Space>

is what you want, but, again, are you sure that's what you want?,
        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);}

Sam Roberts wrote:

That's funny, I haven't found that emacs uses spaces to indent by
default. Its supposed to default to the GNU coding standard, though what
it does on any machine is pretty subject to the whims of who installed
it. The emacs standard/GNU indent style is the weirdest around, IMO.

Yeah, I don't like its default behaviour. I very rarely use emacs without a .emacs file that overrides this. It's pretty easy:

In your .emacs file:

(setq indent-tabs-mode nil)

Or from within emacs: "M-x customize-option" "indent-tabs-mode" and choose off (nil).

Or at any point for a given session: "M-x set-variable" "indent-tabs-mode" "nil"

I'm averse to indents over about 4, and I think tabs are lame. They work
fine if everybody on your team uses the same settings, but when anybody
else looks at your code, it will look bad.

Or when you look at it in a web browser, which can't (afaik) be customized, etc. etc.

I think a bunch or ruby core developers (matz, for one) use emacs, so
many of the files use tabs, and indent two char widths per nesting
level. Not all, though.

Maybe if we keep this off-topic thread going long enough they'll fix their editors just to shut us up.

Ben

Phil Tomson wrote:

The lucky thing for us Ruby users is that the number of spaces/tabs (while the two might look the same on the screen) doesn't matter to the interpreter as it does with another language that starts with a P.

Does mixing tabs & spaces munge YAML text?

James

···

--

http://catapult.rubyforge.com
http://orbjson.rubyforge.com
http://ooo4r.rubyforge.com
http://www.jamesbritt.com

I disagree. I use 80 character width for everything. I have three
xterms spanned across my screen - all 80 characters wide. My IDEs have
the size of the window limited to 80 characters wide. It's the perfect
size. If a line goes too far then I know it's too complicated (expect
strings, string often span lines) or I'm writing in Java. I think Java
kills the 80 characters wides... That's another story though.

I think everyone who doesn't use 80 characters (or less) should be
shot. But just in the foot, so they'll learn the error of their ways.
:slight_smile:

···

On Wed, 23 Mar 2005 09:11:59 +0900, Nikolai Weibull <mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:

* James Edward Gray II (Mar 22, 2005 23:50):
Seriously, though, the 80 character boundary is a thing of the past.
Terminals (not to mention emulators) can be made wider, 132 being a good
"standard" setting, so it's not even about backwards compatibility
anymore. I find that I can write code the way I want it if I'm not
constrained to 80 characters per line. (I only switched to a
132-character-wide terminal about 3 months ago, though, so I may change
my mind yet again.),

--
~Travis

Yes! 2 spaces is the way, the light, the shining path!
Death to all Tab heretics!

Just kidding (well, mostly anyway)

···

On Wed, 23 Mar 2005 08:13:50 +0900, Jim Freeze <jim@freeze.org> wrote:

* Kent Loobey <kent@darkwing.uoregon.edu> [2005-03-23 08:08:18 +0900]:

> On Tuesday 22 March 2005 02:48 pm, James Edward Gray II wrote:
> > On Mar 22, 2005, at 4:24 PM, Martin Ankerl wrote:

Now that you guys have had a chance to air your feelings,
let's all get back to work, writing Ruby code with 2 spaces,
just as Matz does.

Oh, and by the way, it's four spaces if you are using Ruby C.

--
Bill Guindon (aka aGorilla)

Now that you guys have had a chance to air your feelings,
let's all get back to work, writing Ruby code with 2 spaces,
just as Matz does.

I feel much better now, and am now fit for some more years of 2-spaced ruby code. :slight_smile:

martinus

Nikolai Weibull wrote:

It's very easy to do with vim. Simply leave your 'ts' alone, set 'sw' to
8, don't set 'sts' and don't set 'et'. Those are all default settings
by the way.

And people say vi isn't intuitive!

:wink:

In a perfect world, people would actually use tabs appropriately. Until
they do, the best solution is to not use them,

Yeah, I agree.

Ben

* Jim Freeze (Mar 23, 2005 01:30):

One problem with tabs is that people tend to mix tabs and spaces,
then, when I set the tab to be 2-spaces, I may get 2,3 or 4 spaced
indents.

There's no good reason to change the width of tabs. Your editor should
be able to emulate this behaviour anyway. In vim, to get two-space
indents:

  :set softtabstop=2 shiftwidth=2

or

  :set sts=2 sw=2

if you're lazy, and leave the 'tabstop' setting alone,
        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);}

* James Edward Gray II (Mar 23, 2005 01:40):

> >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. :wink:
>
> Actually, for the Quiz, 72 would be a better limit, as that works
> best for mails.
>
> Seriously, though, the 80 character boundary is a thing of the past.

All space and tab holy war fun aside, I strongly disagree on this
point and would like a chance to plead my case. Here's some food for
thought: If it's so in the past, why does almost everyone still
recommend it?

The truth is that is has nothing to do with terminals anymore, in my
not at all humble opinion. You've just pointed out a great reason
above: email clients. Here's another one: Web pages. 90% of the
problems I have with the current Ruby Quiz site are that the code
often overflows the boxes. I hand edit each and every chunk of code
in a never-ending battle against this. I'm trying to come up with a
good solution for it in the Ruby Quiz 2.0 site, but that's harder than
it sounds. I want the new site to include the code as that'll open up
a lot of exciting options, but I have to figure out how to do this
realistically first and this issue is the biggest hurdle.

As I said, 72 would be a better limit for the Quiz.

Ironically, I still don't think those are the biggest reason to do it.

I believe the number one reason you should still keep code at 80
characters per line is to help authors put your work in their books.
It just so happens that a typical programming book with reasonable
fonts and light indenting of the code examples has darn near 80
characters of space to play with. You do want everyone writing about
your code don't you? I bet you'll shorten that margin back up when
you begin writing your first masterpiece. :wink:

How often is this the case? I have yet to see a book displaying
snippets of code from actual software beyond showing of horrible coding
practices (perhaps a good reason to stay within the 80-characters-per-
line limit, as you don't want to end up in one of those books).

I know Holub on Patterns is an exception (a book you seem to hold very
dear), as it is more or less a "patterns in the wild"-kinda book.
Still, not many books are written that way. Most write the code for the
book, not the other way around.

To summarize, it isn't just about terminals. Be kind to your email
reader, webmaster, and author. Stick with 80.

Be kind to yourself: expand your horizon,
        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);}

James Edward Gray II wrote:

To summarize, it isn't just about terminals. Be kind to your email reader, webmaster, and author. Stick with 80.

The 80 char limit never was about terminals. It was about punched cards.

Nikolai Weibull wrote:

* Sascha Ebach (Mar 23, 2005 01:30):

Nice to have so many experts here :slight_smile: I have actually been fighting around with this issue. I have found that using tabs always gives me headaches somewhere. For example when I paste code examples into emails.

What do I have to set in vim if I always want two spaces (also when I press the tab key)? No exceptions. I essentially want this:

class PageController
def edit
   case @request.method
     when :get
       @page = Page.find(@params['id'])
     when :post
   ...
end
end

What? You want tab to equal two spaces, regardless of column? Then,

  :imap <Tab> <Space><Space>

is what you want, but, again, are you sure that's what you want?,

Yes, I think so. I cannot remember that I really need a tab in any programming language I have used. If I need a tab in my strings I can simply do a "\t". Do you know of any circumstance you actually _need_ a tab?

Sascha

In article <4240CD36.1010604@neurogami.com>,

···

James Britt <jamesUNDERBARb@neurogami.com> wrote:

Phil Tomson wrote:

The lucky thing for us Ruby users is that the number of spaces/tabs (while
the two might look the same on the screen) doesn't matter to the
interpreter as it does with another language that starts with a P.

Does mixing tabs & spaces munge YAML text?

Ah, yes. This can cause problems with YAML...

Phil

Btw, doesn't anyone know of a good ruby code indenter proggie?

That would also relax the headache of those who want to include the code
in books or webpages.

Csaba

···

On 2005-03-23, Jim Freeze <jim@freeze.org> wrote:

* Nikolai Weibull <mailing-lists.ruby-talk@rawuncut.elitemail.org> [2005-03-23 09:11:59 +0900]:
Boy, I don't know where all this came from, I thought the majority
were all on the same page, but maybe it is because of
all the new people. Maybe we should have a coding standard
linked from ruby-lang.

* James Britt <jamesUNDERBARb@neurogami.com> [2005-03-23 10:56:55 +0900]:

Phil Tomson wrote:
>The lucky thing for us Ruby users is that the number of spaces/tabs (while
>the two might look the same on the screen) doesn't matter to the
>interpreter as it does with another language that starts with a P.

Does mixing tabs & spaces munge YAML text?

YAML does not support tabs for indention.
That is something _why gave much consideration too.

···

--
Jim Freeze
Code Red. Code Ruby

I don't always use 80 characters. Most of the time? Yes. Other
times? No. Some of it is because of the vagaries of the Ruby
interpreter -- there are times when it is less readable to have the
line extend beyond the 80-character "limit", or it is simply not
possible (e.g., certain "raise Blah unless ..." type conditions).

I simply do what's pragmatic.

-austin

···

On Wed, 23 Mar 2005 11:44:46 +0900, Travis Smith <zultan.durin@gmail.com> wrote:

On Wed, 23 Mar 2005 09:11:59 +0900, Nikolai Weibull > <mailing-lists.ruby-talk@rawuncut.elitemail.org > wrote:

* James Edward Gray II (Mar 22, 2005 23:50):
Seriously, though, the 80 character boundary is a thing of the
past. Terminals (not to mention emulators) can be made wider, 132
being a good "standard" setting, so it's not even about backwards
compatibility anymore. I find that I can write code the way I
want it if I'm not constrained to 80 characters per line. (I only
switched to a 132-character-wide terminal about 3 months ago,
though, so I may change my mind yet again.),

I disagree. I use 80 character width for everything. I have three
xterms spanned across my screen - all 80 characters wide. My IDEs
have the size of the window limited to 80 characters wide. It's
the perfect size. If a line goes too far then I know it's too
complicated (expect strings, string often span lines) or I'm
writing in Java. I think Java kills the 80 characters wides...
That's another story though.

I think everyone who doesn't use 80 characters (or less) should be
shot. But just in the foot, so they'll learn the error of their
ways. :slight_smile:

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

* Ben Giddings (Mar 23, 2005 01:10):

> It's very easy to do with vim. Simply leave your 'ts' alone, set
> 'sw' to 8, don't set 'sts' and don't set 'et'. Those are all
> default settings by the way.

And people say vi isn't intuitive!

well, I could have spelled them out, but that wouldn't have changed
much ;-). (The full command is
  :set tabstop& shiftwidth& softtabstop& expandtab&
by the way),
        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);}

I believe this is one of the design goals of the new Facets of Ruby series.

James Edward Gray II

···

On Mar 22, 2005, at 7:07 PM, Nikolai Weibull wrote:

How often is this the case? I have yet to see a book displaying
snippets of code from actual software beyond showing of horrible coding
practices (perhaps a good reason to stay within the 80-characters-per-
line limit, as you don't want to end up in one of those books).

I have, and later in the day they come back and bite me in the butt
because something else it's formatted to the wider size.

···

On Wed, 23 Mar 2005 10:07:22 +0900, Nikolai Weibull <mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:

* James Edward Gray II (Mar 23, 2005 01:40):
Be kind to yourself: expand your horizon,

--
~Travis

Write it up and send it in to Ruby Quiz. You know I'm a sure thing for running that one! :wink:

James Edward Gray II

···

On Mar 22, 2005, at 9:54 PM, Csaba Henk wrote:

Btw, doesn't anyone know of a good ruby code indenter proggie?

That would also relax the headache of those who want to include the code
in books or webpages.

Yes, but they were based on clay tablets iirc.

ps: Arfin, if you're still reading this... Welcome to Ruby :slight_smile:

without a doubt the strangest thread I've seen triggered by a new user question.

···

On Wed, 23 Mar 2005 10:34:50 +0900, Timothy Hunter <cyclists@nc.rr.com> wrote:

James Edward Gray II wrote:
> To summarize, it isn't just about terminals. Be kind to your email
> reader, webmaster, and author. Stick with 80.

The 80 char limit never was about terminals. It was about punched cards.

--
Bill Guindon (aka aGorilla)