Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3438
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!talisker.lacave.net!lacave.net!not-for-mail |
|---|---|
| From | Brian Candler <b.candler@pobox.com> |
| Newsgroups | comp.lang.ruby |
| Subject | Re: Set attribute of superclass dynamically |
| Date | Sun, 24 Apr 2011 11:00:03 -0500 |
| Organization | Service de news de lacave.net |
| Lines | 72 |
| Message-ID | <d73928e439898e283d6fa6f62f958119@ruby-forum.com> (permalink) |
| References | <ebce7d6a4bf3664dc0904383f9352c21@ruby-forum.com> |
| NNTP-Posting-Host | bristol.highgroove.com |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | 7bit |
| X-Trace | talisker.lacave.net 1303660839 20845 65.111.164.187 (24 Apr 2011 16:00:39 GMT) |
| X-Complaints-To | abuse@lacave.net |
| NNTP-Posting-Date | Sun, 24 Apr 2011 16:00:39 +0000 (UTC) |
| In-Reply-To | <ebce7d6a4bf3664dc0904383f9352c21@ruby-forum.com> |
| X-Received-From | This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway |
| X-Mail-Count | 382125 |
| X-Ml-Name | ruby-talk |
| X-Rubymirror | Yes |
| X-Ruby-Talk | <d73928e439898e283d6fa6f62f958119@ruby-forum.com> |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.ruby:3438 |
Show key headers only | View raw
Jeroen v. wrote in post #994745:
> I have the following code
>
> class foo
That's wrong - class names must start with a capital letter
> price_per_liter = 1
That sets a local variable. It's only visible between the 'class' and
'end' excluding any nested 'def', 'class' or 'module' (since each of
those start a new scope for local variables). This is unlikely to be
useful.
> price
That will raise an error - it's not a local variable so it must be a
method call, but your class Foo object doesn't have a method 'price'
either (i.e. def self.price ... )
> module bar
Error again: should be 'Bar' not 'bar'. However I'm not sure why you're
making a module Foo::Bar - it's not a class, and it's not a subclass of
Foo.
> def set_price val
> self.price_per_liter = val
That will call method 'price_per_liter=', but you have not defined one.
Error again.
> end
> end
> end
>
> In this way I can succesfully set the price per liter of the class. How
> can I make it dynamically, so I can also set the attribute 'price'?
I'm not sure what it is you're trying to achieve. Here's a guess:
class Foo
def self.price_per_liter=(val)
@price_per_liter = val
end
def self.price_per_liter
@price_per_liter
end
end
class Bar < Foo
end
Foo.price_per_liter = 133.9
Bar.price_per_liter = 144.9
puts Foo.price_per_liter
puts Bar.price_per_liter
> I tried instance_variable_set(@variable, val), but that doesn't work.
> Probably because the variables of the class 'foo' and the module 'bar'
> lives independent of each other.
It's true that the two classes have separate instance variables (since
each class is a separate object, of class Class). But you should be able
to do
Foo.instance_variable_set(:@price_per_liter, 133.9)
--
Posted via http://www.ruby-forum.com/.
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Set attribute of superclass dinamically "Jeroen v." <jeroeningen@gmail.com> - 2011-04-24 09:29 -0500
Re: Set attribute of superclass dynamically Brian Candler <b.candler@pobox.com> - 2011-04-24 11:00 -0500
Re: Set attribute of superclass dynamically "Jeroen v." <jeroeningen@gmail.com> - 2011-04-24 12:13 -0500
Re: Set attribute of superclass dynamically Brian Candler <b.candler@pobox.com> - 2011-04-24 14:50 -0500
Re: Set attribute of superclass dinamically Josh Cheek <josh.cheek@gmail.com> - 2011-04-24 12:29 -0500
csiph-web