Why not adopt "Python Style" indentation for Ruby?

Dear Mats,

I am kind of new to ruby and like it after looking python first. As
some other people said, my only major complaint is it’s many “END”.
Among java/c/C++/python, the “END” keep reminding me the inconvenience
of it during coding ruby. I like ruby because it’s easy to learn, and
to read. It’s designed for be friends of programmers. But I feel the
“END” may have a negative role to ruby’s purpose or attraction.

I am fine to have “END” for class or methods. But for IF,WHILE, CASE,
FOR, etc., when there are many levels it often make me confused what the
matching part of the those "END"s are.

I understand and agree your comment that ruby had better to have
something to close the code block. But I sincerely hope you could come
up something else to replace the “END”.

My first thought to use brace “{…}” to replace “END” since it’s a
popular convention.

I suggest to use only the “do…end” to formalize the blocks, brace {}
will be stopped to be used. It seems to me it’s a waste of symbols to
have two ways to represent blocks, which may not be the most frequently
used. Or consider to use use:
“/* */”, “|…|”, “<…>”, “[…]” , “(…)”, “((…))”, , “//…//”, "
:…: ", " ‘…’ ", " ... " for blocks.

To enhance readability is probably one of ruby’s design purpose and I
really hope some thing could be done earlier to make the “END” looks
prettier.

Thank you!

On 18/05/07, Yukihiro M. [email protected] 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

On 6/1/07, Keith F. [email protected] wrote:

Actually, mate-highlighting was added in vim 7:
Vim documentation: pi_paren

Right, which is what I use. And as I said, it only highlights pairs
of single character delimiters which are unequal, it can’t highlight
do/end or “/”.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Frasier M. wrote:

Why not adopt “Python Style” indentation for Ruby? Because that would
bite a big one.

I am fine to have “END” for class or methods. But for IF,WHILE, CASE,
FOR, etc., when there are many levels it often make me confused what the
matching part of the those "END"s are.

Then don’t do that. Seriously. Twisty conditionals is a code smell.
(Having fought with deep hierarchies of YAML and HAML, the lack of an
explicit “end” token doesn’t really help in complex documents; in fact,
it can be detriment. Sometimes explicit is better than implicit.)

BTW, doesn’t your code editor find the matching braces (or do/ends) for
you? (If not, get a real editor. They’re free.)

To enhance readability is probably one of ruby’s design purpose and I
really hope some thing could be done earlier to make the “END” looks
prettier.

I rarely run into Ruby code that is not readable. When I do, it’s
almost always for a reason (e.g. Camping source code, or some code
golfing).

The idea that coders need to be coerced into writing readable code is as
flawed as that of the need for Java-style static typing to avoid bugs.

It sounds good in blog posts and mailing lists, but lacks empirical
evidence.


James B.

www.happycamperstudios.com - Wicked Cool Coding
www.jamesbritt.com - Playing with Better Toys
www.ruby-doc.org - Ruby Help & Documentation
www.rubystuff.com - The Ruby Store for Ruby Stuff

Just my quick chip-in:

I find Ruby’s syntax as-is works remarkably well. The only problem it
causes me is with do-end mismatches where the parser just runs off the
end of the file, with no indication of where the missing do or end might
be. I have on many occasions resorted to chopping a copy of my source
file to find the problem (that is, lopping out a class or a method at a
time until the problem goes away)

DSL’s are also a source of this problem:

context “an empty object” do
should “have zero size” do

end
end

Miss out either of those 'do’s and it’s hard to tell where the problem
is.

Personally, the solution I would love to have is a parser/pretty-printer
tool which can read a Ruby source file and spit it back out with
“standardised” indentation. It would then be obvious where the problem
lies. You’d get the benefit of python-style indentation, without
actually having to use python-style indentation :slight_smile:

Unfortunately, the lack of specification makes it really hard to
implement; and I don’t think I could use something like ParseTree as I
think it would rely on the file being parsed successfully up-front,
whereas I want this for programs which don’t parse. Maybe there’s
another ruby implementation which could do this. Otherwise, the best I
can think of is to modify the existing Ruby parser to spit out
auto-indented code as it reads the source.

I don’t wish to change to a different text editor, and in any case, I
don’t believe that any editor has a sufficiently complete knowledge of
Ruby lexing and parsing rules that it will always match the Ruby way of
parsing source.

Anyway, if anyone knows of a tool which does this, it would make me very
happy :slight_smile:

Ruby code is reasonable or readable with “END”. And I agree a better
editor would help to find matching END, but still wonder if there could
be room to make the ruby code cleaner by considering other syntax to
replace “END”.

After you use ruby for a while, you’ll stop wanting to make it look
like some other language. You’ll realize it’s nice the way it is. And
certain things will start to grow on you, such as using @x instead of
self.__x.

I also like the idea that “END” is optional for one line if, like:

if a > 0 :
print a

If that’s a one-line ‘if’, what’s this?

print a if a > 0

even better, that’s what. :slight_smile:

– Mark.

On 6 oct. 08, at 15:57, Brian C. wrote:

Personally, the solution I would love to have is a parser/pretty-
printer
tool which can read a Ruby source file and spit it back out with
“standardised” indentation. It would then be obvious where the problem
lies. You’d get the benefit of python-style indentation, without
actually having to use python-style indentation :slight_smile:

Maybe this is close to what you want.
http://blog.neontology.com/posts/2006/05/10/beautiful-ruby-in-textmate

James, thank you for you fast response.

I didn’t propose to get rid of the “END” like python does, but wondered
if there could be cleaner syntax to as an alternative for “END”.

James B. wrote:

I am fine to have “END” for class or methods. But for IF,WHILE, CASE,
FOR, etc., when there are many levels it often make me confused what the
matching part of the those "END"s are.

Then don’t do that. Seriously. Twisty conditionals is a code smell.
(Having fought with deep hierarchies of YAML and HAML, the lack of an
explicit “end” token doesn’t really help in complex documents; in fact,
it can be detriment. Sometimes explicit is better than implicit.)

Ruby code is reasonable or readable with “END”. And I agree a better
editor would help to find matching END, but still wonder if there could
be room to make the ruby code cleaner by considering other syntax to
replace “END”.

What I was saying in previous post is to free up the braces “{}” out of
block syntax to replace “END”. For blocks, besides “do…end”, use
something else like "[], or /**/. e.g.:

class Foo
def bar(b)
c = b+1
if b is not nil
if c > 10
c++
if c %2 == 0
c += 3
print c
end
end

    if d < 9
       d += 2
       print d
    end
  end

  a = [1, 2]
  a.each {|num| print num }

end

end

tries to clean up the "END"s

class Foo
def bar(b) {
c = b+1
if b is not nil {
if c > 10 {
c++
if c %2 == 0 {
c += 3
print c
}
}

    if d < 9 {
       d += 2
       print d
    }
  }

  a = [1, 2]
  a.each [|num| print num ]
  a.each (|num| print num )
  a.each < |num| print num >
  a.each /* |num| print num */
  a.each /# |num| print num #/
  ...
}

end

If {} can represent a bock, then personally it might also readable to
use [] and syntax above to denote blocks.

I also like the idea that “END” is optional for one line if, like:

if a > 0 :
print a

I can live with “END” in ruby, now just want to express an opinion or
wish to make the ruby code more cleaner, and more programmer attracted.

Brian C. wrote:

I don’t wish to change to a different text editor, and in any case, I
don’t believe that any editor has a sufficiently complete knowledge of
Ruby lexing and parsing rules that it will always match the Ruby way of
parsing source.

Anyway, if anyone knows of a tool which does this, it would make me very
happy :slight_smile:

Well, vi/vim/gvim users can have the code auto-format, which will “fix”
indentation.

When I get those nasty kEND errors I run the auto-format macro and scan
down the code until I see something out of place. Then error is usually
a few lines above that point.


James B.

www.happycamperstudios.com - Wicked Cool Coding
www.jamesbritt.com - Playing with Better Toys
www.ruby-doc.org - Ruby Help & Documentation
www.rubystuff.com - The Ruby Store for Ruby Stuff

Frasier M. wrote:

Dear Mats,

I am kind of new to ruby and like it after looking python first. As
some other people said, my only major complaint is it’s many “END”.
Among java/c/C++/python, the “END” keep reminding me the inconvenience
of it during coding ruby. I like ruby because it’s easy to learn, and
to read. It’s designed for be friends of programmers. But I feel the
“END” may have a negative role to ruby’s purpose or attraction.

Like any new language, it takes some time to adjust your eyes and
fingers.

I am fine to have “END” for class or methods. But for IF,WHILE, CASE,
FOR, etc., when there are many levels it often make me confused what the
matching part of the those "END"s are.

None of your suggestions seem to address this issue. Matching ‘end’ is
no harder than matching ‘}’. If you get confused, add documentation like
some people do:

end#if
end#while
end#class

I understand and agree your comment that ruby had better to have
something to close the code block. But I sincerely hope you could come
up something else to replace the “END”.

My first thought to use brace “{…}” to replace “END” since it’s a
popular convention.

But then it looks like C/Java.

I think ‘end’ looks fine, personally. I used to use curly braces all the
time, but now I find myself using them as little as possible. do/end
seems nicer and I don’t get them confused with hashes that way.

Besides, I enjoy the current look for conditionals. Given your later
example, I prefer to see

if b is not nil

def bar b

than

if b is not nil {

def bar b {

Especially if it’s going to lead to arguments about where that ‘{’
should go. Same line? Next line? Next line indented? I don’t want C. I
don’t want Java. I don’t want parentheses around my conditionals when
they aren’t needed. I don’t want to have to use my shift key more than
necessary. I really don’t see the problem with the current syntax. I
respect your opinion but definitely do not agree.

-Justin

Mark T. wrote:

After you use ruby for a while, you’ll stop wanting to make it look
like some other language. You’ll realize it’s nice the way it is. And
certain things will start to grow on you, such as using @x instead of
self.__x.

Ok. I will try to use ruby more then. I like ruby’s other syntax such
as @x than self.__x. Only complaint is the “END”, which I have to type
even for a couple of short lines of IF.

If that’s a one-line ‘if’, what’s this?

print a if a > 0

even better, that’s what. :slight_smile:

– Mark.

Yeah, that’s one line if but it seems Mats mentioned one syntax before
like : if a > 0 then do this . Sth like that. When if clause is
longer the existing one-line if may not looks elegant. Thanks.

I’ve thought that one rather likes the structure of a language learned
first.

I learned Ruby first then Python.

I never liked the indentation of Python probably as a result

Tom

On 6 oct. 08, at 16:17, Joe Wölfel wrote:

Maybe this is close to what you want.
http://blog.neontology.com/posts/2006/05/10/beautiful-ruby-in-textmate

Sorry, here’s a better link.

On Mon, Oct 6, 2008 at 5:46 PM, Tom R. [email protected]
wrote:

I’ve thought that one rather likes the structure of a language learned
first.

I learned Ruby first then Python.

I never liked the indentation of Python probably as a result

Actually, the more I write in scheme, the more I like its structure.
It all just works.

martin

Justin C. wrote:

None of your suggestions seem to address this issue. Matching ‘end’ is
no harder than matching ‘}’. If you get confused, add documentation like
some people do:

end#if
end#while
end#class

It’s a good way. It’s just that there’re more typings. If you says
"matching end is no harder than }, then } could be an considered as an
option I assume.

My first thought to use brace “{…}” to replace “END” since it’s a
popular convention.

But then it looks like C/Java.

Liking other languages may not be bad. Ruby’s syntax on my first
impression is that it’s similar to python, thinking about the “class” or
“def”. Ruby has evolved by learning merits from other languages.

I think ‘end’ looks fine, personally. I used to use curly braces all the
time, but now I find myself using them as little as possible. do/end
seems nicer and I don’t get them confused with hashes that way.

Thanks for your reply, Justin. It seems I am not the only one who are
concerned with the “END”. Not 2 years ago, 1 year ago, or now. On
ruby’s logo, it says “programmer’s best friend”. It seems ruby’s
objective is to make it programmer-friendly language. When there are
not few people have been wondering if any improvement could be made on
its syntax, some effort may need to be considered on the improvement.
If it’s determined there’s absolutely no room to improve, then that’s
fine.

If no improvement will be made in near future, I can live with “END”,
though in my mind I may still have the thought of it and wondering if it
could be made better.

Don’t get my wrong. I hope ruby getting better and can attract more
programmers. With this mind, I pointed what I felt, which is shared by
other new comers.

On Mon, Oct 6, 2008 at 6:18 PM, Frasier M. [email protected]
wrote:

Only complaint is the “END”, which I have to type
even for a couple of short lines of IF.

If you use an IDE/editor like NetBeans, you don’t – it’s inserted for
you
when you enter something like

if @foo.nil?
| # cursor placed here
end # auto-inserted

FWIW,

Hi –

On Tue, 7 Oct 2008, Tom R. wrote:

I’ve thought that one rather likes the structure of a language learned first.

I like the structure of Ruby more than that of BASIC, so there goes
that theory :slight_smile:

David

+1

I always type (), [], {}, do/end, begin/end,<>, etc. upfront together
and then insert what goes in between. I can’t quite remember the last
time I have mismatched these in any language.

OTOH, I can’t stand Python’s semantically significant white space
approach. It seems to give me trouble a lot more often than I’d like…
perhaps because I have not used it long enough.

Alex

+1

I’ve used Java for years, and it’s great, but I would not want to make
Ruby
more like Java. I’m new to Ruby, but really enjoy it so far. As for
Python,
I just can’t get my self into it. I attended one of Mark Lutz’s training
sessions, just to find I didn’t care for coding Python. I’m sure it’s a
great language, but I guess it’s not for me. Mark did a great job. I
find
Ruby very easy to use, and the syntax well thought out. Don’t change a
thing, at least not until I’ve learned it better.

Alle Tuesday 07 October 2008, Tom R. ha scritto:

I’ve thought that one rather likes the structure of a language learned
first.

I learned Ruby first then Python.

I never liked the indentation of Python probably as a result

Tom

I learned Python first, hated its indentation-based structure, switched
to
Ruby and loved it.

Stefano

David A. Black wrote:

David

Yeah :frowning: Although sometimes I miss the simplicity of ON KEY GOSUB…

-Justin