# Why not adopt "Python Style" indentation for Ruby?

**URL:** <https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458>\
**Category:** Ruby\
**Created:** [May 18, 2007, 9:26am UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458 "2007-05-18T09:26:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 9:26am UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/1 "2007-05-18T09:26:28Z")

</div>

As far as I can see, the ‘end’ keyword is ‘repeating yourself’ when  
used with properly indented code.

class Foo

def bar(value)  
if value \< 7  
puts “hello”  
else  
puts “world”  
end  
end

def foobar  
puts “foobar”  
end

end

could be

class Foo

def bar(value)  
if value \< 7  
puts “hello”  
else  
puts “world”

def foobar  
puts “foobar”

with no reduction in meaning, yet 25% fewer lines of code.

What are the reasons why this isn’t used/implemented/liked? It would  
be a small change to the interpreter. Enabling meaningful indentation  
would only make ‘end’ optional, not invalid; backwards compatibility  
wouldn’t be a problem.

(I use both Ruby and Python. I think indentation is one of the few  
_language_ features where Python leads Ruby.)

If this post generates a positive response, I’ll make a patch for Ruby  
1.9.

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 9:38am UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/2 "2007-05-18T09:38:29Z")

</div>

Hi,

In message “Re: Why not adopt “Python Style” indentation for Ruby?”  
on Fri, 18 May 2007 16:25:03 +0900, Chris D.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|What are the reasons why this isn’t used/implemented/liked?

Python style block by indentation is an interesting idea, but it works  
badly with

- tab/space mixture
- templates, e.g. eRuby
- expression with code chunk, e.g lambdas and blocks

So I’d rather be conservative here.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 11:05am UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/3 "2007-05-18T11:05:50Z")

</div>

On 18/05/07, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> - tab/space mixture
> - templates, e.g. eRuby
> - expression with code chunk, e.g lambdas and blocks

- clipboards, email, pastebins, and other places where the code is not  
preserved literally

Thanks

Michal

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 11:07am UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/4 "2007-05-18T11:07:50Z")

</div>

Actually,

Isn’t end repeating yourself full stop?

Do we really need it?

Julian.

On Fri, 18 May 2007 16:25:03 +0900

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 12:50pm UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/5 "2007-05-18T12:50:59Z")

</div>

- tab/space mixture
- templates, e.g. eRuby
- expression with code chunk, e.g lambdas and blocks
- clipboards, email, pastebins, and other places where the code is not  
preserved literally
- code generators

tabs can be made forbidden (g.v.rossum himself said he’d never  
supported tabs in the first place if he new what it would cauze)

maybe it can be made optional. or one could write a little conversion  
script.

i agree python’s indentation looks clean. yet i have experienced  
allmost all of the above mentioned “cons”…

\_c.

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 11:09am UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/6 "2007-05-18T11:09:51Z")

</div>

On May 18, 2007, at 11:04 AM, Michal S. wrote:

> > works  
> > badly with
> > 
> > - tab/space mixture
> > - templates, e.g. eRuby
> > - expression with code chunk, e.g lambdas and blocks
> 
> - clipboards, email, pastebins, and other places where the code is not  
> preserved literally

code generators

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 12:57pm UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/7 "2007-05-18T12:57:00Z")

</div>

On May 18, 2007, at 12:50 PM, cies wrote:

> maybe it can be made optional. or one could write a little  
> conversion script.

Or add source filters:

[Acme::Pythonic - Python whitespace conventions for Perl - metacpan.org](http://search.cpan.org/~fxn/Acme-Pythonic-0.45/lib/Acme/Pythonic.pm)

MWUUAHAHAHA.

– fxn

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 2:31pm UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/8 "2007-05-18T14:31:14Z")

</div>

On Fri, May 18, 2007 at 06:09:23PM +0900, Xavier N. wrote:

> > preserved literally
> 
> code generators

Disabling sections of code, i.e.

if false  
…  
… code with original indentation  
…  
end

Also, multi-level namespaces where you don’t want to indent the entire  
source file that much, e.g.

module Foo  
module Bar  
class Baz  
def wibble  
…  
end  
end # Baz  
end # Bar  
end # Foo

(Admittedly, I believe it’s now possible to write

module Foo; module Bar; end; end  
class Foo::Bar::Baz  
…  
end)

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 3:37pm UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/9 "2007-05-18T15:37:25Z")

</div>

Hi,

In message “Re: Why not adopt “Python Style” indentation for Ruby?”  
on Fri, 18 May 2007 22:01:13 +0900, Daniel M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|Note that the Haskell folks have managed to evolve a language in which  
|python-like spacing can be used to mark the extent of discrete code  
|chunks, but so can traditional braces.

I admit Haskell syntax is much better than Python’s. But I am not yet  
sure if it can be applied to Ruby.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 3:02pm UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/10 "2007-05-18T15:02:18Z")

</div>

Chris D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> As far as I can see, the ‘end’ keyword is ‘repeating yourself’ when  
> used with properly indented code.

Note that the Haskell folks have managed to evolve a language in which  
python-like spacing can be used to mark the extent of discrete code  
chunks, but so can traditional braces.

This is more then kind of thing I’d like to see in Ruby: having it  
allow the compact python notation, but not require it. (So that,  
e.g., code generators would still be easy to write)

I think anyone serious about adding indentation-as-syntax to Ruby  
should take a very close look at how Haskell manages to allow it  
without requiring it.

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 4:04pm UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/11 "2007-05-18T16:04:28Z")

</div>

On 5/18/07, Chris D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What are the reasons why this isn’t used/implemented/liked? It would  
> be a small change to the interpreter. Enabling meaningful indentation  
> would only make ‘end’ optional, not invalid; backwards compatibility  
> wouldn’t be a problem.
> 
> (I use both Ruby and Python. I think indentation is one of the few  
> _language_ features where Python leads Ruby.)

This is a very subjective thing. A lot of people don’t use Python  
because they don’t like this feature.

In my case I like the symmetry that Ruby’s matching end provides. I  
find it makes the code easier to read and maintain. I’ve written and  
maintained some Python code that “trails off into nowhere” with a long  
series of indentations. I find this “stairway” code rather painful to  
work with.

But if it is possible to add this feature to Ruby without breaking  
anything, it could be interesting. It would certainly draw a few  
Pythonists to check out Ruby.

Ryan

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/12 "2007-05-18T16:16:29Z")

</div>

“Yukihiro M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> - tab/space mixture
> - templates, e.g. eRuby
> - expression with code chunk, e.g lambdas and blocks
> 
> So I’d rather be conservative here.
> 
> matz.

imho: indentation should not define semantic;  
suggestion: make ‘end’ optional if code block contains only one  
statement;

BRs  
Sergey V.

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 4:00pm UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/13 "2007-05-18T16:00:28Z")

</div>

On 18.05.2007 15:36, Yukihiro M. wrote:

> sure if it can be applied to Ruby.  
> Just to throw in my 0.02EUR here: I don’t think it’s worth the effort  
> (which I guess is huge) because Ruby’s syntax _is_ concise already. If  
> I got to decide I would rather see those efforts go into JVM based Ruby  
> implementations and / or native threads. But that’s just my personal  
> preference.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 4:26pm UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/14 "2007-05-18T16:26:30Z")

</div>

Hi,

In message “Re: Why not adopt “Python Style” indentation for Ruby?”  
on Fri, 18 May 2007 23:15:05 +0900, “S.Volkov” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|imho: indentation should not define semantic;

Agreed.

|suggestion: make ‘end’ optional if code block contains only one statement;

I’m not sure if we can define syntax for above suggestion.

while cond  
puts a  
puts b # delimit by indentation or else??

or maybe

while cond: puts a # should be in same line

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/15 "2007-05-18T17:20:37Z")

</div>

On 5/18/07, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> sure if it can be applied to Ruby.
> 
> ```
> matz.
> 
> ```

I’ve picked up on python in the last couple months and like the  
indentation. In addition to forced readablity, I find another  
practical benefit is that it eliminates hard to track down syntax  
errors due to matching {}, begin/end, etc. In ruby, C, perl, etc  
these errors can be painful to track down in a large file with lots of  
blocks within blocks.

Syntactically it isn’t too difficult once you have the lexer generate  
indent and unindent tokens. Then the parser just looks for matching  
indent and unindent tokens instead of {} or begin/end, etc.

You might consider something like this as an alternative block syntax.  
Maybe a “:” followed by a newline (and extra indentation) would start  
this style. I’m not sure of a clean way to handle arguments though.

p.s. in the late eighties I made a little shell language (for the  
Atari ST) that used indentation just like python.

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 5:22pm UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/16 "2007-05-18T17:22:38Z")

</div>

Dnia Fri, 18 May 2007 22:36:58 +0900, Yukihiro M. napisa³(a):

> I admit Haskell syntax is much better than Python’s.

Matz, you must be joking… Python is much easier to read than Haskell.

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 5:15pm UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/17 "2007-05-18T17:15:36Z")

</div>

Dnia Fri, 18 May 2007 16:38:05 +0900, Yukihiro M. napisa³(a):

> Python style block by indentation is an interesting idea, but it works  
> badly with
> 
> - tab/space mixture

Tabs are against PEP8 which recomends space usage only.

> - templates, e.g. eRuby

eRuby is crap if you compare it with Haml template  
[http://haml.hamptoncatlin.com/](http://haml.hamptoncatlin.com/).

It is interesting how they use indendation and how clean it can be. And  
finally… they are written in Ruby and for Ruby users! 🙂

What do you think about OPTIONAL block indendation in Ruby?

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/18 "2007-05-18T17:40:41Z")

</div>

On 5/18/07, Eric M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve picked up on python in the last couple months and like the  
> indentation. In addition to forced readablity, I find another  
> practical benefit is that it eliminates hard to track down syntax  
> errors due to matching {}, begin/end, etc. In ruby, C, perl, etc  
> these errors can be painful to track down in a large file with lots of  
> blocks within blocks.

You could always run it through a tool to clean up code and provide  
proper indentation. I personally don’t like it when a language  
dictates how my code ought to look, since I’m pretty picky myself :s

> Syntactically it isn’t too difficult once you have the lexer generate  
> indent and unindent tokens. Then the parser just looks for matching  
> indent and unindent tokens instead of {} or begin/end, etc.

When working in a team, regardless of how the code has been formatted,  
I’ve always been able to slap pieces of code together and not worry  
about indentation (when such language doesn’t worry about  
indentation). Working in python always adds a little extra stress  
when mixing and matching code, and the problem gets bigger when  
indentation isn’t standardized across teams or when applying code from  
the web.

filipe

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/19 "2007-05-18T17:26:40Z")

</div>

On 5/18/07, Jaroslaw Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > - templates, e.g. eRuby
> 
> eRuby is crap if you compare it with Haml template  
> [http://haml.hamptoncatlin.com/](http://haml.hamptoncatlin.com/).

Except eRuby is a general templating system, and Haml is for XHTML  
generation.

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [May 18, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458/20 "2007-05-18T17:46:42Z")

</div>

In message [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid), Yukihiro  
Matsumoto writes:

> |suggestion: make ‘end’ optional if code block contains only one statement;

> I’m not sure if we can define syntax for above suggestion.

I am pretty sure we can’t, unless we acquire a machine sufficiently  
quantum to let us simply generate multiple outputs for each input  
program. That would, I think, resolve the question. Sort of.

-s

[Next page](https://www.ruby-forum.com/t/why-not-adopt-python-style-indentation-for-ruby/104458.md?page=2)
