Goto function?

Simen E. schrieb:

There’s no way to do:

goto :a
label :a

Right, but with some evil Ruby code, this is possible:

irb(main):001:0> require “goto”
=> true
irb(main):002:0> with_goto do
irb(main):003:1*
irb(main):004:1* puts 1
irb(main):005:1> goto :b
irb(main):006:1>
irb(main):007:1* label :a
irb(main):008:1> puts 3
irb(main):009:1> goto :c
irb(main):010:1>
irb(main):011:1* label :b
irb(main):012:1> puts 2
irb(main):013:1> goto :a
irb(main):014:1>
irb(main):015:1* label :c
irb(main):016:1> puts 4
irb(main):017:1>
irb(main):018:1* end
1
2
3
4
=> nil
irb(main):019:0>

No, I won’t show the implementation yet. See
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/202387

Regards,
Pit

On Wed, 16 Aug 2006 13:55:01 +0200, Christian N.
[email protected] wrote:

It is often said that continuations are as powerful as goto.
But is there a way to jump forward with continuations, without
rewriting the code to use additional methods?

As others said, continuations continue. You can jump backwards, because
you know the whole context to jump into - it already existed.

To jump forward in code without explicitly setting up a context in which
to run would either result in undefined behaviour, accepting some
defaults
for the context (all local / instance / other nonglobal variables are
nil,
for example), or the timespace continuum breaking. That might not apply
if
you were scripting a time machine, of course.

This is precisely why goto is considered harmful in the first place - to
jump forward, you have to use only global context, or some other context
that is defined to be shared when using such a construct. It would be
interesting however if there was a concept of capture a (possible)
future
point of code execution without having to do explicit setup via a
command
object or something similar. I’ll admit at this point that my savvy gets
a
little fuzzy around very high level programming theory - so I might
actually be honking wrong in which case I’m all ears intrigued as to the
workings of that construct.

David V.

Ah very clever. Corrupting the minds of the innocent by introducing
bad programming practices to the language.

This reminds me of a conversation I had with one of my junior
programmers on a project back in the 80’s. He argued that since the C
language specification included the GOTO statement it was perfectly
fine to use it. I pointed out that my Jeep had a roll bar, but that
didn’t mean I should use it!

On Tue, Oct 9, 2012 at 4:45 PM, Frédéric Lemire [email protected]
wrote:

I like cleanliness and modularization, but I also like to have the eval
command, and metaprogramming stuff to explore sometimes unexpected ways.
So, why not a goto command ?

It’s neat to be able to get dirty when one needs it and feels confident
about it !

Anyway… for now, I’ll just make exceptions to my indentation fixation,
waiting for a freer future.

You can use continuations (although I think I recall hearing that they
were either removed or dumbed down in 1.9).

My friends ! Let’s not become dogmatic, please !!! …

I have nothing against standards in coding and all, but please, let us
people write dirty code sometimes ! It can be so handy !

I like well indented code, and pushing a whole lot of code I want to
make optional one indentation further is a pain in the back. There are
many ways to program well, and there is also idiosyncratic or
exceptional programming that may be way clearer with a blatant GOTO.

Why not ? I like it when a language gives me tools, handy tools, logical
ones, for particular situations, like, imagine, you just want to have
the data in a proper format and start doing nifty stuff with it right
away. Who cares if the data-load part is not yet modularized, and all,
and if there are some orange cones around your hefty chunk of code with
multiple passes and all — with DEVIATION signs flashing in front it ?
It’s surely temporary, so, let’s relaaax.

And… what if it wasn’t temporary ? I agree that using it too much
(especially backwards) is a bad habit, but in some situations, why not ?
Like, we have a break instruction that is very handy to short-circuit
the code and jump above it.

I like cleanliness and modularization, but I also like to have the eval
command, and metaprogramming stuff to explore sometimes unexpected ways.
So, why not a goto command ?

It’s neat to be able to get dirty when one needs it and feels confident
about it !

Anyway… for now, I’ll just make exceptions to my indentation fixation,
waiting for a freer future.

Ciao !

You can compile Ruby with SUPPORT_JOKE to enable goto.

http://patshaughnessy.net/2012/2/29/the-joke-is-on-us-how-ruby-1-9-supports-the-goto-statement

– Matma R.

It’s a question of structure and proper planning more than any type of
dogmatic fanboyism. Goto is not good practice. We have loops and
recursion
for such things, and if you admit it’s dirty then why in the world are
you
using it? This is more of an appeal to logic and sanity than anything.

Eric C. писал 10.10.2012 02:12:

confident
about it !

Anyway… for now, I’ll just make exceptions to my indentation
fixation,
waiting for a freer future.

You can use continuations (although I think I recall hearing that
they
were either removed or dumbed down in 1.9).

They were, in fact, added in 1.9.

On Wed, Oct 10, 2012 at 5:05 AM, Peter Z. [email protected]
wrote:

It’s neat to be able to get dirty when one needs it and feels confident
They were, in fact, added in 1.9.
Oh, interesting.

To the OP: It’s usually advised to break a method into several, if you
find that it gets too long or has too many levels of indentation.

On Oct 10, 2012, at 06:05 , Peter Z. [email protected]
wrote:

Anyway… for now, I’ll just make exceptions to my indentation fixation,
waiting for a freer future.

You can use continuations (although I think I recall hearing that they
were either removed or dumbed down in 1.9).

They were, in fact, added in 1.9.

Neither, actually – callcc is present in both 1.8.7 and 1.9, but was
moved from the core to a stdlib in 1.9 (the ‘continuation’ module).

However, callcc is not guaranteed to be present in all implementations
(and is in fact not present in JRuby and MacRuby).

I use callcc as the basis for the (mostly humorous/proof-of-concept)
“reconsidered” gem, which – to the original poster’s question – adds a
limited form of goto to ruby, and for the more serious ambit gem, which
provides nondeterministic programming for Ruby, and is the basis for the
in-progress rulog logic programming system for Ruby.

See:

jimwise (Jim Wise) · GitHub

for more on reconsidered, ambit, and rulog. :slight_smile:

On Oct 10, 2012, at 03:05 , Peter Z. [email protected]
wrote:

Anyway… for now, I’ll just make exceptions to my indentation fixation,
waiting for a freer future.

You can use continuations (although I think I recall hearing that they
were either removed or dumbed down in 1.9).

They were, in fact, added in 1.9.

No, they were added MUCH earlier than 1.9.

RUBY_VERSION
=> “1.8.7”
callcc {}
=> nil

In fact, way way earlier than that:

10024 % cd ruby_1_4
10025 % ag callcc
./ChangeLog
2301: * eval.c (rb_callcc): call scope_dup() for all scopes in
3127: * eval.c (rb_callcc): experimental continuation support.

./eval.c
7518:rb_callcc(self)
7616: rb_define_global_function(“callcc”, rb_callcc, 0);

On Wed, Oct 10, 2012 at 2:01 PM, Ryan D.
[email protected]wrote:

Eric C. 10.10.2012 02:12:
They were, in fact, added in 1.9.

No, they were added MUCH earlier than 1.9.

I’m guessing he was probably thinking of Fibers