Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #2331
| From | Brian Candler <b.candler@pobox.com> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: Autoconvert object from one class1 to other class2 in class1 def |
| Date | 2011-04-05 07:41 -0500 |
| Organization | Service de news de lacave.net |
| Message-ID | <d85ebc05011e72d6c31b4353743d934e@ruby-forum.com> (permalink) |
| References | <5095fcbac456c1c2b31e5c05fbea960d@ruby-forum.com> <3962d67dedc7aa2c534540d17939bf1a@ruby-forum.com> <48a449220f19a90143933ae377a0bbb3@ruby-forum.com> |
Luk Mus wrote in post #990967:
> But i can get object_id:
>
> class String
> def get_id
> puts self.__id__
> puts self.object_id
> end
> end
Sure, the object_id is basically the object reference. It's a property
of the object itself, not of any variable which contains a reference to
that object.
In ruby, variables are not objects.
> Is it realy to get name by ID?
That question doesn't parse.
If you're asking "Is it really impossible to get the name of the
variable which holds a particular object_id", then I already said yes,
and explained why. Consider these cases:
"string".get_my_name # what should it return??
a = "some string"
@b = a
$c = a
a.get_my_name # what should it return??
h = {:foo => "another string"}
h[:foo].get_my_name # what should it return??
In any case, with closures, the same local variable can exist multiple
times with different values. e.g.
def make_adder(n)
lambda { |x| x+n }
end
a1 = make_adder(1)
a2 = make_adder(100)
puts a1.call(1) # 2
puts a2.call(1) # 101
The local variable 'n' has two different values in the two closures.
If you really want to update a local variable remotely, then you can
look at the 'Binding' class - but I think you really don't. You have to
pass the binding explicitly, and then use 'eval' to access a local
variable within that binding.
# DON'T DO THIS!
def increment(var, b)
eval "#{var} += 1", b
end
a = 10
increment("a", binding)
puts a # 11
Local variables are not supposed to be abused this way. It's more
reasonable to do this with instance variables (which means you should be
thinking about encapsulating long-lived state in objects, not in local
variables).
def increment(var)
instance_variable_set(var, instance_variable_get(var) + 1)
end
@a = 20
increment(:@a)
puts @a # 21
But in most sane applications, you'd use accessor methods on the object
being passed, not mess around with instance variables directly.
--
Posted via http://www.ruby-forum.com/.
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Autoconvert object from one class1 to other class2 in class1 def Luk Mus <support@fit4u.su> - 2011-04-05 03:02 -0500
Re: Autoconvert object from one class1 to other class2 in class1 def Luk Mus <support@fit4u.su> - 2011-04-05 03:19 -0500
Re: Autoconvert object from one class1 to other class2 in class1 def Brian Candler <b.candler@pobox.com> - 2011-04-05 03:46 -0500
Re: Autoconvert object from one class1 to other class2 in class1 def Luk Mus <support@fit4u.su> - 2011-04-05 04:15 -0500
Re: Autoconvert object from one class1 to other class2 in class1 def Brian Candler <b.candler@pobox.com> - 2011-04-05 07:41 -0500
Re: Autoconvert object from one class1 to other class2 in class1 def Adam Prescott <adam@aprescott.com> - 2011-04-05 07:50 -0500
Re: Autoconvert object from one class1 to other class2 in class1 def Robert Klemme <shortcutter@googlemail.com> - 2011-04-05 07:56 -0500
csiph-web