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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:06:50+00:00 2026-05-15T23:06:50+00:00

Let’s say, I want to separate certain combinations of elements from an array. For

  • 0

Let’s say, I want to separate certain combinations of elements from an array. For example

data = %w{ start before rgb 255 255 255 between hex FFFFFF after end }
rgb, hex = [], []
data.each_with_index do |v,i|
  p [i,v]
  case v.downcase
    when 'rgb' then rgb  = data.slice! i,4
    when 'hex' then hex  = data.slice! i,2
  end
end
pp [rgb, hex, data]
# >> [0, "start"]
# >> [1, "before"]
# >> [2, "rgb"]
# >> [3, "hex"]
# >> [4, "end"]
# >> [["rgb", "255", "255", "255"],
# >>  ["hex", "FFFFFF"],
# >>  ["start", "before", "between", "after", "end"]]

The code have done the correct extraction, but it missed the elements just after the extracted sets. So if my data array is

data = %w{ start before rgb 255 255 255 hex FFFFFF after end }

then

pp [rgb, hex, data]
# >> [["rgb", "255", "255", "255"],
# >>  [],
# >>  ["start", "before", "hex", "FFFFFF", "after", "end"]]

Why does it happen? How to get those missed elements inside #each_with_index? Or may be there is a better solution for this problem assuming that there are much more sets to extract?

  • 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-15T23:06:50+00:00Added an answer on May 15, 2026 at 11:06 pm

    The problem is that you are mutating the collection while you are iterating over it. This cannot possibly work. (And in my opinion, it shouldn’t. Ruby should raise an exception in this case, instead of silently allowing incorrect behavior. That’s what pretty much all other imperative languages do.)

    This here is the best I could come up with while still keeping your original style:

    require 'pp'
    
    data = %w[start before rgb 255 255 255 hex FFFFFF after end]
    
    rgb_count = hex_count = 0
    
    rgb, hex, rest = data.reduce([[], [], []]) do |acc, el|
      acc.tap do |rgb, hex, rest|
        next (rgb_count = 3  ; rgb << el) if /rgb/i =~ el
        next (rgb_count -= 1 ; rgb << el) if rgb_count > 0
        next (hex_count = 1  ; hex << el) if /hex/i =~ el
        next (hex_count -= 1 ; hex << el) if hex_count > 0
        rest << el
      end
    end
    
    data.replace(rest)
    
    pp rgb, hex, data
    # ["rgb", "255", "255", "255"]
    # ["hex", "FFFFFF"]
    # ["start", "before", "after", "end"]
    

    However, what you have is a parsing problem and that should really be solved by a parser. A simple hand-rolled parser/state machine will probably be a little bit more code than the above, but it will be so much more readable.

    Here’s a simple recursive-descent parser that solves your problem:

    class ColorParser
      def initialize(input)
        @input = input.dup
        @rgb, @hex, @data = [], [], []
      end
    
      def parse
        parse_element until @input.empty?
        return @rgb, @hex, @data
      end
    
      private
    
      def parse_element
        parse_color or parse_stop_word
      end
    
      def parse_color
        parse_rgb or parse_hex
      end
    
      def parse_rgb
        return unless /rgb/i =~ peek
        @rgb << consume
        parse_rgb_values
      end
    

    I really like recursive-descent parsers because their structure almost perfectly matches the grammar: just keep parsing elements until the input is empty. What is an element? Well, it’s a color specification or a stop word. What is a color specification? Well, it’s either an RGB color specification or a hex color specification. What is an RGB color specification? Well, it’s something that matches the Regexp /rgb/i followed by RGB values. What are RGB values? Well, it’s just three numbers …

      def parse_rgb_values
        3.times do @rgb << consume.to_i end
      end
    
      def parse_hex
        return unless /hex/i =~ peek
        @hex << consume
        parse_hex_value
      end
    
      def parse_hex_value
        @hex << consume.to_i(16)
      end
    
      def parse_stop_word
        @data << consume unless /rgb|hex/i =~ peek
      end
    
      def consume
        @input.slice!(0)
      end
    
      def peek
        @input.first
      end
    end
    

    Use it like so:

    data = %w[start before rgb 255 255 255 hex FFFFFF after end]
    rgb, hex, rest = ColorParser.new(data).parse
    
    require 'pp'
    
    pp rgb, hex, rest
    # ["rgb", 255, 255, 255]
    # ["hex", 16777215]
    # ["start", "before", "after", "end"]
    

    For comparison, here’s the grammar:

    • S → element*
    • element → color | word
    • color → rgb | hex
    • rgb → rgb rgbvalues
    • rgbvalues → token token token
    • hex → hex hexvalue
    • hexvalue → token
    • word → token
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.