Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9260265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:50:54+00:00 2026-06-18T12:50:54+00:00

I am studying object_id method behavior in Ruby in order to know when a

  • 0

I am studying object_id method behavior in Ruby in order to know when a new object is created. I can understand (1) is identical to (2) and (3) is identical to (4). But I do not understand why (5) is not identical to (6).

(1) upto (6) are described below with my source code.

So I would like to understand when object_id will be changed (newly assigned) in Ruby. Does anybody explain it concisely?

Thank you very much in advance.

source code

a = "foo"
b = a
b.slice!(0)

p a.object_id # (1)
p b.object_id # (2)

b = b

p a.object_id # (3)
p b.object_id # (4)

b = b.slice(0)

p a.object_id # (5)
p b.object_id # (6)

result

70302398954840                                                                                                                                                                                      
70302398954840
70302398954840
70302398954840
70302398954840
70302398954680
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:50:55+00:00Added an answer on June 18, 2026 at 12:50 pm

    Variables are not objects and mutating an object does not change what the object is. That is, mutating an object will never change which variables evaluate to that object.

    The difference between the two methods is that slice! mutates the original string/object (ick!) and slice returns a new string/object without mutating the original (yay!).

    In the following X denotes a particular (but arbitrary) string, and Y represents a different particular (but arbitrary) string. Do not confuse X and Y with variables; they merely represent different objects to explain the behavior.

    a = X                # a = X
    b = a                # b = a -> b = X, so b = X
    b.slice!(0)          # MUTATE object X
    
    p a.object_id # (1)  # a = X
    p b.object_id # (2)  # b = X
    
    b = b                # b = b -> b = X, so b = X (no effect)
    
    p a.object_id # (3)  # a = X
    p b.object_id # (4)  # b = X
    
    b = b.slice(0)       # b = f(b), where NEW object (Y) is returned, so b = Y
    
    p a.object_id # (5)  # a = X
    p b.object_id # (6)  # b = Y
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well I am currently studying JavaFX and as a total beginner(but not in Java)
How many String object are created I am studying for the SCJP I cant
I'm new at object oriented programming and we are studying on a Java project.
when studying RMI sometimes (head first Java) dudes use Naming.rebind(name, object) but other peoples
I'm studying Steven Kochan's Programming in Objective-C 2.0. We created a Fraction object with
In studying actionscript 3's graphics class, I've come across the undocumented drawRoundRectComplex() method. It's
After studying TCP/UDP difference all week, I just can't decide which to use. I
I am studying the ruby on rails guides namely, the layouts and rendering topic
I just began studying WCF because i need it for a school assignment. But
Since I started studying object-oriented programming, I frequently read articles/blogs saying functions are better,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.