473,473 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Ruby and object references / copying

16 New Member
Hey guys,

I had a theoretical question about Ruby's object handling. Not that it really matters, but I'm using Ruby 1.8.6.

In Ruby, every variable holds a reference to an object. For instance, if you were to say:

Expand|Select|Wrap|Line Numbers
  1. myVar1 = []
  2. myVar2 = myVar1
  3. myVar2 << "giggity giggity goo"
  4.  
  5. myVar[0]      # => "giggity giggity goo"
  6.  
...the result is that the Array object referenced by both variables (it's the same object after all) would hold a new element.

However, this creates an interesting dilemma with regards to attribute readers, because technically, you can modify what should be a protected object. Normally this isn't a problem, but in the case that the object has mutating/destructive methods, they can be used. Consider the following class:

Expand|Select|Wrap|Line Numbers
  1. class Retarded
  2.   attr_reader :var
  3.  
  4.   def initialize
  5.     @var = "mystring \n"
  6.   end
  7. end
  8.  
  9. x = Retarded.new
  10.  
  11. x.var.chomp!     # => "mystring "
  12. x.var            # => "mystring " the internal variable has changed
  13.  
Is there any idiomatic way to avoid this problem, or do we all just live with it, hoping it won't matter in the end?

Thanks!
Sep 21 '07 #1
1 5862
improvcornartist
303 Recognized Expert Contributor
I have noticed the same thing. I've had programs where I wanted to play with an array, but keep the original unchanged. What I had to do was make a clone (obj.clone) and then alter the clone object. This probably doesn't help much in the way of attribute readers, but you could make a make a method to return the clone so the original isn't touched. I am by no means an expert on Ruby (still fairly new to it), so this is just my understanding. I would be interested in what others have to say, also.
Sep 21 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
6
by: solex | last post by:
Hello, I am trying to use serialization to copy objects. The object in question "Institution" inherits from a parent object "Party" both are marked as <Serializable()>. Initially I can copy an...
13
by: dmh2000 | last post by:
I am experimenting with the interactive interpreter environments of Python and Ruby and I ran into what seems to be a fundamental difference. However I may be doing something wrong in Python....
35
by: Frederick Gotham | last post by:
(Before I begin, please don't suggest to me to use "std::vector" rather than actual arrays.) I understand that an object can have resources (e.g. dynamically allocated memory), and so we have to...
28
by: Stef Mientki | last post by:
hello, I'm trying to build a simple functional simulator for JAL (a Pascal-like language for PICs). My first action is to translate the JAL code into Python code. The reason for this approach is...
9
by: Erwin Moller | last post by:
Hi Group, This may seem a odd question in a PHP group, but I think this might be a good place to ask since I am mainly a PHP coder these days that maybe starts with Ruby. Situation: A client...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.