Hacker News new | past | comments | ask | show | jobs | submit login

I believe strings are mutable in Ruby.



They are...

    s = "hello"
    s << "   world"
    s # hello world


Is that allocating a new buffer, leaving the "hello" string to be collected by the GC?


No, it's operating in place:

    def append_world(str)
        str << " world"
    end

    a = "hello"
    append_world(a)
    a                       #=> "hello world"


No, it expands the existing buffer. (leaving " world" to be collected). Note that the following is different and more like what you're thinking.

    a = a + " world"




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: