From 85553b339acf3f9285a1c03b2fff342d9ddb9b6b Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 11 Jan 2017 16:00:07 +0100 Subject: documentation for all classes --- lib/overwrite.rb | 57 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 11 deletions(-) (limited to 'lib/overwrite.rb') diff --git a/lib/overwrite.rb b/lib/overwrite.rb index 31d30c9..91bc9e1 100644 --- a/lib/overwrite.rb +++ b/lib/overwrite.rb @@ -2,41 +2,51 @@ require "base64" class Object # An object is blank if it's false, empty, or a whitespace string. # For example, "", " ", +nil+, [], and {} are all blank. + # @return [TrueClass,FalseClass] def blank? respond_to?(:empty?) ? empty? : !self end + # Is it a numeric object + # @return [TrueClass,FalseClass] def numeric? true if Float(self) rescue false end # Returns dimension of nested arrays + # @return [Fixnum] def dimension self.class == Array ? 1 + self[0].dimension : 0 end end class Numeric + # Convert number to percent + # @return [Float] def percent_of(n) self.to_f / n.to_f * 100.0 end end class Float - # round to n significant digits - # http://stackoverflow.com/questions/8382619/how-to-round-a-float-to-a-specified-number-of-significant-digits-in-ruby + # Round to n significant digits + # http://stackoverflow.com/questions/8382619/how-to-round-a-float-to-a-specified-number-of-significant-digits-in-ruby + # @param [Fixnum] + # @return [Float] def signif(n) Float("%.#{n}g" % self) end - # converts -10 logarithmized values back + # Convert -10 log values to original values + # @return [Float] def delog10 10**(-1*self) end end module Enumerable - # @return [Array] only the duplicates of an enumerable + # Get duplicates + # @return [Array] def duplicates inject({}) {|h,v| h[v]=h[v].to_i+1; h}.reject{|k,v| v==1}.keys end @@ -51,7 +61,10 @@ module Enumerable end class String - # @return [String] converts camel-case to underscore-case (OpenTox::SuperModel -> open_tox/super_model) + # Convert camel-case to underscore-case + # @example + # OpenTox::SuperModel -> open_tox/super_model + # @return [String] def underscore self.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). @@ -60,7 +73,7 @@ class String downcase end - # convert strings to boolean values + # Convert strings to boolean values # @return [TrueClass,FalseClass] true or false def to_boolean return true if self == true || self =~ (/(true|t|yes|y|1)$/i) @@ -71,7 +84,8 @@ class String end class File - # @return [String] mime_type including charset using linux cmd command + # Get mime_type including charset using linux file command + # @return [String] def mime_type `file -ib '#{self.path}'`.chomp end @@ -79,7 +93,7 @@ end class Array - # Sum up the size of single arrays in an array of arrays + # Sum the size of single arrays in an array of arrays # @param [Array] Array of arrays # @return [Integer] Sum of size of array elements def sum_size @@ -92,33 +106,43 @@ class Array } end - # For symbolic features + # Check if the array has just one unique value. # @param [Array] Array to test. - # @return [Boolean] Whether the array has just one unique value. + # @return [TrueClass,FalseClass] def zero_variance? return self.uniq.size == 1 end + # Get the median of an array + # @return [Numeric] def median sorted = self.sort len = sorted.length (sorted[(len - 1) / 2] + sorted[len / 2]) / 2.0 end + # Get the mean of an array + # @return [Numeric] def mean self.compact.inject{ |sum, el| sum + el }.to_f / self.compact.size end + # Get the variance of an array + # @return [Numeric] def sample_variance m = self.mean sum = self.compact.inject(0){|accum, i| accum +(i-m)**2 } sum/(self.compact.length - 1).to_f end + # Get the standard deviation of an array + # @return [Numeric] def standard_deviation Math.sqrt(self.sample_variance) end + # Convert array values for R + # @return [Array] def for_R if self.first.is_a?(String) #"\"#{self.collect{|v| v.sub('[','').sub(']','')}.join(" ")}\"" # quote and remove square brackets @@ -128,6 +152,8 @@ class Array end end + # Collect array with index + # in analogy to each_with_index def collect_with_index result = [] self.each_with_index do |elt, idx| @@ -139,11 +165,15 @@ end module URI + # Is it a https connection + # @param [String] + # @return [TrueClass,FalseClass] def self.ssl? uri URI.parse(uri).instance_of? URI::HTTPS end - # @return [Boolean] checks if resource exists by making a HEAD-request + # Check if a http resource exists by making a HEAD-request + # @return [TrueClass,FalseClass] def self.accessible?(uri) parsed_uri = URI.parse(uri + (OpenTox::RestClientWrapper.subjectid ? "?subjectid=#{CGI.escape OpenTox::RestClientWrapper.subjectid}" : "")) http_code = URI.task?(uri) ? 600 : 400 @@ -163,6 +193,9 @@ module URI false end + # Is the URI valid + # @param [String] + # @return [TrueClass,FalseClass] def self.valid? uri u = URI.parse(uri) u.scheme!=nil and u.host!=nil @@ -170,6 +203,8 @@ module URI false end + # Is the URI a task URI + # @param [String] def self.task? uri uri =~ /task/ and URI.valid? uri end -- cgit v1.2.3