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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:43:00+00:00 2026-05-13T10:43:00+00:00

In SO question 2068165 one answer raised the idea of using something like this:

  • 0

In SO question 2068165 one answer raised the idea of using something like this:

params[:task][:completed_at] &&= Time.parse(params[:task][:completed_at])

as a DRYer way of saying

params[:task][:completed_at] = Time.parse(params[:task][:completed_at]) if params[:task][:completed_at]

where the params Hash would be coming from a (Rails/ActionView) form.

It’s a kind of corollary to the well-known ||= idiom, setting the value if the LHS is not nil/false.

Is using &&= like this actually a recognised Ruby idiom that I’ve somehow missed or have I just forgotten a more commonly-used idiom? It is getting rather late…

  • 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-13T10:43:00+00:00Added an answer on May 13, 2026 at 10:43 am

    It ought to be. If nothing else, params[:task] is only evaluated once when using the &&= form.

    To clarify:

    params[:task][:completed_at] = params[:task][:completed_at] && ...
    

    calls [](:task) on params twice, [](:completed_at) and []=(:completed_at) once each on params[:task].

    params[:task][:completed_at] &&= ...
    

    calls [](:task) on params once, and its value is stashed away for both the [](:completed_at) and []=(:completed_at) calls.


    Actual example describing what I’m trying to illustrate (based on Marc-Andre’s example code; much thanks):

    class X
      def get
        puts "get"
        @hash ||= {}
      end
    end
    
    irb(main):008:0> x = X.new
    => #<X:0x7f43c496b130>
    irb(main):009:0> x.get
    get
    => {}
    irb(main):010:0> x.get[:foo] = 'foo'
    get
    => "foo"
    irb(main):011:0> x.get[:foo]
    get
    => "foo"
    irb(main):012:0> x.get[:foo] &&= 'bar'
    get
    => "bar"
    irb(main):013:0> x.get[:foo] = x.get[:foo] && 'bar'
    get
    get
    => "bar"
    

    Note that using the “expanded” form causes “get” to be printed out twice, but using the compact form causes it to only be printed once.

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

Sidebar

Related Questions

Question is relevant to this and this ; the difference is, I'd prefer something
Question is old and i guess has no 100% right answer. But would like
Question says it all, really. My application is a time tracker. It's currently written
Question Using XSLT 1.0, given a string with arbitrary characters how can I get
Question: Is it possible to cluster an application which is using Socket.io for WebSocket
Question: I have just wrote my first code using c# lambda expressions. It works,
Question: How do I get a byte stream that works like StringIO for Python
Question: What kind of resources are needed to learn how to create one's own
Question: Considering the hello-gl2 example from Android NDK r6b, is this example correct when
Question is rather simple but I couldn't find nice & clean solution to my

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.