Forum: Ruby-core [ruby-trunk - Bug #7827][Open] Allow users to yield a block in string format

A14d1bca90d7ebeda50dca99aab381b9?d=identicon&s=25 Markus H. (markus_h)
on 2013-02-11 18:48
(Received via mailing list)
Issue #7827 has been reported by shevegen (markus heiler).

----------------------------------------
Bug #7827: Allow users to yield a block in string format
https://bugs.ruby-lang.org/issues/7827

Author: shevegen (markus heiler)
Status: Open
Priority: Low
Assignee:
Category:
Target version:
ruby -v: 1.9.3p385


Hi.

Not long ago I realized that you can use "invalid" syntax inside a ruby
block.

For instance:

def foo; end

foo() { ABC } # ABC is not known.

This works, until you yield that.

And if you yield it, that would fail, unless the ABC constant would be
known.
(Or perhaps a global variable instead, so let's consider:

foo() { $abc }

So far so good.

Now I actually had the idea that a ruby user might want to
return the content of a block in string format, modify that
string, and then somehow eval it (or .call it)

Would that be useful? Or even possible?

It probably is not so useful, but it would be kind of
fun to be able to put whole classes inside a block,
which later could get known, or turned into an unbound
object again... Ok, I have no idea if this is useful
at all. :)

If it is just a crazy idea, just close it :D

So my suggestion would be:


  def foo; end
  result = foo() { $abc }
  $abc = 5
  # And here, either yield externally on result,
  # or turn it into a proc somehow, and then
  # before evaluating it, perhaps add a new
  # method like:
  result.proc.proc_string # This here would return '$abc'
  which could then manually be eval()ed at a later
  time.

I know, I have absolutely no use case for this, I
just thought it would be cool to use invalid code
inside {} and then perhaps lateron turn this into
valid ruby code (or use it in some other way, such
as __END__ and DATA.read)

Thanks for reading at least!
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 matz (Yukihiro Matsumoto) (Guest)
on 2013-02-12 01:55
(Received via mailing list)
Issue #7827 has been updated by matz (Yukihiro Matsumoto).

Status changed from Open to Rejected

Adding it as a standard feature requires Ruby to hold source
information, that leads to memory consumption.
You might be interested in ruby2ruby for the purpose.

Matz.

----------------------------------------
Bug #7827: Allow users to yield a block in string format
https://bugs.ruby-lang.org/issues/7827#change-36153

Author: shevegen (markus heiler)
Status: Rejected
Priority: Low
Assignee:
Category:
Target version:
ruby -v: 1.9.3p385


Hi.

Not long ago I realized that you can use "invalid" syntax inside a ruby
block.

For instance:

def foo; end

foo() { ABC } # ABC is not known.

This works, until you yield that.

And if you yield it, that would fail, unless the ABC constant would be
known.
(Or perhaps a global variable instead, so let's consider:

foo() { $abc }

So far so good.

Now I actually had the idea that a ruby user might want to
return the content of a block in string format, modify that
string, and then somehow eval it (or .call it)

Would that be useful? Or even possible?

It probably is not so useful, but it would be kind of
fun to be able to put whole classes inside a block,
which later could get known, or turned into an unbound
object again... Ok, I have no idea if this is useful
at all. :)

If it is just a crazy idea, just close it :D

So my suggestion would be:


  def foo; end
  result = foo() { $abc }
  $abc = 5
  # And here, either yield externally on result,
  # or turn it into a proc somehow, and then
  # before evaluating it, perhaps add a new
  # method like:
  result.proc.proc_string # This here would return '$abc'
  which could then manually be eval()ed at a later
  time.

I know, I have absolutely no use case for this, I
just thought it would be cool to use invalid code
inside {} and then perhaps lateron turn this into
valid ruby code (or use it in some other way, such
as __END__ and DATA.read)

Thanks for reading at least!
This topic is locked and can not be replied to.