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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:55:50+00:00 2026-05-23T04:55:50+00:00

Trying to do something weird that might turn into something more useful, I tried

  • 0

Trying to do something weird that might turn into something more useful, I tried to define my own []= operator on a custom class, which you can do, and have it return something different than the value argument, which apparently you can’t do. []= operator’s return value is always value; even when you override this operator, you don’t get to control the return value.

class Weird 
  def []=(key, value)
    puts "#{key}:#{value}"
    return 42
  end
end

x = Weird.new
x[:a] = "a"
  output "a:a"
  return value => "a"  # why not 42?

Does anyone have an explanation for this? Any way around it?

ruby MRI 1.8.7. Is this the same in all rubys; Is it part of the language?

  • 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-23T04:55:51+00:00Added an answer on May 23, 2026 at 4:55 am

    Note that this behavior also applies to all assignment expressions (i.e. also attribute assignment methods: def a=(value); 42; end).

    My guess is that it is designed this way to make it easy to accurately understand assignment expressions used as parts of other expressions.

    For example, it is reasonable to expect x = y.a = z[4] = 2 to:

    1. call z.[]=(4,2), then
    2. call y.a=(2), then
    3. assign 2 to the local variable x, then finally
    4. yield the value 2 to any “surrounding” (or lower precedence) expression.

    This follows the principle of least surprise; it would be rather surprising if, instead, it ended up being equivalent to x = y.a=(z.[]=(4,2)) (with the final value being influenced by both method calls).


    While not exactly authoritative, here is what Programming Ruby has to say:

    • Programming Ruby (1.8), in the Expressions section:

      An assignment statement sets the variable or attribute on its left side (the lvalue) to refer to the value on the right (the rvalue). It then returns that value as the result of the assignment expression.

    • Programming Ruby 1.9 (3rd ed) in section 22.6 Expressions, Conditionals, and Loops:

      (right after describing []= method calls)

      The value of an assignment expression is its rvalue. This is true even if the assignment is to an attribute method that returns something different.

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

Sidebar

Related Questions

Lately something weird has been happening to my projects in xcode: I've been trying
I'm trying to import a large subversion repository into git using git-svn (so that
I'm trying to implement OpenFeint in my Cocos2D iPhone game, but here's something weird
I'm still learning what hibernate can do and this time i'm trying something that
I'm trying to understand the tspan tag in svg and here is something weird.
hey all, i been trying to make a class that uses a dictionary to
I am trying to get something similar to $_SERVER[PATH_INFO] but weird server issues are
I'm trying to learn C and I've come across something weird: struct { int
I'm trying to do something that seems like it should be very simple, but
I am trying something very simple, but for some reason it does not work.

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.