From: sto.mar@... Date: 2012-11-11T18:24:27+09:00 Subject: Re: Instance name of a class/object? Am 11.11.2012 05:21, schrieb Carolyn Grant: > Hi! I'm quite new to Ruby, and am quite confused about instance names. > > I am in the middle of writing an app that has a good number of > configuration files, and I'd like to write: > > basic_config = MySettings.new # Picks up & processes basic_config.yml > adv_config = MySettings.new # Picks up & processes adv_config.yml > > I'm using SettingsLogic to the hard work, so MySettings looks a bit like > this: > > class MySettings < SettingsLogic > source instance.name # For example > > # Do some other stuff > end > > I tried my_class.name; that returns a "Method not defined" error" (I'm > using 1.9.3p194) > > What I'd really like to avoid is: > > basic_config = MySettings.new(basic_config). > > As far as I can tell, what I want to do isn't possible. I did ask on > StackOverflow and Boris was a great help, but his code requires a :name > => some_name flag. So I figured I'd ask here and see if anyone can help > me! Can I do what I want to do? This could not work for several reasons: - the object is created first, before it can be assigned to a variable - a single object could be referenced by several variables - and of course: you can create and use objects without ever assigning them to a variable: `puts Time.new' What's wrong with config = MySettings.new('basic_config') [or config = MySettings.new('basic_config.yml') ] where the initialize method accepts a file name? --