#coding: utf-8 require 'fileutils' module Ns module Util extend self def concat_url(prefix_url='', url='') prefix_url = prefix_url.to_s sep = (prefix_url[-1, 1] == '/' || url[0, 1] == '/') ? '' : '/' "#{prefix_url}#{sep}#{url}" end #将一个hash和array写入data下的yaml结构中 def self.yamlize(hash_or_array = {}, file_name = nil) return if hash_or_array.nil? hash = if hash_or_array.is_a?(Array) hash_or_array.inject({}) do |r, k| r[k] = nil r end else hash_or_array end method_key = file_name.nil? ? "hash" : file_name.to_s name = "#{method_key}_#{Time.now.to_i}" file = "#{Rails.root}/data/#{name}.yml" path = File.dirname(file) FileUtils.mkpath(path) unless File.directory?(path) File.open(file, 'w+') do |f| f.puts YAML.dump(method_key.to_sym=>hash) end puts "Generate a yaml file in #{file}" file end end end