Sha256: 91a5961158fd0ea0794fa695c5b2f86df4269f622921ba317d059ca05c81289a

Contents?: true

Size: 1.6 KB

Versions: 22

Compression:

Stored size: 1.6 KB

Contents

require File.join 'active_support','time'

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 :to_duck :duck
  alias :to_d    :duck

end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
mpatch-1.2.0 lib/mpatch/active_support/str2duck.rb
mpatch-1.1.0 lib/mpatch/active_support/str2duck.rb
mpatch-1.0.2 lib/mpatch/active_support/str2duck.rb
mpatch-1.0.1 lib/mpatch/active_support/str2duck.rb
mpatch-1.0.0 lib/mpatch/active_support/str2duck.rb
procemon-0.8.1 lib/procemon/extra/str2duck.rb
procemon-0.8.0 lib/procemon/extra/str2duck.rb
procemon-0.7.0 lib/procemon/extra/str2duck.rb
procemon-0.6.3 lib/procemon/extra/str2duck.rb
procemon-0.6.2 lib/procemon/extra/str2duck.rb
procemon-0.6.1 lib/procemon/extra/str2duck.rb
procemon-0.5.0 lib/procemon/extra/str2duck.rb
procemon-0.4.5 lib/procemon/extra/str2duck.rb
procemon-0.4.4 lib/procemon/extra/str2duck.rb
procemon-0.4.3 lib/procemon/extra/str2duck.rb
procemon-0.4.2 lib/procemon/extra/str2duck.rb
procemon-0.4.1 lib/procemon/extra/str2duck.rb
procemon-0.4.0 lib/procemon/extra/str2duck.rb
procemon-0.3.4 lib/procemon/extra/str2duck.rb
procemon-0.3.3 lib/procemon/extra/str2duck.rb