Why not adopt "Python Style" indentation for Ruby?

F

Frasier Mruby

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".
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.
 
B

Brian Candler

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 :)

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 :)
 
M

Mark Thomas

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. :)

-- Mark.
 
J

James Britt

Brian said:
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 :)

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 Britt

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
 
J

Justin Collins

Frasier said:
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 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.

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
 
T

Tom Reilly

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
 
D

David A. Black

Hi --

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 :)


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 
F

Frasier Mruby

Mark said:
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. :)

-- 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.
 
M

Martin DeMello

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
 
F

Frasier Mruby

Justin said:
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.
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.
 
H

Hassan Schroeder

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? <return>
| # cursor placed here
end # auto-inserted

FWIW,
 
B

Bilyk, Alex

+1=20

I always type (), [], {}, do/end, begin/end,<>, etc. upfront together and t=
hen insert what goes in between. I can't quite remember the last time I hav=
e 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 beca=
use I have not used it long enough.

Alex

-----Original Message-----
From: Justin Collins [mailto:[email protected]]=20
Sent: Monday, October 06, 2008 3:55 PM
To: ruby-talk ML
Subject: Re: Why not adopt "Python Style" indentation for Ruby?

Frasier said:
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 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.

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
 
J

Justin Stanczak

[Note: parts of this message were removed to make it a legal post.]

+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.

+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

-----Original Message-----
From: Justin Collins [mailto:[email protected]]
Sent: Monday, October 06, 2008 3:55 PM
To: ruby-talk ML
Subject: Re: Why not adopt "Python Style" indentation for Ruby?

Frasier said:
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 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.

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
 
S

Stefano Crocco

Alle Tuesday 07 October 2008, Tom Reilly 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
 
J

Justin Collins

David said:
Hi --



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


David

Yeah :( Although sometimes I miss the simplicity of ON KEY GOSUB...

-Justin
 
N

Nobuyoshi Nakada

Hi,

At Tue, 7 Oct 2008 04:57:45 +0900,
Brian Candler wrote in [ruby-talk:317027]:
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)

Python style indentation also can't help you alone. How can it
know which did you intend?

if x == "ok":
print "ok"
print "foo"

if x == "ok":
print "ok"
print "foo"

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 :)

Recent 1.9 warns when it see a mismatch.

$ ./ruby -ve 'begin
end'
ruby 1.9.0 (2008-10-07 revision 19704) [i686-linux]
/ruby: warning: mismatched indentations: line 1:'begin' and line 2:'end'
 
G

Gaspard Bucher

[Note: parts of this message were removed to make it a legal post.]

I code in ruby and c++.

I love {} in c++.

I love "do", "end" in ruby.

That's just because I am deeply grateful for what both these languages
let me achieve, each in it's own field.

Gaspard
 
D

David A. Black

Hi --

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.

That doesn't follow.
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.

Yes, to a large extent. But Ruby also *is* a language. It's not an
experiment in how to stick different ideas from other languages
together.

Have you written to any C mailing lists, suggesting that C adopt
features from Ruby?
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.

It's been discussed a million times over many years, if that's what
you mean. If Matz were going to remove the "end" keyword, I think he
would have done it by now.
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.

Please don't put this in terms of Ruby attracting programmers. Ruby
does about as good a job as any language I've ever heard of, when it
comes to having programmers like it. Not everyone does, of course, but
Matz has accomplished something truly great and amazing in exactly
that sense. "end" is not a test of whether or not Ruby is an
attractive language, and curly braces are not Ruby's ticket to
popularity.

In general, the best thing by far is to spend some time with the
language, and let it speak to you.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top