Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2302 > unrolled thread

Autoconvert object from one class1 to other class2 in class1 def

Started byLuk Mus <support@fit4u.su>
First post2011-04-05 03:02 -0500
Last post2011-04-05 07:56 -0500
Articles 7 — 4 participants

Back to article view | Back to comp.lang.ruby


Contents

  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

#2302 — Autoconvert object from one class1 to other class2 in class1 def

FromLuk Mus <support@fit4u.su>
Date2011-04-05 03:02 -0500
SubjectAutoconvert object from one class1 to other class2 in class1 def
Message-ID<5095fcbac456c1c2b31e5c05fbea960d@ruby-forum.com>
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

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

[toc] | [next] | [standalone]


#2303

FromLuk Mus <support@fit4u.su>
Date2011-04-05 03:19 -0500
Message-ID<670986c859eadfac73e33b9e682aba39@ruby-forum.com>
In reply to#2302
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

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

[toc] | [prev] | [next] | [standalone]


#2305

FromBrian Candler <b.candler@pobox.com>
Date2011-04-05 03:46 -0500
Message-ID<3962d67dedc7aa2c534540d17939bf1a@ruby-forum.com>
In reply to#2302
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.

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

[toc] | [prev] | [next] | [standalone]


#2314

FromLuk Mus <support@fit4u.su>
Date2011-04-05 04:15 -0500
Message-ID<48a449220f19a90143933ae377a0bbb3@ruby-forum.com>
In reply to#2305
Brian Candler wrote in post #990955:
>> 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.

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?

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

[toc] | [prev] | [next] | [standalone]


#2331

FromBrian Candler <b.candler@pobox.com>
Date2011-04-05 07:41 -0500
Message-ID<d85ebc05011e72d6c31b4353743d934e@ruby-forum.com>
In reply to#2314
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/.

[toc] | [prev] | [next] | [standalone]


#2333

FromAdam Prescott <adam@aprescott.com>
Date2011-04-05 07:50 -0500
Message-ID<BANLkTinP4CM6KgnfDsZJwAfvcNGpVMJu7w@mail.gmail.com>
In reply to#2314
[Note:  parts of this message were removed to make it a legal post.]

On Tue, Apr 5, 2011 at 10:15 AM, Luk Mus <support@fit4u.su> wrote:

> Brian Candler wrote in post #990955:
> >> 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.
>
> 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.

[toc] | [prev] | [next] | [standalone]


#2335

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-04-05 07:56 -0500
Message-ID<BANLkTikqVoOYMet8oK39T_JsW7xEHZjOvA@mail.gmail.com>
In reply to#2314
On Tue, Apr 5, 2011 at 11:15 AM, Luk Mus <support@fit4u.su> wrote:
> Brian Candler wrote in post #990955:
>>> 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.
>
> 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?

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

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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web