Re: [ruby_ireland] Constants scoping problem

23 views
Skip to first unread message

Richard Conroy

unread,
Sep 21, 2012, 10:40:49 AM9/21/12
to ruby_i...@googlegroups.com
Padrino could be doing some interesting things with routes and scopes.

Try MyProject::MY_CONST to explicitly reference the constant

On Fri, Sep 21, 2012 at 3:00 PM, Stu <stu....@gmail.com> wrote:
Hi all,

I spoke to a few of ye about this on IRC today (thanks again for the tips!). Still stumped tho :-(

I have a Padrino app which is causing me some confusion when I try to access app constants inside routes.

I'm fairly green with Ruby, and I think this is my misunderstanding of how scoping works in Ruby, rather than a Padrino-specific issue.

Here is a gist (simplified since the IRC discussion) showing the problem: https://gist.github.com/3760461/ 

I would have thought I'd be able to access the constant unscoped. 

Apparently not. Can anybody explain why?


Cheers,
Stu

PS: I stepped through using the debugger as Declan suggested but that only confirmed what the console output shows - ie: the constant exists, inside the MyProject scope.




--
You received this message because you are subscribed to the Google Groups "Ruby Ireland" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/ruby_ireland/-/1tPdCmRdN3QJ.
To post to this group, send an email to ruby_i...@googlegroups.com.
To unsubscribe from this group, send email to ruby_ireland...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ruby_ireland?hl=en-GB.



--

Stu Gray

unread,
Sep 21, 2012, 11:08:47 AM9/21/12
to ruby_i...@googlegroups.com

Hi Richard,

Yeah, I can defo work around it like that.

But I'm curious now... I don't like unsolved mysteries :-)

Richard Conroy

unread,
Sep 21, 2012, 11:19:23 AM9/21/12
to ruby_i...@googlegroups.com
The issue then is that the constant MY_CONST is not in scope in the padrino controller.

I would put a debug line in to your get '....' block to see what self is. 

Padrino is built on top of Sinatra, and the simplicity of its route definitions is deceptive. They look like arbitrary method calls in the public scope, but I suspect that there is some interesting stuff going on in the background, like define_method going on in the get and post implementations.

If you look at the source for def get() you might see what it is doing with self, and where it is putting the real method definition.

Stu Gray

unread,
Sep 21, 2012, 11:42:00 AM9/21/12
to ruby_i...@googlegroups.com
Cheers Richard.

I had a poke around in the padrino source as suggested. 

I was able to come up with the following code for reproducing the error without using padrino at all:  https://gist.github.com/3762230 

I guess I'm looking for somebody to explain how MY_CONST can be undefined, when self = "Thing" and "Thing::MY_CONST" does exist.

Richard Conroy

unread,
Sep 21, 2012, 1:05:06 PM9/21/12
to ruby_i...@googlegroups.com
put a debugger line in your padrino source.

You will probably find that self is being assigned to something that is not obvious from the source.

In line 38 of the code in the gist below, self is not the class. it is the instance.

Tony Byrne

unread,
Sep 21, 2012, 10:45:05 AM9/21/12
to ruby_i...@googlegroups.com
Hi Stu,

On 21 Sep 2012, at 15:00, Stu <stu....@gmail.com> wrote:

> I would have thought I'd be able to access the constant unscoped.
>
> Apparently not. Can anybody explain why?

I know nothing about Padrino, but I suspect this is down to how Ruby looks up constants, which is not quite as obvious as one would hope. This may help:

http://jfire.io/blog/2011/01/21/making-sense-of-constant-lookup-in-ruby/


Regards,

Tony.

Tony Byrne

AonInsure.ie
Head of Application Development
The Insurance Centre | 7 Sandyford Business Centre
Sandyford | Dublin 18
T: (01) 293 2389 | F: (01) 293 2340 | M: 087 2628 928
tony....@aoninsure.ie | www.aoninsure.ie
Insureforsure Ltd t/a AonInsure.ie is regulated by the Central Bank of Ireland. Registered in Ireland Number 451805.

This message and any attachment are confidential and may be privileged or otherwise protected from disclosure and may include proprietary information. If you are not the intended recipient, please telephone or email the sender and delete this message and any attachment from your system. If you are not the intended recipient you must not copy this message or attachment or disclose the contents to any other person. Any personal views expressed in this e-mail not directly related to the normal course of our business are those of the individual sender and AonInsure.ie does not endorse or accept responsibility for them. Finally you should be aware that AonInsure.ie reserves the right and intends to intercept and monitor incoming and outgoing email correspondence, so you should not expect any email communications to be private in nature.

Marcin Wyszyński

unread,
Sep 21, 2012, 2:12:58 PM9/21/12
to ruby_i...@googlegroups.com
This has nothing to do with Padrino. 'Blame' Ruby class model. self refers to an instance of a class here.

When you define Ruby methods - ie you use the keyword def, your code is executed in the scope of the class although it only affects instances. Since MY_CONST is defined in the class scope, you have access to it during that time - that is inside your class. When on the other hand you work with an instance, MY_CONST is not defined in its scope. You can easily access it through self.class::MY_CONST should you wish to do so.

This is a tricky one however. Take Ruby 1.9.2 and your second Gist will run just fine. Take 1.8.7 or 1.9.3 and it'll break. 1.9.2 seems to be quite liberal in terms of scoping but this is an exception rather than a rule.

That being said I may be completely wrong :)

Marcin

Declan McGrath

unread,
Sep 21, 2012, 2:37:59 PM9/21/12
to ruby_i...@googlegroups.com

Fyi, apologies to Tony whose message got held up in the RubyIreland moderation queue!

Stu Gray

unread,
Sep 21, 2012, 3:45:47 PM9/21/12
to ruby_i...@googlegroups.com

That's great guys. Thanks for the feedback & link - plenty for me to digest.

It's early days for me and Ruby, so still getting to grips with how these kinds of things are handled. 

Appreciate the help!

Stu

Reply all
Reply to author
Forward
0 new messages