Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Is there some way to execute a block within an arbitrary lexical scope?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Ruby Talk  
View profile  
 More options May 1 2008, 3:31 pm
Newsgroups: comp.lang.ruby
From: Ruby Talk <rubyt...@postful-inc.com>
Date: Thu, 1 May 2008 14:31:28 -0500
Local: Thurs, May 1 2008 3:31 pm
Subject: Is there some way to execute a block within an arbitrary lexical scope?
Is there some way to execute a block within a certain lexical scope?

Let's say the scope I want is inside B:
module A
  class B
    # .. wanna get in here
  end
end

And I wanted something like:

method_returning_B_in_lexical_scope_of_A.class_eval do
   ...
end

As most of you know 'A::B.class_eval' does not cut it, since A would not
be in the nesting.

The obvious reason I want this is to take advantage of the nesting for
lookups.

Thanks for any ideas!

-- Ara Vartanian


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Klemme  
View profile  
 More options May 1 2008, 3:43 pm
Newsgroups: comp.lang.ruby
From: Robert Klemme <shortcut...@googlemail.com>
Date: Thu, 01 May 2008 21:43:17 +0200
Local: Thurs, May 1 2008 3:43 pm
Subject: Re: Is there some way to execute a block within an arbitrary lexical scope?
On 01.05.2008 21:31, Ruby Talk wrote:

Why do you need it?  There *is* nested lookup already:

robert@fussel ~
$ ruby -e 'module A;X=1;class B;def x;X;end;end;end;p A::B.new.x'
1

Cheers

        robert


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ruby Talk  
View profile  
 More options May 1 2008, 5:31 pm
Newsgroups: comp.lang.ruby
From: Ruby Talk <rubyt...@postful-inc.com>
Date: Thu, 1 May 2008 16:31:41 -0500
Local: Thurs, May 1 2008 5:31 pm
Subject: Re: Is there some way to execute a block within an arbitrary lexical scope?
Let's say I'm doing some metaprogramming from another class and I want
to do something in B's full lexical class scope.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ara.t.howard  
View profile  
 More options May 1 2008, 6:01 pm
Newsgroups: comp.lang.ruby
From: "ara.t.howard" <ara.t.how...@gmail.com>
Date: Thu, 1 May 2008 17:01:29 -0500
Local: Thurs, May 1 2008 6:01 pm
Subject: Re: Is there some way to execute a block within an arbitrary lexical scope?

On May 1, 2008, at 3:31 PM, Ruby Talk wrote:

> Let's say I'm doing some metaprogramming from another class and I  
> want to do something in B's full lexical class scope.

then you will need to use continuations.

a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being  
better. simply reflect on that.
h.h. the 14th dalai lama


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Klemme  
View profile  
 More options May 2 2008, 3:42 am
Newsgroups: comp.lang.ruby
From: Robert Klemme <shortcut...@googlemail.com>
Date: Fri, 2 May 2008 02:42:26 -0500
Local: Fri, May 2 2008 3:42 am
Subject: Re: Is there some way to execute a block within an arbitrary lexical scope?
2008/5/2 ara.t.howard <ara.t.how...@gmail.com>:

>  On May 1, 2008, at 3:31 PM, Ruby Talk wrote:

> > Let's say I'm doing some metaprogramming from another class and I want to
> do something in B's full lexical class scope.

What exactly is this "something"?

>  then you will need to use continuations.

It depends of course what should be achieved.  A simple constant
lookup is easily done via #const_get.  Other things can be achieved
via #instance_eval.

Cheers

robert

--
use.inject do |as, often| as.you_can - without end


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ruby Talk  
View profile  
 More options May 2 2008, 1:26 pm
Newsgroups: comp.lang.ruby
From: Ruby Talk <rubyt...@postful-inc.com>
Date: Fri, 2 May 2008 12:26:53 -0500
Local: Fri, May 2 2008 1:26 pm
Subject: Re: Is there some way to execute a block within an arbitrary lexical scope?
Robert,

Well, let's say that that something is a new class I want to define
within that scope. And I'd like the innards of that class to be able to
take advantage of the the natural, idiomatic lookup rules, where the
interpreter traipses up the ancestor chain.

To be more clear, let's say I have

X = :bad
module A
  X = :good
  class B
    #...let us say I want to execute something here where X == :good
  end
end

And I'd like to execute something from outside that lexical scope (let's
say from my metaprogramming classes) within that [A, A::B] lexical scope.

I guess what I'm asking is: is there any way of faking lexical scope
(for the purposes of metaprogramming)?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ruby Talk  
View profile  
 More options May 2 2008, 1:37 pm
Newsgroups: comp.lang.ruby
From: Ruby Talk <rubyt...@postful-inc.com>
Date: Fri, 2 May 2008 12:37:10 -0500
Local: Fri, May 2 2008 1:37 pm
Subject: Re: Is there some way to execute a block within an arbitrary lexical scope?
Ara,

Thanks for your suggestion. I'm a little hazy on how this would help. If
I defined a proc or code block or whatever elsewhere, it's bound to the
lexical scope where it was defined. How does calling it from the
continuation help given that?

Anyway, maybe that's not what you were thinking, and it's likely I'm
missing the obvious here. I'm not the best at problem solving with
continuations.

-- Ara Vartanian


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sean O'Halpin  
View profile  
 More options May 2 2008, 9:54 pm
Newsgroups: comp.lang.ruby
From: Sean O'Halpin <sean.ohal...@gmail.com>
Date: Fri, 2 May 2008 20:54:14 -0500
Local: Fri, May 2 2008 9:54 pm
Subject: Re: Is there some way to execute a block within an arbitrary lexical scope?

Hi,

there doesn't appear to be any form of block invocation that changes
how constants are looked up.

Some experiments:

X = :bad
module A
  X = :good
  class B
    #...let us say I want to execute something here where X == :good
  end
end
# as you state, this doesn't work
A.module_eval { X }             # => :bad
# nor this
A.instance_eval { X }           # => :bad
# nor this
A::B.new.instance_eval { X }    # => :bad
# this does of course
module A
  X                             # => :good
end
# and this
module A
  class B
    X                           # => :good
  end
end
# ...but this surprised me
class A::B
  X                             # => :bad
end
# this doesn't work either
context_A = A.module_eval { binding }
eval("X", context_A)            # => :bad
# you have to use a string
context_A = A.module_eval "binding"
eval("X", context_A)            # => :good
# but again
context_B = A::B.module_eval "binding"
eval("X", context_B)            # => :bad
# so you can't parameterize this which means you have to do something
# like:
def eval_in_namespace(namespace, str)
  constants = []
  namespace.split(/::/).reject{|x|
x.empty?}.inject(Module.const_get(self.class.to_s)) { |prev, this|
    (constants << prev.const_get(this)).last
  }
  prefix = constants.map{ |x| "#{x.class.to_s.downcase} #{x}"}.join(';')
  suffix = constants.map{ 'end'}.join(';')
  eval "#{prefix}; #{str}; #{suffix}"
end
eval_in_namespace("A::B", "X")  # => :good
# not very pretty :S
# of course, you could just use:
A::X          # => :good
# but not
A::B::X       # => :bad # !> toplevel constant X referenced by A::B::X

Ara - could you shed some light on how continuations would help here?
I don't see it myself.

Regards,
Sean


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ara.t.howard  
View profile  
 More options May 3 2008, 12:47 am
Newsgroups: comp.lang.ruby
From: "ara.t.howard" <ara.t.how...@gmail.com>
Date: Fri, 2 May 2008 23:47:06 -0500
Local: Sat, May 3 2008 12:47 am
Subject: Re: Is there some way to execute a block within an arbitrary lexical scope?

On May 2, 2008, at 7:54 PM, Sean O'Halpin wrote:

> Ara - could you shed some light on how continuations would help here?
> I don't see it myself.

ingore me, i'm insane.  i was thinking of evil.rb.

a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being  
better. simply reflect on that.
h.h. the 14th dalai lama


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Klemme  
View profile  
 More options May 3 2008, 4:55 am
Newsgroups: comp.lang.ruby
From: Robert Klemme <shortcut...@googlemail.com>
Date: Sat, 03 May 2008 10:55:51 +0200
Local: Sat, May 3 2008 4:55 am
Subject: Re: Is there some way to execute a block within an arbitrary lexical scope?

Please do not top post.

On 02.05.2008 19:26, Ruby Talk wrote:

For constant lookup there is:

robert@fussel /cygdrive/c/Temp
$ ruby lex.rb
2

robert@fussel /cygdrive/c/Temp
$ ruby lex.rb
2
2

robert@fussel /cygdrive/c/Temp
$ cat lex.rb

class Module
   def lex_lookup(c)
     name.split('::').inject([TOPLEVEL_BINDING,[]]) do |(b,o),n|
       b = (b.const_get n rescue eval(n,b))
       [b, o << b]
     end.last.reverse.each do |cl|
       return cl.const_get(c) if cl.const_defined? c
     end
     raise NameError, c
   end
end

X=1
class Foo
   X=2
   class Bar
     puts lex_lookup("X")
   end
   puts lex_lookup("X")
end

robert@fussel /cygdrive/c/Temp
$

You can easily extend that to include top level constants as well as
other evaluations.

Cheers

        robert


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »