[#401849] If statement — Masoud Ahmadi <lists@...>

Will anyone be able to point out what I am doing wrong.

15 messages 2012/12/02

[#401987] Trying to get "translator" to work — JD KF <lists@...>

So, basically, I'm trying to get the below code to work properly for

12 messages 2012/12/06

[#402012] Need help to select some listbox item in different listbox together — Jonathan Masato <lists@...>

Hello,

10 messages 2012/12/07

[#402045] if n belongs to set a and m belongs to set b repeat some steps, How? — "zubair a." <lists@...>

We can do so in java and similar languages like:

11 messages 2012/12/08

[#402078] Time.new(2001, 12, 3).to_i returns wrong value — Robert Buck <lists@...>

I am doing something that not many do, I am writing a database driver

9 messages 2012/12/09

[#402145] How I can create/extract a variable/hash into the current binding in Ruby? — Ramon de C Valle <rcvalle@...>

Hi,

12 messages 2012/12/12

[#402205] Wondering About Flatiron School — "Kevin Y." <lists@...>

Hi everyone!,

35 messages 2012/12/15
[#402207] Re: Wondering About Flatiron School — Chad Perrin <code@...> 2012/12/15

On Sat, Dec 15, 2012 at 11:51:08AM +0900, Kevin Y. wrote:

[#402214] Ruby quick reference arranged in ASCII sequence? — Old Grantonian <lists@...>

As a ruby beginner, I would be grateful for any links to a ruby

17 messages 2012/12/15

[#402226] print - and strip text between tags using Nokogiri — Paul Mena <lists@...>

I'm a Ruby Newbie trying to write a program to process thousands of HTML

13 messages 2012/12/15

[#402332] Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...>

Hello all,

37 messages 2012/12/19
[#402342] Re: Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...> 2012/12/20

First of all, thanks for the fast responses!

[#402352] Re: Perl to Ruby: regex captures to assignment. — Robert Klemme <shortcutter@...> 2012/12/20

On Thu, Dec 20, 2012 at 1:38 AM, Derrick B. <lists@ruby-forum.com> wrote:

[#402357] Re: Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...> 2012/12/20

Robert Klemme wrote in post #1089733:

[#402359] trying to strip characters from a line — Paul Mena <lists@...>

I'm reading a table from a MySQL database and then processing it row by

18 messages 2012/12/20

[#402394] simple division: -9 / 5 = -2 what? — "Derrick B." <lists@...>

$ irb

13 messages 2012/12/22

[#402412] POLS and string-handling — Paul Magnussen <lists@...>

Hi,

14 messages 2012/12/22

[#402460] "Open" dialog of Windows — "Damián M. González" <lists@...>

Hi guys, been researching about pop up the "open" file dialog of

11 messages 2012/12/24

[#402466] How do I install Ruby on my Ubuntu 12.10 partition. — Kaye Ng <lists@...>

I already have Ruby installed on my Windows 7 partition.

23 messages 2012/12/25

[#402510] Ruby Association Certified Ruby Programmer — Sean Westfall <lists@...>

How well respected is this certification in the industry: Ruby

27 messages 2012/12/27
[#402528] Re: Ruby Association Certified Ruby Programmer — Peter Hickman <peterhickman386@...> 2012/12/27

On 27 December 2012 01:28, Sean Westfall <lists@ruby-forum.com> wrote:

[#402555] numeric? — Brandon Weaver <keystonelemur@...>

I've found a bit of an annoyance trying to find out if a number is numeric

20 messages 2012/12/27

[#402580] Ruby Koans regarding Hashes. — "Derrick B." <lists@...>

I am trying to understand this, so let me know how I do. :) I know

18 messages 2012/12/28

[#402609] can't open new ruby program under "new" context menu — "Lee V." <lists@...>

I'm stuck on the new version at trying to do something very simple.

10 messages 2012/12/28

[#402642] require "test/unit" — "Mattias A." <lists@...>

Hi,

17 messages 2012/12/29
[#402667] Re: require "test/unit" — "Mattias A." <lists@...> 2012/12/31

Hi Dami=C3=A1n M. Gonz=C3=A1lez!

[#402747] Re: require "test/unit" — "Derrick B." <lists@...> 2013/01/04

Mattias A. wrote in post #1090700:

[#402749] Re: require "test/unit" — sto.mar@... 2013/01/04

Am 04.01.2013 19:48, schrieb Derrick B.:

Re: POLS and string-handling

From: Martin DeMello <martindemello@...>
Date: 2012-12-23 03:38:14 UTC
List: ruby-talk #402419
On Sat, Dec 22, 2012 at 3:15 PM, Paul Magnussen <lists@ruby-forum.com> wrote:
> # b) String
>
> myString3 = "Fred Nerk"
> myString4 = myString3
>
> myString4[0,4] = "Bert"
>
> => myString3 = "Bert Nerk"
> => myString4 = "Bert Nerk"
>
> myString3 has been "corrupted", presumably because setting myString4 to
> it actually set myString4's pointer, not its value, in the standard OO
> fashion.

There's a subtle point here:

> myString3 = "Fred Nerk"

= is a built in piece of syntax to bind a variable to an object.
Variables are not themselves objects, they are transparent references
to objects. The only time a variable cannot be transparently replaced
by the object it refers to is when it's on the left hand side of an
equal sign. So here, "Fred Nerk" uses the string literal to create the
string object #<String:0x01234567 "Fred Nerk"> [that is, an object
with type String, object_id 0x01234567 and value "Fred Nerk"] on the
heap, and binds the variable myString3 to it.

> myString4 = myString3

Here, myString3 is transparently replaced by the object it refers to,
#<String:0x01234567 "Fred Nerk">, and myString4 is bound to the same
object

> myString4[0,4] = "Bert"

This is the subtle bit. Despite the syntactic sugar, this is *not* an
= sign. There is no variable binding = involved here, it is just ruby
syntax sugar that gets rewritten to myString.[]=(0, 4, "Bert"). That
is, it calls the "[]=" method on the string object, passing it values
(0, 4, "Bert"). Again, since myString4 is not on the left hand side of
an =, it gets transparently replaced by #<String:0x01234567 "Fred
Nerk">, which then gets sent the message []= with arguments (0, 4,
"Bert"), and obligingly updates its value. So our object is now
#<String:0x01234567 "Bert Nerk"> (note that the object hasn't changed,
just its value).

> => myString3 = "Bert Nerk"
> => myString4 = "Bert Nerk"

Again, these are both transparently replaced by the object they refer
to, now #<String:0x01234567 "Bert Nerk">

> But
>
> # c) String literal
>
> myString1 = "Fred Bloggs"

Creates #<String:0x98765432 "Fred Bloggs"> on the heap, binds myString1 to it.

> myString2 = myString1

Binds myString2 to #<String:0x98765432 "Fred Bloggs">

> myString2[0,4] = "Bert"

Sends []=, (0, 4, "Bert") to #<String:0x98765432 "Fred Bloggs">, which
updates itself to  #<String:0x98765432 "Bert Bloggs">

> puts "Fred Bloggs = " + "Fred Bloggs"

Creates two *new* string object, #<String:0x00001111 "Fred Bloggs = ">
and #<String:0x00001112 "Fred Bloggs"> and passes the second one as an
argument to the + method of the first, which returns yet another
string object, #<String:0x00001113 "Fred Bloggs = Fred Bloggs"> which
it passes to "puts" which prints it out.

> puts "myString2 = " + myString2

Creates *one* new string object, #<String:0x00001114 "myString2 = ">,
and calls its + method with #<String:0x98765432 "Bert Bloggs"> (the
transparent replacement for myString2) as an argument. This creates
yet another string object, #<String:0x00001115 "myString2 = Bert
Bloggs">, which gets passed to puts and printed out.

[Note that all the string objects that got created but never had
variables bound to them are temporary objects that the garbage
collector will take care of at some point]

> So how to get around this?  The following appears to do it:
>
> # f) String constant 3
>
> MyString8 = "Fred Shufflebotham"
> myString9 = MyString8.clone

Clone creates a new string object, and sets its value equal to that of
the first one. = then binds myString9 to this new object.

> myString9[0,4] = "Bert"

the []=, 0, 4, "Bert" message is getting sent to the new object

> => MyString8 = "Fred Shufflebotham"

myString8 is still bound to the first object, which never got sent a message.

> => myString9 = "Bert Shufflebotham"

myString9 is still bound to the new object, which *did* get sent the
[]= message and updated its value

> but doesn't it cause a memory leak?

No, the garbage collector takes care of it.

martin

In This Thread