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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:19:40+00:00 2026-05-11T09:19:40+00:00

In Ruby, what is the most expressive way to map an array in such

  • 0

In Ruby, what is the most expressive way to map an array in such a way that certain elements are modified and the others left untouched?

This is a straight-forward way to do it:

old_a = ['a', 'b', 'c']                         # ['a', 'b', 'c'] new_a = old_a.map { |x| (x=='b' ? x+'!' : x) }  # ['a', 'b!', 'c'] 

Omitting the ‘leave-alone’ case of course if not enough:

new_a = old_a.map { |x| x+'!' if x=='b' }       # [nil, 'b!', nil] 

What I would like is something like this:

new_a = old_a.map_modifying_only_elements_where (Proc.new {|x| x == 'b'})          do |y|           y + '!'         end # ['a', 'b!', 'c'] 

Is there some nice way to do this in Ruby (or maybe Rails has some kind of convenience method that I haven’t found yet)?


Thanks everybody for replying. While you collectively convinced me that it’s best to just use map with the ternary operator, some of you posted very interesting answers!

  • 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. 2026-05-11T09:19:41+00:00Added an answer on May 11, 2026 at 9:19 am

    I agree that the map statement is good as it is. It’s clear and simple,, and would easy for anyone to maintain.

    If you want something more complex, how about this?

    module Enumerable   def enum_filter(&filter)     FilteredEnumerator.new(self, &filter)   end   alias :on :enum_filter   class FilteredEnumerator     include Enumerable     def initialize(enum, &filter)       @enum, @filter = enum, filter       if enum.respond_to?(:map!)         def self.map!           @enum.map! { |elt| @filter[elt] ? yield(elt) : elt }         end       end     end     def each       @enum.each { |elt| yield(elt) if @filter[elt] }     end     def each_with_index       @enum.each_with_index { |elt,index| yield(elt, index) if @filter[elt] }      end     def map       @enum.map { |elt| @filter[elt] ? yield(elt) : elt }     end     alias :and :enum_filter     def or       FilteredEnumerator.new(@enum) { |elt| @filter[elt] || yield(elt) }     end   end end  %w{ a b c }.on { |x| x == 'b' }.map { |x| x + '!' } #=> [ 'a', 'b!', 'c' ]  require 'set' Set.new(%w{ He likes dogs}).on { |x| x.length % 2 == 0 }.map! { |x| x.reverse } #=> #<Set: {'likes', 'eH', 'sgod'}>  ('a'..'z').on { |x| x[0] % 6 == 0 }.or { |x| 'aeiouy'[x] }.to_a.join #=> 'aefiloruxy' 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

I have an idea for a simple program to make that will help me
Some of my recent web projects that I worked on, use a flow engine
So in Ruby there is a trick to specify infinity: 1.0/0 => Infinity I
I've got a highly multithreaded app written in Ruby that shares a few instance

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.