[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

access to constants from instance_eval

David Chelimsky

2/8/2007 11:47:00 AM

Hi all,

It seems that Ruby won't allow me to directly access class constants
from instance_eval:

irb(main):001:0> class Foo
irb(main):002:1> BAR = "bar"
irb(main):003:1> def bar
irb(main):004:2> BAR
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> foo = Foo.new
=> #<Foo:0x10d97f0>
irb(main):008:0> foo.class.constants
=> ["BAR"]
irb(main):009:0> foo.bar
=> "bar"
irb(main):010:0> foo.instance_eval { bar }
=> "bar"
irb(main):011:0> foo.instance_eval { BAR }
NameError: uninitialized constant BAR
from (irb):11
from (irb):11:in `instance_eval'
from (irb):11

Can anybody explain to me why this might be the case (is there a
philosophical reason that this is correct?) and/or some meta-magic
spell I can use to coerce instance_eval into recognizing foo's
constants?

Thanks,
David

5 Answers

dblack

2/8/2007 12:19:00 PM

0

James Gray

2/8/2007 1:06:00 PM

0

On Feb 8, 2007, at 5:46 AM, David Chelimsky wrote:

> Hi all,
>
> It seems that Ruby won't allow me to directly access class constants
> from instance_eval:
>
> irb(main):001:0> class Foo
> irb(main):002:1> BAR = "bar"
> irb(main):003:1> def bar
> irb(main):004:2> BAR
> irb(main):005:2> end
> irb(main):006:1> end
> => nil
> irb(main):007:0> foo = Foo.new
> => #<Foo:0x10d97f0>
> irb(main):008:0> foo.class.constants
> => ["BAR"]
> irb(main):009:0> foo.bar
> => "bar"
> irb(main):010:0> foo.instance_eval { bar }
> => "bar"
> irb(main):011:0> foo.instance_eval { BAR }
> NameError: uninitialized constant BAR
> from (irb):11
> from (irb):11:in `instance_eval'
> from (irb):11
>
> Can anybody explain to me why this might be the case (is there a
> philosophical reason that this is correct?) and/or some meta-magic
> spell I can use to coerce instance_eval into recognizing foo's
> constants?

I doubt this is what you are after, but:

>> class Foo
>> BAR = "bar"
>> def bar
>> BAR
>> end
>> end
=> nil
>> foo = Foo.new
=> #<Foo:0x11004cc>
>> foo.instance_eval { self.class.const_get(:BAR) }
=> "bar"

James Edward Gray II

Bertram Scharpf

2/8/2007 1:07:00 PM

0

Hi,

Am Donnerstag, 08. Feb 2007, 21:18:40 +0900 schrieb dblack@wobblini.net:
> On Thu, 8 Feb 2007, David Chelimsky wrote:
>
> >irb(main):001:0> class Foo
> >irb(main):002:1> BAR = "bar"
> >irb(main):003:1> def bar
> >irb(main):004:2> BAR
> >irb(main):005:2> end
> >irb(main):006:1> end
> [...]
> >irb(main):011:0> foo.instance_eval { BAR }
> >NameError: uninitialized constant BAR
>
> foo doesn't have any constants; only Foo does. instance_eval changes
> self, but it's still in the same scope. So the constants available
> inside the block are the same as those available outside the block.

`Scope' is not neccessarily "self":

irb(main):001:0> class C ; X = "i" ; end
=> "i"
irb(main):002:0> X = "o"
=> "o"
irb(main):003:0> C.instance_eval { X }
=> "o"
irb(main):004:0> C.class_eval { X }
=> "o"
irb(main):005:0> C.instance_eval { self::X }
=> "i"
irb(main):006:0> C.class_eval { self::X }
=> "i"
irb(main):007:0>

Is there a documentation that explains the differences between "scope",
"context", "self" and whatelse?

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Islander

12/6/2012 3:31:00 AM

0

On 12/5/2012 5:31 PM, Josh wrote:
> I'm not sure who "does it right". I'm not even sure what the "it" is.
> "It" could be public versus private health insurance/care. Or "it"
> could refer to tort practice. Or "it" could refer to outcome-based
> decision making. Even if you narrow down the "if", I haven't studied
> the issue enough to know who has done it right. I merely noting all
> parties who want to fix our fiscal problems should be studying who has
> done "it" right.

We should take a lesson from the Taiwanese who when faced with the issue
took the time and effort to study universal health care approaches
around the world and to then implement what best met their needs. It is
not perfect, but it is a pretty good system. The only criticism that I
have heard is that it is under-funded, subject to political whims on
their budget.

Basically, it is a single-payer system with competitive health delivery.
While it was initially implemented as a fee-for-service program, they
have realized the importance of paying for results. They continue to
modify the program to improve it. They have cut health care inflation
from 13% to 5%.

Overall, they spend about 6% of GDP on health care and achieve better
results than the US which now spends about 16% of GDP.


El Castor

12/8/2012 8:25:00 PM

0

On Fri, 07 Dec 2012 15:12:31 -0800, Islander <nospam@priracy.net>
wrote:

>On 12/7/2012 11:10 AM, El Castor wrote:
>> On Fri, 07 Dec 2012 09:04:09 -0800, Islander <nospam@priracy.net>
>> wrote:
>>
>>> On 12/6/2012 11:21 PM, El Castor wrote:
>>>> On Thu, 06 Dec 2012 14:19:52 -0800, Islander <nospam@priracy.net>
>>>> wrote:
>>>>
>>>>> On 12/6/2012 11:34 AM, El Castor wrote:
>>>>>> On Thu, 06 Dec 2012 07:52:16 -0800, Islander <nospam@priracy.net>
>>>>>> wrote:
>>>>>>
>>>>>>> On 12/5/2012 10:24 PM, El Castor wrote:
>>>>>>>> On Wed, 05 Dec 2012 19:30:52 -0800, Islander <nospam@priracy.net>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> On 12/5/2012 5:31 PM, Josh wrote:
>>>>>>>>>> I'm not sure who "does it right". I'm not even sure what the "it" is.
>>>>>>>>>> "It" could be public versus private health insurance/care. Or "it"
>>>>>>>>>> could refer to tort practice. Or "it" could refer to outcome-based
>>>>>>>>>> decision making. Even if you narrow down the "if", I haven't studied
>>>>>>>>>> the issue enough to know who has done it right. I merely noting all
>>>>>>>>>> parties who want to fix our fiscal problems should be studying who has
>>>>>>>>>> done "it" right.
>>>>>>>>>
>>>>>>>>> We should take a lesson from the Taiwanese who when faced with the issue
>>>>>>>>> took the time and effort to study universal health care approaches
>>>>>>>>> around the world and to then implement what best met their needs. It is
>>>>>>>>> not perfect, but it is a pretty good system. The only criticism that I
>>>>>>>>> have heard is that it is under-funded, subject to political whims on
>>>>>>>>> their budget.
>>>>>>>>>
>>>>>>>>> Basically, it is a single-payer system with competitive health delivery.
>>>>>>>>> While it was initially implemented as a fee-for-service program, they
>>>>>>>>> have realized the importance of paying for results. They continue to
>>>>>>>>> modify the program to improve it. They have cut health care inflation
>>>>>>>> >from 13% to 5%.
>>>>>>>>>
>>>>>>>>> Overall, they spend about 6% of GDP on health care and achieve better
>>>>>>>>> results than the US which now spends about 16% of GDP.
>>>>>>>>>
>>>>>>>> Might be OK with me -- if it's as you say it is. Universal health care
>>>>>>>> seems inevitable. Obama and Pelosi could have at least tried to do it
>>>>>>>> right. They didn't, and now we are stuck with their abortion.
>>>>>>>>
>>>>>>> Glad to see that you are coming around. Yes, it could have been done
>>>>>>> better, but not in the Congress that Obama had to work with. Don't
>>>>>>> forget that Pelosi got the public option passed in the House, but that
>>>>>>> was killed in the Senate, even as an opt-out, by the Republicans and Joe
>>>>>>> Lieberman.
>>>>>>
>>>>>> Don't blame Republicans. Dems controlled the House and Senate, and
>>>>>> Republicans were not even allowed in the room. Nothing that
>>>>>> Republicans wanted was in that bill -- nothing. There was no national
>>>>>> discussion. No one understood what we were getting, including the
>>>>>> people who drafted the bill, piece by piece. No national groups and no
>>>>>> limits on medical malpractice lawsuits.
>>>>>>
>>>>>>> Perhaps you missed the part of Obamacare that, like the Taiwanese
>>>>>>> program, works to transition from fee-for-service to quality-based
>>>>>>> payment. What do you know of the work being done at the Center for
>>>>>>> Medicare and Medicaid Innovation, established under Obamacare?
>>>>>>
>>>>>> An Australian guy used to post here. He hated their system -- for one
>>>>>> reason. Basic healthcare was minimal, but admittedly cheap. Long waits
>>>>>> and poor service. As it stood, it was not something that he wanted for
>>>>>> himself or his wife. What he did want was the ability to supplement it
>>>>>> with insurance, but he couldn't do that. It was either accept the
>>>>>> crappy socialized system, as is, or buy private insurance and pay for
>>>>>> everything without any benefit from the public system. So he wound up
>>>>>> paying for healthcare twice -- once with his taxes for the socialized
>>>>>> system (from which he got no benefit) and a second time for private
>>>>>> care from insurance, paid for out of his own pocket.
>>>>>>
>>>>>> So, for me, I would want to supplement the crappy socialized system
>>>>>> with my own private insurance. People like you could wait six months
>>>>>> for an MRI. A week is enough for my wife. They may actually have
>>>>>> something like that in France, but did Obama/Pelosi give us a chance
>>>>>> to discuss that?
>>>>>>
>>>>> Boy, are you brainwashed! The Democrats did not control the Senate
>>>>> except for a short time near the end of 2009 and even then they could
>>>>> not get anything past Joe Lieberman. Otherwise, Pelosi did a good job
>>>>> of getting legislation through the House, but most of what she got
>>>>> passed languished in the Senate under threat of Republican filibuster.
>>>>>
>>>>> As to having a chance to "discuss" health care, this has been discussed
>>>>> for nearly a century. It has been discussed to an extreme beyond any
>>>>> other piece of major legislation. What more would you have wanted?
>>>>> Even then, Max Baucus gave up his majority in the Senate Finance
>>>>> Committee with his "Gang of Six" to assure that Republicans had their
>>>>> chance to provide input. What did the Republicans do? Delay, delay,
>>>>> delay until Charles Grassley could hit the campaign trail in August with
>>>>> his Death Panel claims.
>>>>>
>>>>> Face it, the Republicans opposed any form of universal health care and
>>>>> any weakness in the current law is due to their opposition.
>>>>>
>>>>> As to you, you still have the right to pay for whatever you think you
>>>>> might gain in terms of preferential treatment. How's that working for
>>>>> you in electronics or autos?
>>>>
>>>> We have two Apple iPads, and love them both. At the heart of my
>>>> computer sits a powerful Intel cpu. The operating system, designed in
>>>> Redmond, not far from where you live. Very happy with that, too. Sorry
>>>> you hate your country so much. Perhaps the endless overcast and
>>>> numbing isolation on that God forsaken island of yours has gotten you
>>>> depressed? Too much mercury from an all salmon diet?
>>>>
>>> You really should work on your pivot, Jeff. That one was very awkward.
>>> Switching from extolling the electronics technology to accusations
>>> that I hate my country? Surely you can do better than that!
>>
>> Why else were you asking those questions. Of course you hate your
>> country -- the corporate culture, the greed of free enterprise, etc. I
>> could go on, but you are much better at extolling your hate than I am.
>>
>>> I spent my entire professional career working in computing and
>>> microelectronics and I am very proud of what the innovative people in
>>> this country have accomplished. But, I am not proud of how the jobs
>>> that made that technology are being sent offshore. The workers have not
>>> benefited from the increased productivity that the technology made
>>> possible while the corporations have done just fine.
>>>
>>> In my electronics reference above, I was referring to your television.
>>> I doubt that there are any televisions made in the US anymore. I also
>>> know that you own a Prius.
>>>
>>> I am happy to note that Apple is planning to bring some of their
>>> manufacturing back into the US. The chip in that iPad is still
>>> manufactured in the US, as is the glass screen. Now, as surface mount
>>> technology begins to dominate these portable devices, the labor content
>>> gets smaller and smaller. While that is a good thing and gives you your
>>> playthings, there is not much in the way of American jobs in the Apple
>>> announcement.
>>
>> Fifteen years ago Herrnstein and Murray in their much maligned book,
>> The Bell Curve, wrote that in a primarily agricultural 19th century
>> America, men with IQs of 140 happily worked in the fields and barns
>> alongside men with IQs of 90, harvesting wheat and milking cows. Then
>> came the industrial age with acres of looms and assembly lines. There
>> were still many jobs for those slower men and women, manning the looms
>> and assembly lines. But ... the 21st century is the age of
>> Information. The man with an IQ of 140 is a systems engineer designing
>> the chips that power my iPad, working for Google on the self driving
>> car, or like my B-I-L, designing ultrasound equipment for a German
>> company near Seattle. Assembly lines and looms are, or will soon be,
>> manned by robots.
>>
>> Foxconn, the Chinese manufacturer of the iPad, is going to be buying
>> one million robots, presumably to replace a million $2 an hour
>> workers. Apple could do the same in the US, so why doesn't it? Maybe
>> reams of regulations, the EPA, Obamacare, carbon taxes, and the
>> mentality that refused to allow Boeing to build a plant in a
>> right-to-work state, and keeps stalling the XL pipeline? So make
>> Apple brings those jobs back? Ban the robots! Make good assembly line
>> jobs for US workers! Do that and companies like Samsung will eat our
>> lunch.
>>
>> The world is changing, Islander, and your brand of socialism offers no
>> answers. Before you can come up with those answers you will have to
>> admit to yourself what the problem is.
>>
>Specifically what do you think the problem is, Jeff? All those people
>with low IQs? Are you saying that we have an abundance of them? Want
>to put out there who you think the "takers" are?

Half the population inevitably falls on the left half of the bell
curve.

>Seriously, there is now considerable evidence to suggest that any bias
>toward the low end of the IQ distribution is due to prenatal and
>childhood health care. And, that is what the Republican want to cut.

Nonsense. More politically motivated "evidence".

>The party of "keep 'em stupid" and you are willing to continue the scam!

I don't want to keep anyone stupid. If there was a pill or injection
that would give a fetus the IQ of Albert Einstein it would transform
the world. Until then, my doctor and my dentist are both Chinese. The
student population of San Francisco's Lowell High School is:

2.9% Black
7.0% Hispanic
14.1% White
54.8% East Asian

Admission to Lowell High has been based on test scores since three
Chinese families sued.

Ashkenazi Jews and East Asians are more intelligent than European
Whites, and it's not because they eat well. Please don't tell me that
Jews owe their 187 Nobel prizes to matzoh balls. They were born that
way. Just the way it is.