Sha256: 6124f5b1702062088eed6644f2e4b9f1215620c708052e70581790c99cd6d33d

Contents?: true

Size: 1.56 KB

Versions: 5

Compression:

Stored size: 1.56 KB

Contents

# duck typing
class String
  def duck

    begin
      if self == self.to_f.to_s
        return self.to_f
      end
    rescue NoMethodError
    end

    begin
      if self == self.to_i.to_s
        return self.to_i
      end
    rescue NoMethodError
    end

    begin
      if self.gsub(":","") == self.to_datetime.to_s.gsub(
          "T"," ").gsub("+"," +").gsub(":","")
        return self.to_datetime
      end
    rescue Exception
    end

    begin
      if self == self.to_datetime.to_s
        return self.to_datetime
      end
    rescue Exception
    end

    begin
      if self == self.to_date.to_s
        return self.to_date
      end
    rescue Exception
    end

    begin
      if self == "true"
        return true
      end
    rescue NoMethodError
    end


    begin
      if self == "false"
        return false
      end
    rescue NoMethodError
    end

    begin
      string_input= self
      contain= nil
      ["[", "]","^","$","*","/"].each do |one_sym|
        if string_input.include? one_sym
          contain ||= true
        end
      end
      if contain == true
        string_regexp= Regexp.new(string_input).inspect#.gsub('\\','')
        string_regexp= string_regexp[1..(string_regexp.length-2)]
        if string_input == string_regexp
          return Regexp.new(string_input)
        else
          raise ArgumentError,"invalid input string"
        end
      end
    rescue ArgumentError
    end

    begin
      if self == self.to_s.to_s
        return self.to_s
      end
    rescue NoMethodError
    end

  end
  alias_method :to_duck, :duck
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
procemon-0.0.5 lib/procemon/function/str2duck.rb
procemon-0.0.4 lib/procemon/function/str2duck.rb
procemon-0.0.3 lib/procemon/function/str2duck.rb
procemon-0.0.2 lib/procemon/function/str2duck.rb
procemon-0.0.1 lib/procemon/function/str2duck.rb