module Sungemm
Constants
- VERSION
Public Class Methods
count(array)
click to toggle source
# File lib/sungemm/methods.rb, line 27 def self.count(array) h = Hash.new(0) array.each { |el| h[el] += 1 } h end
even_sum(arr)
click to toggle source
# File lib/sungemm/methods.rb, line 33 def self.even_sum(arr) arr.each_with_object([]) do |item, obj| if !item.nil? && item.length.even? obj << item.reverse end end end
fibo_finder(n)
click to toggle source
Finds the n-th fibonachi number. @param n [Fixnum] element number in the fibonachi sequence. @return [Fixnum] N-th fibo number.
# File lib/sungemm/methods.rb, line 5 def self.fibo_finder(n) if (0..1).include?(n) n else fibo_finder(n - 2) + fibo_finder(n - 1) end end
join_params(link, url_parameters)
click to toggle source
# File lib/sungemm/methods.rb, line 41 def self.join_params(link, url_parameters) link + "?" + url_parameters.map { |(k, v)| "#{k}=#{v}" }.join("&") end
key_for_min_value(hash)
click to toggle source
# File lib/sungemm/methods.rb, line 102 def self.key_for_min_value(hash) hash.key(hash.values.min) end
longest_palindrome(str)
click to toggle source
# File lib/sungemm/methods.rb, line 13 def self.longest_palindrome(str) len = str.length while len > 0 offset = str.length - len while offset > 0 substr = str[offset, len] return substr if substr == substr.reverse offset -= 1 end len -= 1 end "" end
pretty_date(date)
click to toggle source
# File lib/sungemm/methods.rb, line 45 def self.pretty_date(date) month_strings = %w(January February March April May June July August Sepetember October November December) invalid_date = false case when m = /\A(\d{2})-(\d{2})-(\d{4})\z/.match(date) year = m[3] month_int = m[1].to_i - 1 month = month_strings[month_int][0, 3] day = m[2] when m = /\A(\w+)[ ](\d{1,2}),[ ](\d{4})\z/.match(date) month_match = m[1] if !month_strings.include?(month_match) invalid_date = true else month = month_match[0, 3] year = m[3] day = m[2] end when m = /\A(\d{4})-(\d{2})-(\d{2})\z/.match(date) year = m[1] month_int = m[2].to_i - 1 month = month_strings[month_int][0, 3] day = m[3] when m = %r{\A(\d{1,2})/(\d{2})/(\d{4})\z}.match(date) year = m[3] month_int = m[1].to_i - 1 month = month_strings[month_int][0, 3] day = m[2] else invalid_date = true end raise "Invalid date format" if invalid_date "#{month} #{day}, #{year}" end
random_boolean()
click to toggle source
# File lib/sungemm/methods.rb, line 81 def self.random_boolean rand(2) == 1 end
reverse_each_word(sentence)
click to toggle source
# File lib/sungemm/methods.rb, line 85 def self.reverse_each_word(sentence) sentence.split(" ").map { |x| x.reverse }.join(" ") end
separate_with_comma(number)
click to toggle source
# File lib/sungemm/methods.rb, line 89 def self.separate_with_comma(number) str = number.to_s if str.length > 3 str.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,") else str end end
simple_sub(str, pattern, replace)
click to toggle source
# File lib/sungemm/methods.rb, line 98 def self.simple_sub(str, pattern, replace) str.gsub(pattern, pattern => replace) end