Autoconvert object from one class1 to other class2 in class1 def

L

Luk Mus

Is it possible to convert from one Class1 to another Class2 within the
functions of the Class1?

Example:

class String
def to_i!
self=self.to_i
end
end

c='1' # c='1' - String
c.to_i! # c=1 - Integer

Previous code is wrong, error:
Can't change the value of self
self=self.to_i
 
L

Luk Mus

or how get name of variable in class method?
I mean that may be:

class String
def to_i!
# i don't know what is real name of this method (get_name_of_variable)
name=self.get_name_of_variable
eval("#{name}=#{name}.to_i")
end
end
 
B

Brian Candler

Luk Mus wrote in post #990946:
Is it possible to convert from one Class1 to another Class2 within the
functions of the Class1?

No, an object cannot change its class. All you can do is return a new
object of a different class.
or how get name of variable in class method?

That's not possible either. The same object could be pointed to by many
variables (local and/or global and/or instance), or by none. For
example, maybe the only reference to the object is as a value in a Hash.
 
L

Luk Mus

Brian Candler wrote in post #990955:
That's not possible either. The same object could be pointed to by many
variables (local and/or global and/or instance), or by none. For
example, maybe the only reference to the object is as a value in a Hash.

Thanks!

But i can get object_id:

class String
def get_id
puts self.__id__
puts self.object_id
end
end

Is it realy to get name by ID?
 
B

Brian Candler

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.
 
A

Adam Prescott

[Note: parts of this message were removed to make it a legal post.]

Brian Candler wrote in post #990955:

Thanks!

But i can get object_id:

class String
def get_id
puts self.__id__
puts self.object_id
end
end

Is it realy to get name by ID?


I suppose just for the local variable case, you could use Kernel's
`local_variables`, combined with a binding, and having a single reference to
the object you're trying to find all local variable references to. ("_" here
is because of irb.)

Equivalent gist paste for the below: https://gist.github.com/903529
local_variables => ["_"]
a = "" => ""
local_variables => ["_", "a"]
b = binding; local_variables.select { |var| b.eval(var).object_id ==
a.object_id }
=> ["a"]
x = a => ""
local_variables => ["_", "a", "b", "x"]
b = binding; local_variables.select { |var| b.eval(var).object_id ==
a.object_id }
=> ["a", "x"]

Not a very useful exercise, though, I think, more an interesting one.
 
R

Robert Klemme

Brian Candler wrote in post #990955:

But i can get object_id:

class String
=A0def get_id
=A0 =A0puts self.__id__
=A0 =A0puts self.object_id
=A0end
end

Is it realy to get name by ID?

No, it isn't. As Brian has pointed out already there can be arbitrary
many references to an object and they can be

- local variables
- member variables
- class variables
- Array entries
- Hash entries

Lik, what do you need this for? What is the use case?

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top