[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Some inflection (?) questions

Joshua Muheim

3/26/2007 9:41:00 PM

Hi all

I have the following code snipped:

@country = Country.new

Instead of hardcoding @country and Country I'd like to use the contents
of two strings:

instance_var_name = 'country'
class_name = 'Country'

How can this be achieved?

Thanks a lot for help and yes, I'm working on my Ruby skills but I'm not
quite there yet. ;-)

Josh

--
Posted via http://www.ruby-....

5 Answers

Tim Hunter

3/26/2007 9:49:00 PM

0

Joshua Muheim wrote:
> Hi all
>
> I have the following code snipped:
>
> @country = Country.new
>
> Instead of hardcoding @country and Country I'd like to use the contents
> of two strings:
>
> instance_var_name = 'country'
> class_name = 'Country'
>
> How can this be achieved?
>
> Thanks a lot for help and yes, I'm working on my Ruby skills but I'm not
> quite there yet. ;-)
>
> Josh
>
>
ri Object#instance_variable_set
ri Module#const_get



Chris Carter

3/26/2007 9:50:00 PM

0

On 3/26/07, Joshua Muheim <forum@josh.ch> wrote:
> Hi all
>
> I have the following code snipped:
>
> @country = Country.new
>
> Instead of hardcoding @country and Country I'd like to use the contents
> of two strings:
>
> instance_var_name = 'country'
> class_name = 'Country'
>
> How can this be achieved?
>
> Thanks a lot for help and yes, I'm working on my Ruby skills but I'm not
> quite there yet. ;-)
>
> Josh
>
> --
> Posted via http://www.ruby-....
>
>

you can use the instance_variable_set and const_get methods. exempli gratia:
def set_country(ivar_name,klass)
self.instance_var_set('@'+ivar_name, Object.const_get(klass).new)
end

--
Chris Carter
concentrationstudios.com
brynmawrcs.com

Joshua Muheim

3/26/2007 10:22:00 PM

0

Thanks a lot, guys!

I got another problem now:

private
def model_obj_name
CountriesController.controller_class_name.underscore.sub(/_controller$/,
'').singularize
end

def model_obj=(obj)
self.model_obj_name
end

This gives me the following error:

private method `model_obj_name' called for
#<CountriesController:0x23e3f20>

Why that? I *can* call private methods within the methods of the same
object, can't I?!

--
Posted via http://www.ruby-....

Gavin Kistner

3/26/2007 10:25:00 PM

0

On Mar 26, 4:22 pm, Joshua Muheim <f...@josh.ch> wrote:
> Thanks a lot, guys!
>
> I got another problem now:
>
> private
> def model_obj_name
> CountriesController.controller_class_name.underscore.sub(/_controller$/,
> '').singularize
> end
>
> def model_obj=(obj)
> self.model_obj_name
> end
>
> This gives me the following error:
>
> private method `model_obj_name' called for
> #<CountriesController:0x23e3f20>
>
> Why that? I *can* call private methods within the methods of the same
> object, can't I?!

Yes, but you can't call them with an explicit receiver. Try simply:

def model_obj=( obj )
model_obj_name
end

Joshua Muheim

3/26/2007 10:40:00 PM

0

Great, thank you :-)

--
Posted via http://www.ruby-....