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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:45:18+00:00 2026-06-18T02:45:18+00:00

I saw Ruby 1.9 has a new enumerator, slice_before . The API docs are

  • 0

I saw Ruby 1.9 has a new enumerator, slice_before. The API docs are pretty cryptic.

In particular I’m baffled by the variation that takes an initial_state value.

For example, I want to split an array with numbers into sub-arrays whenever the progressive sum of the elements exceeds some value:

a = [1,2,0,1,2,3]
a.slice_before(0) do |elem, sum|
  sum += elem
  sum > 3
end.to_a

Expected output:

[[1,2,0], [1,2], [3]]

I’m thinking the sum is like a “carry” or “memo” as in inject but that doesn’t seem to pan out.

The glitch in this code is a cryptic error:

TypeError: can't dup Fixnum
from (irb):43:in `each'

It looks like slice_before doesn’t accept a Fixnum as initial value. Why? Ruby bug?.

I can work around this by keeping my own state variable, but it’s not quite the beautiful Ruby semantic I was looking for.

sum = 0
a.slice_before do |elem|
  sum += elem
  sum > 3 && sum = 0
end.to_a

# => [[1, 2, 0], [1, 2], [3]]

So is initial_state usable for this purpose, or not? The examples in the docs seem to be mostly about text processing. I’m using Ruby 1.9.3p194.

  • 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-18T02:45:19+00:00Added an answer on June 18, 2026 at 2:45 am

    The initial_state is typically a state hash that stores key-value pairs.

    To write your code using a state hash:

    a.slice_before(sum: 0) do |elem, state|
      state[:sum] += elem
      if state[:sum] > 3
        # Reset the sum, so we'll get more elements
        state[:sum] = 0  
      end
    end.to_a
    

    The initial_state must respond to the #dup method, because it is duplicated on each loop.

    The reason Fixnum doesn’t work is because it doesn’t respond to #dup. A Fixnum wouldn’t work because it can’t keep track of state on each loop.

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

Sidebar

Related Questions

I'm new to ruby, and I saw this code snippet 1|2 and it returns
I'm looking at some external code and saw a line of Ruby code that
So, I just saw the Ruby library 'Less'. And I want that functionality... Are
Looking around on the Web I found that the Ruby Time class has Time#advance
I just saw that there is a Ruby to Parrot compiler called Cardinal ,
I want to ping a site in my ruby code and saw that net-ping
I read that Ruby has a language featured called labels, how does that work?
I'm new to Ruby, but not to languages that allow lambda's, such as groovy.
Somewhere I saw a way to simplify ruby blocks with one argument, it basically
I saw some websites that (for example): if you want to view your message

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.