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 6763099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:26:10+00:00 2026-05-26T14:26:10+00:00

Ran into some weird behaviour and wondering if anyone else can confirm what I

  • 0

Ran into some weird behaviour and wondering if anyone else can confirm what I am seeing.

Suppose you create a class with a member variable, and allow it to be read with attr_reader.

class TestClass
  attr_reader :val

  def initialize(value)
   @val = value
  end
end

Now when I do the following, it seems to modify the value of @val, even though I have only granted it read privileges.

test = TestClass.new('hello')
puts test.val
test.val << ' world'
puts test.val

This returns

hello
hello world

This is just the result from some testing I did in irb so not sure if this is always the case

  • 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-05-26T14:26:11+00:00Added an answer on May 26, 2026 at 2:26 pm

    You are not really writing the val attribute. You are reading it and invoking a method on it (the ‘<<‘ method).

    If you want an accessor that prevents the kind of modification you describe then you might want to implement a method that returns a copy of @val instead of using attr_reader.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very new to PHP and ran into some trouble and was wondering if anyone
Today I ran into some weird behaviour with a HashSet's iterator. In the code
I am new to C# and Enterprise Library and I ran into some weird
I ran into a weird situation today doing some one-time sql code. This nested
I ran into some trouble while creating a C-Extension for ruby that got me
I decided to play with some CSS3 and ran into some problems with background
It's been a while since I got into QCL but I ran into some
While creating an online shop application using play-1.2.4 ,I ran into some problems with
Ran into this error message while trying to select some records off a table.
I ran into a problem with some C code like this: struct SomeType {

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.