Feature #4787

Integer#each_modulo(n)

Added by Kenta Murata almost 3 years ago. Updated over 1 year ago.

[ruby-dev:43586]
Status:Assigned
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:core
Target version:next minor

Description

I suggest a new feature of Integer to enumerate by iterated Integer#modulo.
An example implementation in Ruby is the following code:

class Integer
def eachmodulo(n)
raise ArgumentError, "argument must be an Integer" unless n.is
a? Integer
raise ArgumentError, "argument must be larger than 1" if n <= 1
return Enumerator.new(self, :eachmodulo, n) unless blockgiven?
q = self
while q > 0
q, r = q.divmod(n)
yield(r)
end
end
end

p 133.eachmodulo(3).toa #=> [1, 2, 2, 1, 1]

The following code is an example use of the feature:

class Integer
def eachthousandseparation
each_modulo(1000)
end

def thousand_separated_string(sep=',')
  each_thousand_separation.map(&'%03d'.method(:%)).inject{|s, n| n + sep + s }
end

end

p 100000000200.thousandseparated_string #=> "100,000,000,200"

I make an implementation in C, and attach the patch for that.

each_modulo.patch Magnifier (3.91 KB) Kenta Murata, 05/27/2011 01:18 PM

History

#1 Updated by Kenta Murata almost 3 years ago

  • ruby -v changed from ruby 1.9.3dev (2011-05-27 trunk 31745) [x86_64-darwin10.7.0] to -

Hi,

I believe I've sent the following feature request to ruby-core,
but I sent to ruby-dev by mistake.

--
Kenta Murata
Sent with Sparrow (http://www.sparrowmailapp.com)

Forwarded Message:

From: Kenta Murata muraken@gmail.com
In Reply To: ruby-dev@ruby-lang.org
To: ruby developers list ruby-dev@ruby-lang.org
Date: 2011-05-27 13:21:06
Subject: [Ruby 1.9 - Bug #4787][Open] Integer#each_modulo(n)

Issue #4787 has been reported by Kenta Murata.


Bug #4787: Integer#each_modulo(n)
http://redmine.ruby-lang.org/issues/4787

Author: Kenta Murata
Status: Open
Priority: Normal
Assignee: Yukihiro Matsumoto
Category: core
Target version: 1.9.3
ruby -v: ruby 1.9.3dev (2011-05-27 trunk 31745) [x86_64-darwin10.7.0]

I suggest a new feature of Integer to enumerate by iterated Integer#modulo.
An example implementation in Ruby is the following code:

class Integer
def eachmodulo(n)
raise ArgumentError, "argument must be an Integer" unless n.is
a? Integer
raise ArgumentError, "argument must be larger than 1" if n <= 1
return Enumerator.new(self, :eachmodulo, n) unless blockgiven?
q = self
while q > 0
q, r = q.divmod(n)
yield(r)
end
end
end

p 133.eachmodulo(3).toa #=> [1, 2, 2, 1, 1]

The following code is an example use of the feature:

class Integer
def eachthousandseparation
each_modulo(1000)
end

def thousandseparatedstring(sep=',')
eachthousandseparation.map(&'%03d'.method(:%)).inject{|s, n| n + sep + s }
end
end

p 100000000200.thousandseparated_string #=> "100,000,000,200"

I make an implementation in C, and attach the patch for that.

http://redmine.ruby-lang.org

#2 Updated by Nobuyoshi Nakada almost 3 years ago

  • Tracker changed from Bug to Feature

#3 Updated by Yukihiro Matsumoto almost 3 years ago

  • Target version changed from 1.9.3 to 2.0.0

#4 Updated by Thomas Sawyer almost 3 years ago

Why not just let #modulo/#divmod take a block, rather than define a new method?

#5 Updated by Shyouhei Urabe about 2 years ago

  • Status changed from Open to Assigned

#6 Updated by Yutaka HARA over 1 year ago

  • Target version changed from 2.0.0 to next minor

Also available in: Atom PDF