On Nov 18, 2007 2:50 PM, steve <oksteev / yahoo.com> wrote:
> Here's my submission:
>
> http://pastie.caboo.se/119486
>
> class String
>   def godelize
>     prime = 0 ; product = 1
>     each_byte do |b|
>       product *= (prime = prime.next_prime) ** (b+1)
>     end
>
>     product
>   end
>
>   def self.from_godel(godel_integer)

I learned about the "self" keyword and adding methods to existing
classes by reading Steve's submission. I modified my own submission to
use these. It is now slightly shorter and significantly easier to use.

# code begins

require 'mathn'

class String
	def to_godel(primes=Prime.new)
		return 1 if size.zero?
		return (primes.succ ** (1 + self[0])) * slice(1,size).to_godel(primes)
	end
	def self.from_godel(num,primes=Prime.new)
		return "" unless num > 1
		prime = primes.next
		multiplicity = factor_multiplicity(prime,num)
		(multiplicity-1).chr + from_godel(num / (prime ** multiplicity), primes)
	end
	private
	def self.factor_multiplicity(factor,num)
		1.upto(num) {|x| return x - 1 unless num.modulo(factor**x).zero?}
	end
end

puts "Test encoding: "+"Ruby\n".to_godel.to_s+"\n"
puts "Test decoding: "+String.from_godel("Ruby\n".to_godel)+"\n"

# code ends

-- 
There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult.

                    - C.A.R. Hoare -