From 19cdd89260d84da6258049389ee7b0d43f02a749 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Thu, 16 Feb 2017 17:04:53 +0000 Subject: [PATCH] Improved utility files --- array-numbers.rb | 21 +++++++++++++++++++++ primes.rb | 8 ++++++++ 2 files changed, 29 insertions(+) create mode 100644 array-numbers.rb diff --git a/array-numbers.rb b/array-numbers.rb new file mode 100644 index 0000000..26538d6 --- /dev/null +++ b/array-numbers.rb @@ -0,0 +1,21 @@ +# Some utility extensions for helping manipulate numbers +# and lists of digits + +class Array + # Convert a list of digits into a number + def to_i + self.map {|d| d.to_s}.join.to_i + end + + # Convert a two-item list of nubmers into a rational + def to_r + Rational(self[0].to_i, self[1].to_i) + end +end + +class Integer + # Split a number into a list of digits + def to_digits + self.to_s.split('').map {|d| d.to_i} + end +end diff --git a/primes.rb b/primes.rb index 23fd447..7e2cfaa 100644 --- a/primes.rb +++ b/primes.rb @@ -101,3 +101,11 @@ class Primes end + +class Integer + @@primes = Primes.instance + + def prime? + @@primes.include? self + end +end -- 2.34.1