Feature image depicting code snippets for Ruby's Hash#except method as discussed in this week’s "TWIL" post on efficient coding.

Embark on a journey of micro-learning with "TWIL," our weekly series that enriches your grasp on software development one byte at a time. In this iteration, Katie unravels the simplicity of refining code with Ruby's Hash#except method – an elegant alternative to key-by-key hash manipulation that preserves the sanctity of your original data structure. Bid farewell to mutilating your hashes and embrace the harmony of non-destructive updates using Hash#except.

Hash#except

I recently encountered a case where I wanted to remove the first handful or so keys from a hash, leaving the remaining keys and values (both of arbitrary quantity and content, so not something that could be grabbed using Hash#slice, for example.

My previous approach involved deleteing the keys from the hash (grabbing the values if/when needed) and returning the remaining hash, something like:

x = { a: 1, b: 2, c: 3, d: 4 }
x.delete(:a) # => 1
x.delete(:b) # => 2
x            # => {:c=>3, :d=>4}

This requires multiple lines and method calls for a fairly simple task and, more importantly, modifies the original hash.

Enter Hash#except, which functions like slice and returns all keys/values except those specified, and does it as a new hash, so the original hash is untouched:

y = { a: 1, b: 2, c: 3, d: 4 }
y.except(:a, :b) # => {:c=>3, :d=>4}
y                # => {:a=>1, :b=>2, :c=>3, :d=>4}

Resources

  • Ruby
Katie Linero's profile picture
Katie Linero

Senior Software Engineer

Related Posts

A conceptual illustration shows a chat bubble icon at the center of a complex maze, representing the challenges of evaluating Large Language Models for commercial applications. The intricate blue-tinted labyrinth symbolizes the many considerations Cuttlesoft navigates when implementing AI solutions in enterprise software - from API integration and cost management to security compliance. This visual metaphor captures the complexity of choosing the right LLM technology for custom software development across healthcare, finance, and enterprise sectors. The centered message icon highlights Cuttlesoft's focus on practical communication AI applications while the maze's structure suggests the methodical evaluation process used to select appropriate AI tools and frameworks for client solutions.
September 12, 2024 • Frank Valcarcel

Benchmarking AI: Evaluating Large Language Models (LLMs)

Large Language Models like GPT-4 are revolutionizing AI, but their power demands rigorous assessment. How do we ensure these marvels perform as intended? Welcome to the crucial world of LLM evaluation.

Frank Valcarcel speaking at Denver Startup Week 2018 on healthcare technology and HIPAA compliance, highlighting the digital revolution in healthcare IT
October 5, 2018 • Frank Valcarcel

Scaling Healthcare Technologies at DSW 2018

Discover the transformative journey of healthcare technology at Denver Startup Week 2018 with Frank . Dive into the world of healthcare software, product management, and the critical balance between innovation and HIPAA compliance.