Sha256: 4e324338fa4315a71b3f3d711cbfe14ebe6e761533159348d2b360e933c5c47f

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require_relative 'binary/extensions.rb'
require 'binary/version'
require 'prime'

module Binary
  def self.binary(i=nil)
    return nil if block_given?
    return i.map { |n| n.class == Integer ? n.abs.to_s(2) : nil } if array? i
    i = i.abs if i.to_i < 0
    return i.to_s(2) if i.class == Integer
    nil
  end

  def self.number(i=nil)
    return i.map { |n| n.class == String ? n.to_i(2) : nil } if array? i
    return i.to_i(2) if i.class == String
    nil
  end

  def self.array?(i)
  	i.class == Array
  end

  def self.bits num=nil
    return nil unless num
    binary(num).length
  end

  def self.zeros num=nil
    return nil unless num
    binary(num).count("0")
  end

  def self.ones num=nil
    return nil unless num
    binary(num).count("1")
  end

  def self.prime num=nil
    return nil unless num
    binaries = []
    binaries = Prime.each(num) { |prime| binaries << binary(prime) }
  end

  def self.method_missing(method, *args, &block)
    puts "No method was found with the name #{method}."
    puts 'Methods available are:'
    puts (Binary.methods - Object.methods - [:method_missing])
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
binary-1.3.1 lib/binary.rb