[Ruby 1.9 - Bug #4787][Open] Integer#each_modulo(n)

Issue #4787 has been reported by Kenta M…


Bug #4787: Integer#each_modulo(n)

Author: Kenta M.
Status: Open
Priority: Normal
Assignee: Yukihiro M.
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 each_modulo(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, :each_modulo, n) unless block_given?
q = self
while q > 0
q, r = q.divmod(n)
yield(r)
end
end
end

p 133.each_modulo(3).to_a #=> [1, 2, 2, 1, 1]

The following code is an example use of the feature:

class Integer
def each_thousand_separation
each_modulo(1000)
end

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

sep + s }
end
end

p 100_000_000_200.thousand_separated_string #=> “100,000,000,200”

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

Hi,

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


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

Forwarded Message:

Issue #4787 has been updated by Yukihiro M…

Target version changed from 1.9.3 to 1.9.x


Feature #4787: Integer#each_modulo(n)

Author: Kenta M.
Status: Open
Priority: Normal
Assignee: Yukihiro M.
Category: core
Target version: 1.9.x

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 each_modulo(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, :each_modulo, n) unless block_given?
q = self
while q > 0
q, r = q.divmod(n)
yield(r)
end
end
end

p 133.each_modulo(3).to_a #=> [1, 2, 2, 1, 1]

The following code is an example use of the feature:

class Integer
def each_thousand_separation
each_modulo(1000)
end

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

sep + s }
end
end

p 100_000_000_200.thousand_separated_string #=> “100,000,000,200”

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

Issue #4787 has been updated by Thomas S…

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

Feature #4787: Integer#each_modulo(n)

Author: Kenta M.
Status: Open
Priority: Normal
Assignee: Yukihiro M.
Category: core
Target version: 1.9.x

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 each_modulo(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, :each_modulo, n) unless block_given?
q = self
while q > 0
q, r = q.divmod(n)
yield(r)
end
end
end

p 133.each_modulo(3).to_a #=> [1, 2, 2, 1, 1]

The following code is an example use of the feature:

class Integer
def each_thousand_separation
each_modulo(1000)
end

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

sep + s }
end
end

p 100_000_000_200.thousand_separated_string #=> “100,000,000,200”

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

Issue #4787 has been updated by yhara (Yutaka HARA).

Target version changed from 2.0.0 to next minor


Feature #4787: Integer#each_modulo(n)

Author: mrkn (Kenta M.)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro M.)
Category: core
Target version: next minor

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 each_modulo(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, :each_modulo, n) unless block_given?
q = self
while q > 0
q, r = q.divmod(n)
yield(r)
end
end
end

p 133.each_modulo(3).to_a #=> [1, 2, 2, 1, 1]

The following code is an example use of the feature:

class Integer
def each_thousand_separation
each_modulo(1000)
end

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

sep + s }
end
end

p 100_000_000_200.thousand_separated_string #=> “100,000,000,200”

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