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

  • Home
  • SEARCH
  • 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 8751833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:07:19+00:00 2026-06-13T13:07:19+00:00

I am wandering if it is OK to use ruby’s implicit return value when

  • 0

I am wandering if it is OK to use ruby’s implicit return value when using []= method

[]= uses rb_hash_aset and it is returning val – http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-5B-5D-3D

here is a little code to demonstrate what I mean:

require 'benchmark'
CACHE = {}
def uncached_method(key)
    warn "uncached"
    rand(100)
end
def cached(key)
  CACHE[key] || (CACHE[key] = uncached_method(key))
end
def longer_cached(key)
  return CACHE[key] if CACHE[key]
  CACHE[key] = uncached_method(key)
  CACHE[key]
end

Benchmark.bm(7) do |x|
    y = rand(10000)
    cached(y)
    x.report("shorter:") { 10000000.times do cached(y) end }
    x.report("longer:") { 10000000.times do longer_cached(y) end }
end

of course longer_cached is slower because it does two hash lookups to return cached value, but when you read it line by line it makes more sense then the cached method.

I think using implicit returns is one of the things that makes ruby awesome, but I have always questioned their use when setting values.

So my question is: would you use implicit return from (hash[key] = val)?

  • 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-13T13:07:21+00:00Added an answer on June 13, 2026 at 1:07 pm

    Just because nobody mentioned it so far: you are not relying on the return value of Hash#[]=. That return value gets ignored anyway:

    class ReturnFortyTwo
      def []=(*)
        return 42
      end
    end
    
    r = ReturnFortyTwo.new
    
    r[23] = 'This is the value that is going to be returned, not 42'
    # => 'This is the value that is going to be returned, not 42'
    

    In Ruby, assignment expressions always evaluate to the value that is being assigned. No exception. That is guaranteed by the Language Specification. So, I don’t see anything wrong with relying on that.

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

Sidebar

Related Questions

I use both ruby on rails and Java. I really enjoy using migrations when
I'm just starting out with Ruby/Rails and am wondering what Rails developers use to
Just wandering if it makes sense, to use if statements like below within jquery
Is it possible to use ruby code in RJS files? For example, the destroy.js.rjs
Edit : What I recommend now with any Ruby application to use omniauth gem
So I was wandering if there is an equivalent to <%= in a Ruby
I have a bit of an odd use-case for a Ruby Enumerable, it seems.
This is Ruby 1.8 Question: We all know how to use Array#uniq : [1,2,3,1].uniq
I am currently using rails 3.0.7 and ruby 1.9.2 and making a rails App
I want to build an app that use in the backend Ruby on Rails.

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.