Sha256: e25c3252ac464003e7cbacef0f6b94878b43a7dc2dcba45e7050cc028115ba98

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

module Shepherd
	module Utils
		def yes? msg = "Would you like to proceed?"
			print "#{msg} [Yn]: "
			case $stdin.gets
			when /(y|yes)/i
				true
			when "\n"
				true
			when /(n|no)/i
				false
			else
				false
			end
		end # yes?:Method
		
		# Convert bytes into more human readable format.
		# I have found nice resolution here[http://www.ruby-forum.com/topic/119703]
		#  which was originally written by Jeff Emminger and I just add this part
		#  to automaticly set the unit. Thank you, Jeff!
		# 
		# @param [Integer] number of bytes to be formatted
		# @return [String] a formatted number with the unit appended to it
		def self.nice_bytes number
			units = {:b => 1,
						:kb => 2**10,
						:mb => 2**20,
						:gb => 2**30,
						:tb => 2**40}
			
			unit = :b
			case number
				when 1...2**10
					unit = :b
				when 2**10...2**20
					unit = :kb
				when 2**20...2**30
					unit = :mb
				when 2**30...2**40
					unit = :gb
				when 2**40...2**50
					unit = :tb
			end
			"#{sprintf("%.#{0}f", number / units[unit.to_s.downcase.to_sym])} #{unit.to_s.upcase}"
		end
	end
end

class Integer
	def to_nice
		s = self.to_s

		if s.include? ?.
			pre, post = s.split '.'
			"#{pre.reverse.gsub( /\d{3}(?=\d)/, '\&,' ).reverse}.#{post}"
		else
			s.reverse.gsub( /\d{3}(?=\d)/, '\&.' ).reverse
		end
	end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
shepherd-0.3.0 lib/shepherd/utils.rb
shepherd-0.2.0 lib/shepherd/utils.rb
shepherd-0.1.4 lib/shepherd/utils.rb
shepherd-0.1.3 lib/shepherd/utils.rb