dfhoughton/list_matcher · list_matcher.rb
ruby logo
def pattern( list, opts={} )
      return '(?!)' unless list.any?
      return bud(opts).pattern list unless opts.empty?
      list = list.compact.map(&:to_s).select{ |s| s.length > 0 }
      list.map!(&:strip).select!{ |s| s.length > 0 } if strip
      list.map!{ |s| s.gsub %r/\s++/, ' ' } if normalize_whitespace
      return nil if list.empty?
      specializer = Special.new self, @symbols, list
      list = specializer.normalize

      root = tree list, specializer
      root.root = true
      root.flatten
      rx = root.convert
      if m = modifiers
        rx = "(?#{m}:#{rx})"
        grouped = true
      end
      if name
        rx = "(?<#{name}>#{rx})"
        grouped = true
      end
      return rx if grouped && backtracking
      if atomic && !root.atomic?
        wrap rx
      else
        rx
      end
    end
Similar code snippets
1.
dfhoughton/list_matcher · list_matcher.rb
Match rating: 53.29% · See similar code snippets
ruby logo
def tree(list, specializer)
      if list.size == 1
        leaves = list[0].chars.map do |c|
          specializer[c] || Leaf.new( self, c )
        end
        if leaves.length == 1
          leaves.first
        else
          Sequence.new self, *leaves
        end
      elsif list.all?{ |w| w.length == 1 }
        chars = list.select{ |w| !specializer[w] }
        if chars.size > 1
          list -= chars
          c = CharClass.new self, chars
        end
        a = Alternate.new self, specializer, list unless list.empty?
        a.children.unshift c if a && c
        a || c
      elsif c = best_prefix(list)   # found a fixed-width prefix pattern
        if optional = c[1].include?('')
          c[1].reject!{ |w| w == '' }
        end
        c1 = tree c[0], specializer
        c2 = tree c[1], specializer
        c2 = c2.optionalize optional
        Sequence.new self, c1, c2
      elsif c = best_suffix(list)   # found a fixed-width suffix pattern
        if optional = c[0].include?('')
          c[0].reject!{ |w| w == '' }   # TODO make this faster with index
        end
        c1 = tree c[0], specializer
        c1 = c1.optionalize optional
        c2 = tree c[1], specializer
        Sequence.new self, c1, c2
      else
        grouped = list.group_by{ |w| w[0] }
        chars = grouped.select{ |_, w| w.size == 1 && w[0].size == 1 && !specializer[w[0]] }.map{ |v, _| v }
        if chars.size > 1
          list -= chars
          c = CharClass.new self, chars
        end
        a = Alternate.new self, specializer, list
        a.children.unshift c if c
        a
      end
    end
2.
kschiess/parslet · can_flatten.rb
Match rating: 43.96% · See similar code snippets
ruby logo
def flatten_repetition(list, named)
      if list.any? { |e| e.instance_of?(Hash) }
        # If keyed subtrees are in the array, we'll want to discard all 
        # strings inbetween. To keep them, name them. 
        return list.select { |e| e.instance_of?(Hash) }
      end

      if list.any? { |e| e.instance_of?(Array) }
        # If any arrays are nested in this array, flatten all arrays to this
        # level. 
        return list.
          select { |e| e.instance_of?(Array) }.
          flatten(1)
      end

      # Consistent handling of empty lists, when we act on a named result        
      return [] if named && list.empty?

      # If there are only strings, concatenate them and return that. 
      foldl(list.compact) { |s,e| s+e }
    end
3.
mlj/ruby-sfst · sfst.rb
Match rating: 42.37% · See similar code snippets
ruby logo
def analyze(string, options = {})
      x = []
      @fst._analyze(string) do |a| 
        if options[:symbol_sequence]
          x << a.map { |s| s.match(/^<(.*)>$/) ? $1.to_sym : s }
        else
          x << a.join
        end
      end
      x
    end
4.
mbeasley/ctrl · board.rb
Match rating: 41.61% · See similar code snippets
ruby logo
def print(opts = {})
      cards # get the cards once rather than for each list
      puts ""
      title = "☷ #{name.truncate(80).underline}".wrap({indent: opts[:indent]}).bright
      puts title

      if opts[:lists] && opts[:lists].any?
        valid_lists = opts[:lists].map(&:downcase)
      else
        valid_lists = lists.map(&:name).map(&:downcase)
      end

      lists.
        select { |list| valid_lists.include?(list.name.downcase) }.
        each { |list| list.print(opts.merge({indent: 1})) }
    end
5.
sh8/wunderlist-api · api.rb
Match rating: 40.85% · See similar code snippets
ruby logo
def lists
      res_lists = self.request :get, 'api/v1/lists'
      lists = []
      res_lists.each do |l|
        list = Wunderlist::List.new(l)
        list.api = self
        lists << list
      end

      lists

    end
6.
lsegal/yard · base_helper.rb
Match rating: 40.74% · See similar code snippets
ruby logo
def format_types(list, brackets = true)
      list.nil? || list.empty? ? "" : (brackets ? "(#{list.join(", ")})" : list.join(", "))
    end
7.
sass/ruby-sass · functions.rb
Match rating: 40.55% · See similar code snippets
ruby logo
def zip(*lists)
      length = nil
      values = []
      lists.each do |list|
        array = list.to_a
        values << array.dup
        length = length.nil? ? array.length : [length, array.length].min
      end
      values.each do |value|
        value.slice!(length)
      end
      new_list_value = values.first.zip(*values[1..-1])
      list(new_list_value.map {|list| list(list, :space)}, :comma)
    end
8.
tomdionysus/stringtree-ruby · tree.rb
Match rating: 40.02% · See similar code snippets
ruby logo
def match_count(data, list = {})
      return nil if @root == nil
      i=0
      while (i<data.length)
        node = @root.find_forward(data, i, data.length-i)
        if (node!=nil && node.value!=nil)
          if (!list.has_key?(node)) 
            list[node] = 1
          else
            list[node] += 1
          end
          i += node.length
        else
          i += 1
        end
      end
      list
    end
9.
butchmarshall/active_record_survey · node.rb
Match rating: 39.88% · See similar code snippets
ruby logo
def answers
			nm = self.survey.node_maps

			next_answer_nodes = lambda { |node, list|
				nm.select { |node_map|
					!node_map.parent.nil? && node_map.parent.node == node && node_map.node.class.ancestors.include?(::ActiveRecordSurvey::Node::Answer) && !node_map.marked_for_destruction?
				}.select { |i|
					!list.include?(i.node)
				}.collect { |i|
					i.survey = self.survey
					i.node.survey = self.survey

					list << i.node

					next_answer_nodes.call(i.node, list)
				}.flatten.uniq

				list
			}
			next_answer_nodes.call(self, []).flatten.uniq
		end
10.
sass/ruby-sass · map.rb
Match rating: 39.85% · See similar code snippets
ruby logo
def to_a
      value.map do |k, v|
        list = List.new([k, v], separator: :space)
        list.options = options
        list
      end
    end