id
int32
0
24.9k
repo
stringlengths
5
58
path
stringlengths
9
168
func_name
stringlengths
9
130
original_string
stringlengths
66
10.5k
language
stringclasses
1 value
code
stringlengths
66
10.5k
code_tokens
sequence
docstring
stringlengths
8
16k
docstring_tokens
sequence
sha
stringlengths
40
40
url
stringlengths
94
266
2,400
famished-tiger/Rley
lib/rley/parse_forest_visitor.rb
Rley.ParseForestVisitor.traverse_children
def traverse_children(aParentNode) children = aParentNode.children broadcast(:before_subnodes, aParentNode, children) # Let's proceed with the visit of children children.each_with_index do |a_node, i| edge_sign = aParentNode.signatures[i] if a_node.kind_of?(SPPF::CompositeNode) push_node(a_node, edge_sign) access_paths = node_accesses[a_node] last_path = legs.last[-1] path_reused = access_paths.include?(last_path) unless path_reused node_accesses[a_node].push(last_path) a_node.accept(self) end pop_node else a_node.accept(self) end end broadcast(:after_subnodes, aParentNode, children) end
ruby
def traverse_children(aParentNode) children = aParentNode.children broadcast(:before_subnodes, aParentNode, children) # Let's proceed with the visit of children children.each_with_index do |a_node, i| edge_sign = aParentNode.signatures[i] if a_node.kind_of?(SPPF::CompositeNode) push_node(a_node, edge_sign) access_paths = node_accesses[a_node] last_path = legs.last[-1] path_reused = access_paths.include?(last_path) unless path_reused node_accesses[a_node].push(last_path) a_node.accept(self) end pop_node else a_node.accept(self) end end broadcast(:after_subnodes, aParentNode, children) end
[ "def", "traverse_children", "(", "aParentNode", ")", "children", "=", "aParentNode", ".", "children", "broadcast", "(", ":before_subnodes", ",", "aParentNode", ",", "children", ")", "# Let's proceed with the visit of children", "children", ".", "each_with_index", "do", "|", "a_node", ",", "i", "|", "edge_sign", "=", "aParentNode", ".", "signatures", "[", "i", "]", "if", "a_node", ".", "kind_of?", "(", "SPPF", "::", "CompositeNode", ")", "push_node", "(", "a_node", ",", "edge_sign", ")", "access_paths", "=", "node_accesses", "[", "a_node", "]", "last_path", "=", "legs", ".", "last", "[", "-", "1", "]", "path_reused", "=", "access_paths", ".", "include?", "(", "last_path", ")", "unless", "path_reused", "node_accesses", "[", "a_node", "]", ".", "push", "(", "last_path", ")", "a_node", ".", "accept", "(", "self", ")", "end", "pop_node", "else", "a_node", ".", "accept", "(", "self", ")", "end", "end", "broadcast", "(", ":after_subnodes", ",", "aParentNode", ",", "children", ")", "end" ]
Visit event. The visitor is about to visit the children of a non terminal node. @param aParentNode [NonTeminalNode] the (non-terminal) parent node.
[ "Visit", "event", ".", "The", "visitor", "is", "about", "to", "visit", "the", "children", "of", "a", "non", "terminal", "node", "." ]
307a8ed6a328182c3cb83a0c3ed04c04b7135a13
https://github.com/famished-tiger/Rley/blob/307a8ed6a328182c3cb83a0c3ed04c04b7135a13/lib/rley/parse_forest_visitor.rb#L139-L162
2,401
jarib/celerity
lib/celerity/element_collection.rb
Celerity.ElementCollection.[]
def [](n) if @elements && @elements[n - Celerity.index_offset] element_class.new(@container, :object, @elements[n - Celerity.index_offset]) else iterator_object(n - Celerity.index_offset) end end
ruby
def [](n) if @elements && @elements[n - Celerity.index_offset] element_class.new(@container, :object, @elements[n - Celerity.index_offset]) else iterator_object(n - Celerity.index_offset) end end
[ "def", "[]", "(", "n", ")", "if", "@elements", "&&", "@elements", "[", "n", "-", "Celerity", ".", "index_offset", "]", "element_class", ".", "new", "(", "@container", ",", ":object", ",", "@elements", "[", "n", "-", "Celerity", ".", "index_offset", "]", ")", "else", "iterator_object", "(", "n", "-", "Celerity", ".", "index_offset", ")", "end", "end" ]
Get the element at the given index. By default, this is 1-indexed to keep compatibility with Watir. Also note that because of Watir's lazy loading, this will return an Element instance even if the index is out of bounds. @param [Fixnum] n Index of wanted element, 1-indexed unless Celerity.index_offset is changed. @return [Celerity::Element] Returns a subclass of Celerity::Element
[ "Get", "the", "element", "at", "the", "given", "index", ".", "By", "default", "this", "is", "1", "-", "indexed", "to", "keep", "compatibility", "with", "Watir", "." ]
90c6eeb3db868be1d9c0195b53f040b5b53d0e91
https://github.com/jarib/celerity/blob/90c6eeb3db868be1d9c0195b53f040b5b53d0e91/lib/celerity/element_collection.rb#L68-L74
2,402
jarib/celerity
lib/celerity/elements/select_list.rb
Celerity.SelectList.selected?
def selected?(value) assert_exists raise UnknownObjectException, "unknown option with value #{value.inspect} for select_list #{@conditions.inspect}" unless include?(value) [email protected] { |e| matches_option?(e, value) && e.isSelected } end
ruby
def selected?(value) assert_exists raise UnknownObjectException, "unknown option with value #{value.inspect} for select_list #{@conditions.inspect}" unless include?(value) [email protected] { |e| matches_option?(e, value) && e.isSelected } end
[ "def", "selected?", "(", "value", ")", "assert_exists", "raise", "UnknownObjectException", ",", "\"unknown option with value #{value.inspect} for select_list #{@conditions.inspect}\"", "unless", "include?", "(", "value", ")", "!", "!", "@object", ".", "getOptions", ".", "find", "{", "|", "e", "|", "matches_option?", "(", "e", ",", "value", ")", "&&", "e", ".", "isSelected", "}", "end" ]
Returns true if any of the selected options match the given value. @param [String, Regexp] value A value. @raise [Celerity::Exception::UnknownObjectException] if the value does not exist. @return [true, false]
[ "Returns", "true", "if", "any", "of", "the", "selected", "options", "match", "the", "given", "value", "." ]
90c6eeb3db868be1d9c0195b53f040b5b53d0e91
https://github.com/jarib/celerity/blob/90c6eeb3db868be1d9c0195b53f040b5b53d0e91/lib/celerity/elements/select_list.rb#L99-L103
2,403
jarib/celerity
lib/celerity/container.rb
Celerity.Container.contains_text
def contains_text(expected_text) assert_exists return nil unless respond_to? :text case expected_text when Regexp text() =~ expected_text when String text().index(expected_text) else raise TypeError, "expected String or Regexp, got #{expected_text.inspect}:#{expected_text.class}" end end
ruby
def contains_text(expected_text) assert_exists return nil unless respond_to? :text case expected_text when Regexp text() =~ expected_text when String text().index(expected_text) else raise TypeError, "expected String or Regexp, got #{expected_text.inspect}:#{expected_text.class}" end end
[ "def", "contains_text", "(", "expected_text", ")", "assert_exists", "return", "nil", "unless", "respond_to?", ":text", "case", "expected_text", "when", "Regexp", "text", "(", ")", "=~", "expected_text", "when", "String", "text", "(", ")", ".", "index", "(", "expected_text", ")", "else", "raise", "TypeError", ",", "\"expected String or Regexp, got #{expected_text.inspect}:#{expected_text.class}\"", "end", "end" ]
Check if the element contains the given text. @param [String, Regexp] expected_text The text to look for. @return [Fixnum, nil] The index of the matched text, or nil if it doesn't match.
[ "Check", "if", "the", "element", "contains", "the", "given", "text", "." ]
90c6eeb3db868be1d9c0195b53f040b5b53d0e91
https://github.com/jarib/celerity/blob/90c6eeb3db868be1d9c0195b53f040b5b53d0e91/lib/celerity/container.rb#L45-L57
2,404
famished-tiger/Rley
lib/rley/engine.rb
Rley.Engine.parse
def parse(aTokenizer) tokens = [] aTokenizer.each do |a_token| next unless a_token term_name = a_token.terminal term_symb = grammar.name2symbol[term_name] a_token.instance_variable_set(:@terminal, term_symb) tokens << a_token end parser = build_parser(grammar) parser.gf_graph.diagnose if configuration.diagnose result = parser.parse(tokens) result.tidy_up! return result end
ruby
def parse(aTokenizer) tokens = [] aTokenizer.each do |a_token| next unless a_token term_name = a_token.terminal term_symb = grammar.name2symbol[term_name] a_token.instance_variable_set(:@terminal, term_symb) tokens << a_token end parser = build_parser(grammar) parser.gf_graph.diagnose if configuration.diagnose result = parser.parse(tokens) result.tidy_up! return result end
[ "def", "parse", "(", "aTokenizer", ")", "tokens", "=", "[", "]", "aTokenizer", ".", "each", "do", "|", "a_token", "|", "next", "unless", "a_token", "term_name", "=", "a_token", ".", "terminal", "term_symb", "=", "grammar", ".", "name2symbol", "[", "term_name", "]", "a_token", ".", "instance_variable_set", "(", ":@terminal", ",", "term_symb", ")", "tokens", "<<", "a_token", "end", "parser", "=", "build_parser", "(", "grammar", ")", "parser", ".", "gf_graph", ".", "diagnose", "if", "configuration", ".", "diagnose", "result", "=", "parser", ".", "parse", "(", "tokens", ")", "result", ".", "tidy_up!", "return", "result", "end" ]
Parse the sequence of tokens produced by the given tokenizer object. @param aTokenizer [#each] @return [Parser::GFGParsing]
[ "Parse", "the", "sequence", "of", "tokens", "produced", "by", "the", "given", "tokenizer", "object", "." ]
307a8ed6a328182c3cb83a0c3ed04c04b7135a13
https://github.com/famished-tiger/Rley/blob/307a8ed6a328182c3cb83a0c3ed04c04b7135a13/lib/rley/engine.rb#L78-L94
2,405
famished-tiger/Rley
lib/rley/engine.rb
Rley.Engine.to_ptree
def to_ptree(aRawParse) factory = ParseRep::ParseTreeFactory.new(aRawParse) if configuration.repr_builder == :default result = factory.create(nil) else result = factory.create(configuration.repr_builder) end return result end
ruby
def to_ptree(aRawParse) factory = ParseRep::ParseTreeFactory.new(aRawParse) if configuration.repr_builder == :default result = factory.create(nil) else result = factory.create(configuration.repr_builder) end return result end
[ "def", "to_ptree", "(", "aRawParse", ")", "factory", "=", "ParseRep", "::", "ParseTreeFactory", ".", "new", "(", "aRawParse", ")", "if", "configuration", ".", "repr_builder", "==", ":default", "result", "=", "factory", ".", "create", "(", "nil", ")", "else", "result", "=", "factory", ".", "create", "(", "configuration", ".", "repr_builder", ")", "end", "return", "result", "end" ]
Convert raw parse result into a parse tree representation @param aRawParse [Parser::GFGParsing] @return [Rley::PTree::ParseTree]
[ "Convert", "raw", "parse", "result", "into", "a", "parse", "tree", "representation" ]
307a8ed6a328182c3cb83a0c3ed04c04b7135a13
https://github.com/famished-tiger/Rley/blob/307a8ed6a328182c3cb83a0c3ed04c04b7135a13/lib/rley/engine.rb#L114-L123
2,406
famished-tiger/Rley
lib/rley/engine.rb
Rley.Engine.to_pforest
def to_pforest(aRawParse) factory = ParseRep::ParseForestFactory.new(aRawParse) if configuration.repr_builder == :default result = factory.create(nil) else result = factory.create(configuration.repr_builder) end return result end
ruby
def to_pforest(aRawParse) factory = ParseRep::ParseForestFactory.new(aRawParse) if configuration.repr_builder == :default result = factory.create(nil) else result = factory.create(configuration.repr_builder) end return result end
[ "def", "to_pforest", "(", "aRawParse", ")", "factory", "=", "ParseRep", "::", "ParseForestFactory", ".", "new", "(", "aRawParse", ")", "if", "configuration", ".", "repr_builder", "==", ":default", "result", "=", "factory", ".", "create", "(", "nil", ")", "else", "result", "=", "factory", ".", "create", "(", "configuration", ".", "repr_builder", ")", "end", "return", "result", "end" ]
Convert raw parse result into a parse forest representation @param aRawParse [Parser::GFGParsing] @return [Rley::SPPF::ParseForest]
[ "Convert", "raw", "parse", "result", "into", "a", "parse", "forest", "representation" ]
307a8ed6a328182c3cb83a0c3ed04c04b7135a13
https://github.com/famished-tiger/Rley/blob/307a8ed6a328182c3cb83a0c3ed04c04b7135a13/lib/rley/engine.rb#L128-L137
2,407
jarib/celerity
lib/celerity/element_locator.rb
Celerity.ElementLocator.with_nullpointer_retry
def with_nullpointer_retry(max_retries = 3) tries = 0 yield rescue java.lang.NullPointerException => e raise e if tries >= max_retries tries += 1 warn "warning: celerity caught #{e} - retry ##{tries}" retry end
ruby
def with_nullpointer_retry(max_retries = 3) tries = 0 yield rescue java.lang.NullPointerException => e raise e if tries >= max_retries tries += 1 warn "warning: celerity caught #{e} - retry ##{tries}" retry end
[ "def", "with_nullpointer_retry", "(", "max_retries", "=", "3", ")", "tries", "=", "0", "yield", "rescue", "java", ".", "lang", ".", "NullPointerException", "=>", "e", "raise", "e", "if", "tries", ">=", "max_retries", "tries", "+=", "1", "warn", "\"warning: celerity caught #{e} - retry ##{tries}\"", "retry", "end" ]
HtmlUnit throws NPEs sometimes when we're locating elements Retry seems to work fine.
[ "HtmlUnit", "throws", "NPEs", "sometimes", "when", "we", "re", "locating", "elements", "Retry", "seems", "to", "work", "fine", "." ]
90c6eeb3db868be1d9c0195b53f040b5b53d0e91
https://github.com/jarib/celerity/blob/90c6eeb3db868be1d9c0195b53f040b5b53d0e91/lib/celerity/element_locator.rb#L152-L161
2,408
jarib/celerity
lib/celerity/clickable_element.rb
Celerity.ClickableElement.click_and_attach
def click_and_attach assert_exists_and_enabled browser = Browser.new(@browser.options.dup) browser.webclient.set_cookie_manager( @browser.webclient.get_cookie_manager ) # hirobumi: we do want cookies as well. @browser.disable_event_listener do rescue_status_code_exception { browser.page = @object.click } end browser end
ruby
def click_and_attach assert_exists_and_enabled browser = Browser.new(@browser.options.dup) browser.webclient.set_cookie_manager( @browser.webclient.get_cookie_manager ) # hirobumi: we do want cookies as well. @browser.disable_event_listener do rescue_status_code_exception { browser.page = @object.click } end browser end
[ "def", "click_and_attach", "assert_exists_and_enabled", "browser", "=", "Browser", ".", "new", "(", "@browser", ".", "options", ".", "dup", ")", "browser", ".", "webclient", ".", "set_cookie_manager", "(", "@browser", ".", "webclient", ".", "get_cookie_manager", ")", "# hirobumi: we do want cookies as well.", "@browser", ".", "disable_event_listener", "do", "rescue_status_code_exception", "{", "browser", ".", "page", "=", "@object", ".", "click", "}", "end", "browser", "end" ]
Click the element and return a new Browser instance with the resulting page. This is useful for elements that trigger popups when clicked. @return [Celerity::Browser]
[ "Click", "the", "element", "and", "return", "a", "new", "Browser", "instance", "with", "the", "resulting", "page", ".", "This", "is", "useful", "for", "elements", "that", "trigger", "popups", "when", "clicked", "." ]
90c6eeb3db868be1d9c0195b53f040b5b53d0e91
https://github.com/jarib/celerity/blob/90c6eeb3db868be1d9c0195b53f040b5b53d0e91/lib/celerity/clickable_element.rb#L38-L50
2,409
potatosalad/ruby-erlang-terms
lib/erlang/map.rb
Erlang.Map.value?
def value?(value) value = Erlang.from(value) each { |k,v| return true if value == v } return false end
ruby
def value?(value) value = Erlang.from(value) each { |k,v| return true if value == v } return false end
[ "def", "value?", "(", "value", ")", "value", "=", "Erlang", ".", "from", "(", "value", ")", "each", "{", "|", "k", ",", "v", "|", "return", "true", "if", "value", "==", "v", "}", "return", "false", "end" ]
Return `true` if this `Map` has one or more keys which map to the provided value. @example Erlang::Map["A" => 1, "B" => 2, "C" => 3].value?(2) # => true @param value [Object] The value to check for @return [Boolean]
[ "Return", "true", "if", "this", "Map", "has", "one", "or", "more", "keys", "which", "map", "to", "the", "provided", "value", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/map.rb#L218-L222
2,410
potatosalad/ruby-erlang-terms
lib/erlang/map.rb
Erlang.Map.get
def get(key) key = Erlang.from(key) entry = @trie.get(key) if entry return entry[1] elsif @default return @default.call(key) end end
ruby
def get(key) key = Erlang.from(key) entry = @trie.get(key) if entry return entry[1] elsif @default return @default.call(key) end end
[ "def", "get", "(", "key", ")", "key", "=", "Erlang", ".", "from", "(", "key", ")", "entry", "=", "@trie", ".", "get", "(", "key", ")", "if", "entry", "return", "entry", "[", "1", "]", "elsif", "@default", "return", "@default", ".", "call", "(", "key", ")", "end", "end" ]
Retrieve the value corresponding to the provided key object. If not found, and this `Map` has a default block, the default block is called to provide the value. Otherwise, return `nil`. @example m = Erlang::Map["A" => 1, "B" => 2, "C" => 3] m["B"] # => 2 m.get("B") # => 2 m.get("Elephant") # => nil # Erlang Map with a default proc: m = Erlang::Map.new("A" => 1, "B" => 2, "C" => 3) { |key| key.size } m.get("B") # => 2 m.get("Elephant") # => 8 @param key [Object] The key to look up @return [Object]
[ "Retrieve", "the", "value", "corresponding", "to", "the", "provided", "key", "object", ".", "If", "not", "found", "and", "this", "Map", "has", "a", "default", "block", "the", "default", "block", "is", "called", "to", "provide", "the", "value", ".", "Otherwise", "return", "nil", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/map.rb#L242-L250
2,411
potatosalad/ruby-erlang-terms
lib/erlang/map.rb
Erlang.Map.fetch
def fetch(key, default = Undefined) key = Erlang.from(key) entry = @trie.get(key) if entry return entry[1] elsif block_given? return yield(key) elsif not Undefined.equal?(default) return Erlang.from(default) else raise KeyError, "key not found: #{key.inspect}" end end
ruby
def fetch(key, default = Undefined) key = Erlang.from(key) entry = @trie.get(key) if entry return entry[1] elsif block_given? return yield(key) elsif not Undefined.equal?(default) return Erlang.from(default) else raise KeyError, "key not found: #{key.inspect}" end end
[ "def", "fetch", "(", "key", ",", "default", "=", "Undefined", ")", "key", "=", "Erlang", ".", "from", "(", "key", ")", "entry", "=", "@trie", ".", "get", "(", "key", ")", "if", "entry", "return", "entry", "[", "1", "]", "elsif", "block_given?", "return", "yield", "(", "key", ")", "elsif", "not", "Undefined", ".", "equal?", "(", "default", ")", "return", "Erlang", ".", "from", "(", "default", ")", "else", "raise", "KeyError", ",", "\"key not found: #{key.inspect}\"", "end", "end" ]
Retrieve the value corresponding to the given key object, or use the provided default value or block, or otherwise raise a `KeyError`. @overload fetch(key) Retrieve the value corresponding to the given key, or raise a `KeyError` if it is not found. @param key [Object] The key to look up @overload fetch(key) { |key| ... } Retrieve the value corresponding to the given key, or call the optional code block (with the missing key) and get its return value. @yield [key] The key which was not found @yieldreturn [Object] Object to return since the key was not found @param key [Object] The key to look up @overload fetch(key, default) Retrieve the value corresponding to the given key, or else return the provided `default` value. @param key [Object] The key to look up @param default [Object] Object to return if the key is not found @example m = Erlang::Map["A" => 1, "B" => 2, "C" => 3] m.fetch("B") # => 2 m.fetch("Elephant") # => KeyError: key not found: "Elephant" # with a default value: m.fetch("B", 99) # => 2 m.fetch("Elephant", 99) # => 99 # with a block: m.fetch("B") { |key| key.size } # => 2 m.fetch("Elephant") { |key| key.size } # => 8 @return [Object]
[ "Retrieve", "the", "value", "corresponding", "to", "the", "given", "key", "object", "or", "use", "the", "provided", "default", "value", "or", "block", "or", "otherwise", "raise", "a", "KeyError", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/map.rb#L286-L298
2,412
potatosalad/ruby-erlang-terms
lib/erlang/map.rb
Erlang.Map.slice
def slice(*wanted) trie = Trie.new(0) wanted.each { |key| key = Erlang.from(key) trie.put!(key, get(key)) if key?(key) } return self.class.alloc(trie, @default) end
ruby
def slice(*wanted) trie = Trie.new(0) wanted.each { |key| key = Erlang.from(key) trie.put!(key, get(key)) if key?(key) } return self.class.alloc(trie, @default) end
[ "def", "slice", "(", "*", "wanted", ")", "trie", "=", "Trie", ".", "new", "(", "0", ")", "wanted", ".", "each", "{", "|", "key", "|", "key", "=", "Erlang", ".", "from", "(", "key", ")", "trie", ".", "put!", "(", "key", ",", "get", "(", "key", ")", ")", "if", "key?", "(", "key", ")", "}", "return", "self", ".", "class", ".", "alloc", "(", "trie", ",", "@default", ")", "end" ]
Return a new `Map` with only the associations for the `wanted` keys retained. @example m = Erlang::Map["A" => 1, "B" => 2, "C" => 3] m.slice("B", "C") # => Erlang::Map["B" => 2, "C" => 3] @param wanted [::Enumerable] The keys to retain @return [Map]
[ "Return", "a", "new", "Map", "with", "only", "the", "associations", "for", "the", "wanted", "keys", "retained", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/map.rb#L621-L628
2,413
bbc/hive-runner
lib/hive/worker.rb
Hive.Worker.poll_queue
def poll_queue @job = reserve_job if @job.nil? @log.info('No job found') else @log.info('Job starting') begin @current_job_start_time = Time.now execute_job rescue => e @log.info("Error running test: #{e.message}\n : #{e.backtrace.join("\n :")}") end cleanup end end
ruby
def poll_queue @job = reserve_job if @job.nil? @log.info('No job found') else @log.info('Job starting') begin @current_job_start_time = Time.now execute_job rescue => e @log.info("Error running test: #{e.message}\n : #{e.backtrace.join("\n :")}") end cleanup end end
[ "def", "poll_queue", "@job", "=", "reserve_job", "if", "@job", ".", "nil?", "@log", ".", "info", "(", "'No job found'", ")", "else", "@log", ".", "info", "(", "'Job starting'", ")", "begin", "@current_job_start_time", "=", "Time", ".", "now", "execute_job", "rescue", "=>", "e", "@log", ".", "info", "(", "\"Error running test: #{e.message}\\n : #{e.backtrace.join(\"\\n :\")}\"", ")", "end", "cleanup", "end", "end" ]
The main worker process loop Check the queues for work
[ "The", "main", "worker", "process", "loop", "Check", "the", "queues", "for", "work" ]
9079f23362e201c342f416b5038d935e83e0de84
https://github.com/bbc/hive-runner/blob/9079f23362e201c342f416b5038d935e83e0de84/lib/hive/worker.rb#L89-L103
2,414
bbc/hive-runner
lib/hive/worker.rb
Hive.Worker.reserve_job
def reserve_job @log.info "Trying to reserve job for queues: #{@queues.join(', ')}" job = job_message_klass.reserve(@queues, reservation_details) @log.debug "Job: #{job.inspect}" raise InvalidJobReservationError.new("Invalid Job Reserved") if ! (job.nil? || job.valid?) job end
ruby
def reserve_job @log.info "Trying to reserve job for queues: #{@queues.join(', ')}" job = job_message_klass.reserve(@queues, reservation_details) @log.debug "Job: #{job.inspect}" raise InvalidJobReservationError.new("Invalid Job Reserved") if ! (job.nil? || job.valid?) job end
[ "def", "reserve_job", "@log", ".", "info", "\"Trying to reserve job for queues: #{@queues.join(', ')}\"", "job", "=", "job_message_klass", ".", "reserve", "(", "@queues", ",", "reservation_details", ")", "@log", ".", "debug", "\"Job: #{job.inspect}\"", "raise", "InvalidJobReservationError", ".", "new", "(", "\"Invalid Job Reserved\"", ")", "if", "!", "(", "job", ".", "nil?", "||", "job", ".", "valid?", ")", "job", "end" ]
Try to find and reserve a job
[ "Try", "to", "find", "and", "reserve", "a", "job" ]
9079f23362e201c342f416b5038d935e83e0de84
https://github.com/bbc/hive-runner/blob/9079f23362e201c342f416b5038d935e83e0de84/lib/hive/worker.rb#L106-L112
2,415
bbc/hive-runner
lib/hive/worker.rb
Hive.Worker.diagnostics
def diagnostics retn = true protect retn = @diagnostic_runner.run if !@diagnostic_runner.nil? unprotect @log.info('Diagnostics failed') if not retn status = device_status status = set_device_status('happy') if status == 'busy' raise DeviceNotReady.new("Current device status: '#{status}'") if status != 'happy' retn end
ruby
def diagnostics retn = true protect retn = @diagnostic_runner.run if !@diagnostic_runner.nil? unprotect @log.info('Diagnostics failed') if not retn status = device_status status = set_device_status('happy') if status == 'busy' raise DeviceNotReady.new("Current device status: '#{status}'") if status != 'happy' retn end
[ "def", "diagnostics", "retn", "=", "true", "protect", "retn", "=", "@diagnostic_runner", ".", "run", "if", "!", "@diagnostic_runner", ".", "nil?", "unprotect", "@log", ".", "info", "(", "'Diagnostics failed'", ")", "if", "not", "retn", "status", "=", "device_status", "status", "=", "set_device_status", "(", "'happy'", ")", "if", "status", "==", "'busy'", "raise", "DeviceNotReady", ".", "new", "(", "\"Current device status: '#{status}'\"", ")", "if", "status", "!=", "'happy'", "retn", "end" ]
Diagnostics function to be extended in child class, as required
[ "Diagnostics", "function", "to", "be", "extended", "in", "child", "class", "as", "required" ]
9079f23362e201c342f416b5038d935e83e0de84
https://github.com/bbc/hive-runner/blob/9079f23362e201c342f416b5038d935e83e0de84/lib/hive/worker.rb#L251-L261
2,416
bbc/hive-runner
lib/hive/worker.rb
Hive.Worker.upload_files
def upload_files(job, *paths) @log.info("Uploading assets") paths.each do |path| @log.info("Uploading files from #{path}") Dir.foreach(path) do |item| @log.info("File: #{item}") next if item == '.' or item == '..' begin artifact = job.report_artifact("#{path}/#{item}") @log.info("Artifact uploaded: #{artifact.attributes.to_s}") rescue => e @log.error("Error uploading artifact #{item}: #{e.message}") @log.error(" : #{e.backtrace.join("\n : ")}") end end end end
ruby
def upload_files(job, *paths) @log.info("Uploading assets") paths.each do |path| @log.info("Uploading files from #{path}") Dir.foreach(path) do |item| @log.info("File: #{item}") next if item == '.' or item == '..' begin artifact = job.report_artifact("#{path}/#{item}") @log.info("Artifact uploaded: #{artifact.attributes.to_s}") rescue => e @log.error("Error uploading artifact #{item}: #{e.message}") @log.error(" : #{e.backtrace.join("\n : ")}") end end end end
[ "def", "upload_files", "(", "job", ",", "*", "paths", ")", "@log", ".", "info", "(", "\"Uploading assets\"", ")", "paths", ".", "each", "do", "|", "path", "|", "@log", ".", "info", "(", "\"Uploading files from #{path}\"", ")", "Dir", ".", "foreach", "(", "path", ")", "do", "|", "item", "|", "@log", ".", "info", "(", "\"File: #{item}\"", ")", "next", "if", "item", "==", "'.'", "or", "item", "==", "'..'", "begin", "artifact", "=", "job", ".", "report_artifact", "(", "\"#{path}/#{item}\"", ")", "@log", ".", "info", "(", "\"Artifact uploaded: #{artifact.attributes.to_s}\"", ")", "rescue", "=>", "e", "@log", ".", "error", "(", "\"Error uploading artifact #{item}: #{e.message}\"", ")", "@log", ".", "error", "(", "\" : #{e.backtrace.join(\"\\n : \")}\"", ")", "end", "end", "end", "end" ]
Upload any files from the test
[ "Upload", "any", "files", "from", "the", "test" ]
9079f23362e201c342f416b5038d935e83e0de84
https://github.com/bbc/hive-runner/blob/9079f23362e201c342f416b5038d935e83e0de84/lib/hive/worker.rb#L294-L310
2,417
bbc/hive-runner
lib/hive/worker.rb
Hive.Worker.checkout_code
def checkout_code(repository, checkout_directory, branch) CodeCache.repo(repository).checkout(:head, checkout_directory, branch) or raise "Unable to checkout repository #{repository}" end
ruby
def checkout_code(repository, checkout_directory, branch) CodeCache.repo(repository).checkout(:head, checkout_directory, branch) or raise "Unable to checkout repository #{repository}" end
[ "def", "checkout_code", "(", "repository", ",", "checkout_directory", ",", "branch", ")", "CodeCache", ".", "repo", "(", "repository", ")", ".", "checkout", "(", ":head", ",", "checkout_directory", ",", "branch", ")", "or", "raise", "\"Unable to checkout repository #{repository}\"", "end" ]
Get a checkout of the repository
[ "Get", "a", "checkout", "of", "the", "repository" ]
9079f23362e201c342f416b5038d935e83e0de84
https://github.com/bbc/hive-runner/blob/9079f23362e201c342f416b5038d935e83e0de84/lib/hive/worker.rb#L402-L404
2,418
acook/remedy
lib/remedy/characters.rb
Remedy.Characters.gremlins
def gremlins { space: "\u2420", tab: "\u21B9", carriage_return: "\u23CE", line_feed: "\u240A", control_c: "\u2404", control_d: "\u2403", control_r: "\u2412", escape: "\u238B", backspace: "\u2408", delete: "\u232B", up: "\u2191", down: "\u2193", left: "\u2190", right: "\u2192" } end
ruby
def gremlins { space: "\u2420", tab: "\u21B9", carriage_return: "\u23CE", line_feed: "\u240A", control_c: "\u2404", control_d: "\u2403", control_r: "\u2412", escape: "\u238B", backspace: "\u2408", delete: "\u232B", up: "\u2191", down: "\u2193", left: "\u2190", right: "\u2192" } end
[ "def", "gremlins", "{", "space", ":", "\"\\u2420\"", ",", "tab", ":", "\"\\u21B9\"", ",", "carriage_return", ":", "\"\\u23CE\"", ",", "line_feed", ":", "\"\\u240A\"", ",", "control_c", ":", "\"\\u2404\"", ",", "control_d", ":", "\"\\u2403\"", ",", "control_r", ":", "\"\\u2412\"", ",", "escape", ":", "\"\\u238B\"", ",", "backspace", ":", "\"\\u2408\"", ",", "delete", ":", "\"\\u232B\"", ",", "up", ":", "\"\\u2191\"", ",", "down", ":", "\"\\u2193\"", ",", "left", ":", "\"\\u2190\"", ",", "right", ":", "\"\\u2192\"", "}", "end" ]
Glyphs and Alternate Names
[ "Glyphs", "and", "Alternate", "Names" ]
66452b4e0540dc9c95ca596c69998f79e3a54db6
https://github.com/acook/remedy/blob/66452b4e0540dc9c95ca596c69998f79e3a54db6/lib/remedy/characters.rb#L130-L151
2,419
potatosalad/ruby-erlang-terms
lib/erlang/bitstring.rb
Erlang.Bitstring.bitslice
def bitslice(arg, length = (missing_length = true)) if missing_length if arg.is_a?(Range) from, to = arg.begin, arg.end from += bitsize if from < 0 return nil if from < 0 to += bitsize if to < 0 to += 1 if !arg.exclude_end? length = to - from length = 0 if length < 0 length = bitsize - from if (from + length) > bitsize return nil if length < 0 l8 = length.div(8) l1 = length % 8 pad = 8 - l1 enum = each_bit skip = from enum = enum.drop_while { if skip > 0 skip -= 1 next true else next false end } head = enum.take(length) if l1 == 0 return Erlang::Binary[[head.join].pack(BIT_PACK)] else tail = head[-l1..-1] head = head[0...-l1] tail = ([0] * pad).concat(tail) return Erlang::Bitstring[[[head.join, tail.join].join].pack(BIT_PACK), bits: l1] end else arg += bitsize if arg < 0 return nil if arg < 0 return nil if arg >= bitsize a8 = arg.div(8) a1 = arg % 8 byte = @data.getbyte(a8) return nil if byte.nil? return (byte >> ((@bits - a1 - 1) & 7)) & 1 end else return nil if length < 0 arg += bitsize if arg < 0 return nil if arg < 0 length = bitsize - arg if (arg + length) > bitsize return nil if length < 0 l8 = length.div(8) l1 = length % 8 pad = 8 - l1 enum = each_bit skip = arg enum = enum.drop_while { if skip > 0 skip -= 1 next true else next false end } head = enum.take(length) if l1 == 0 return Erlang::Binary[[head.join].pack(BIT_PACK)] else tail = head[-l1..-1] head = head[0...-l1] tail = ([0] * pad).concat(tail) return Erlang::Bitstring[[[head.join, tail.join].join].pack(BIT_PACK), bits: l1] end end end
ruby
def bitslice(arg, length = (missing_length = true)) if missing_length if arg.is_a?(Range) from, to = arg.begin, arg.end from += bitsize if from < 0 return nil if from < 0 to += bitsize if to < 0 to += 1 if !arg.exclude_end? length = to - from length = 0 if length < 0 length = bitsize - from if (from + length) > bitsize return nil if length < 0 l8 = length.div(8) l1 = length % 8 pad = 8 - l1 enum = each_bit skip = from enum = enum.drop_while { if skip > 0 skip -= 1 next true else next false end } head = enum.take(length) if l1 == 0 return Erlang::Binary[[head.join].pack(BIT_PACK)] else tail = head[-l1..-1] head = head[0...-l1] tail = ([0] * pad).concat(tail) return Erlang::Bitstring[[[head.join, tail.join].join].pack(BIT_PACK), bits: l1] end else arg += bitsize if arg < 0 return nil if arg < 0 return nil if arg >= bitsize a8 = arg.div(8) a1 = arg % 8 byte = @data.getbyte(a8) return nil if byte.nil? return (byte >> ((@bits - a1 - 1) & 7)) & 1 end else return nil if length < 0 arg += bitsize if arg < 0 return nil if arg < 0 length = bitsize - arg if (arg + length) > bitsize return nil if length < 0 l8 = length.div(8) l1 = length % 8 pad = 8 - l1 enum = each_bit skip = arg enum = enum.drop_while { if skip > 0 skip -= 1 next true else next false end } head = enum.take(length) if l1 == 0 return Erlang::Binary[[head.join].pack(BIT_PACK)] else tail = head[-l1..-1] head = head[0...-l1] tail = ([0] * pad).concat(tail) return Erlang::Bitstring[[[head.join, tail.join].join].pack(BIT_PACK), bits: l1] end end end
[ "def", "bitslice", "(", "arg", ",", "length", "=", "(", "missing_length", "=", "true", ")", ")", "if", "missing_length", "if", "arg", ".", "is_a?", "(", "Range", ")", "from", ",", "to", "=", "arg", ".", "begin", ",", "arg", ".", "end", "from", "+=", "bitsize", "if", "from", "<", "0", "return", "nil", "if", "from", "<", "0", "to", "+=", "bitsize", "if", "to", "<", "0", "to", "+=", "1", "if", "!", "arg", ".", "exclude_end?", "length", "=", "to", "-", "from", "length", "=", "0", "if", "length", "<", "0", "length", "=", "bitsize", "-", "from", "if", "(", "from", "+", "length", ")", ">", "bitsize", "return", "nil", "if", "length", "<", "0", "l8", "=", "length", ".", "div", "(", "8", ")", "l1", "=", "length", "%", "8", "pad", "=", "8", "-", "l1", "enum", "=", "each_bit", "skip", "=", "from", "enum", "=", "enum", ".", "drop_while", "{", "if", "skip", ">", "0", "skip", "-=", "1", "next", "true", "else", "next", "false", "end", "}", "head", "=", "enum", ".", "take", "(", "length", ")", "if", "l1", "==", "0", "return", "Erlang", "::", "Binary", "[", "[", "head", ".", "join", "]", ".", "pack", "(", "BIT_PACK", ")", "]", "else", "tail", "=", "head", "[", "-", "l1", "..", "-", "1", "]", "head", "=", "head", "[", "0", "...", "-", "l1", "]", "tail", "=", "(", "[", "0", "]", "*", "pad", ")", ".", "concat", "(", "tail", ")", "return", "Erlang", "::", "Bitstring", "[", "[", "[", "head", ".", "join", ",", "tail", ".", "join", "]", ".", "join", "]", ".", "pack", "(", "BIT_PACK", ")", ",", "bits", ":", "l1", "]", "end", "else", "arg", "+=", "bitsize", "if", "arg", "<", "0", "return", "nil", "if", "arg", "<", "0", "return", "nil", "if", "arg", ">=", "bitsize", "a8", "=", "arg", ".", "div", "(", "8", ")", "a1", "=", "arg", "%", "8", "byte", "=", "@data", ".", "getbyte", "(", "a8", ")", "return", "nil", "if", "byte", ".", "nil?", "return", "(", "byte", ">>", "(", "(", "@bits", "-", "a1", "-", "1", ")", "&", "7", ")", ")", "&", "1", "end", "else", "return", "nil", "if", "length", "<", "0", "arg", "+=", "bitsize", "if", "arg", "<", "0", "return", "nil", "if", "arg", "<", "0", "length", "=", "bitsize", "-", "arg", "if", "(", "arg", "+", "length", ")", ">", "bitsize", "return", "nil", "if", "length", "<", "0", "l8", "=", "length", ".", "div", "(", "8", ")", "l1", "=", "length", "%", "8", "pad", "=", "8", "-", "l1", "enum", "=", "each_bit", "skip", "=", "arg", "enum", "=", "enum", ".", "drop_while", "{", "if", "skip", ">", "0", "skip", "-=", "1", "next", "true", "else", "next", "false", "end", "}", "head", "=", "enum", ".", "take", "(", "length", ")", "if", "l1", "==", "0", "return", "Erlang", "::", "Binary", "[", "[", "head", ".", "join", "]", ".", "pack", "(", "BIT_PACK", ")", "]", "else", "tail", "=", "head", "[", "-", "l1", "..", "-", "1", "]", "head", "=", "head", "[", "0", "...", "-", "l1", "]", "tail", "=", "(", "[", "0", "]", "*", "pad", ")", ".", "concat", "(", "tail", ")", "return", "Erlang", "::", "Bitstring", "[", "[", "[", "head", ".", "join", ",", "tail", ".", "join", "]", ".", "join", "]", ".", "pack", "(", "BIT_PACK", ")", ",", "bits", ":", "l1", "]", "end", "end", "end" ]
Return specific objects from the `Bitstring`. All overloads return `nil` if the starting index is out of range. @overload bitslice(index) Returns a single bit at the given `index`. If `index` is negative, count backwards from the end. @param index [Integer] The index to retrieve. May be negative. @return [Integer, nil] @example b = Erlang::Bitstring[2, bits: 2] b.bitslice(0) # => 1 b.bitslice(1) # => 0 b.bitslice(-1) # => 0 b.bitslice(2) # => nil @overload bitslice(index, length) Return a bitstring starting at `index` and continuing for `length` bits or until the end of the `Bitstring`, whichever occurs first. @param start [Integer] The index to start retrieving bits from. May be negative. @param length [Integer] The number of bits to retrieve. @return [Bitstring, Binary] @example b = Erlang::Bitstring[1, 117, bits: 7] b.bitslice(0, 11) # => Erlang::Bitstring[1, 7, bits: 3] b.bitslice(11, 4) # => Erlang::Bitstring[5, bits: 4] b.bitslice(16, 1) # => nil @overload bitslice(index..end) Return a bitstring starting at `index` and continuing to index `end` or the end of the `Bitstring`, whichever occurs first. @param range [Range] The range of bits to retrieve. @return [Bitstring, Binary] @example b = Erlang::Bitstring[1, 117, bits: 7] b.bitslice(0...11) # => Erlang::Bitstring[1, 7, bits: 3] b.bitslice(11...15) # => Erlang::Bitstring[5, bits: 4] b.bitslice(16..-1) # => nil @see Erlang::Binary#bitslice
[ "Return", "specific", "objects", "from", "the", "Bitstring", ".", "All", "overloads", "return", "nil", "if", "the", "starting", "index", "is", "out", "of", "range", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/bitstring.rb#L191-L264
2,420
potatosalad/ruby-erlang-terms
lib/erlang/bitstring.rb
Erlang.Bitstring.each_bit
def each_bit return enum_for(:each_bit) unless block_given? index = 0 headbits = (self.bytesize - 1) * 8 skipbits = 8 - @bits @data.each_byte do |byte| loop do break if index == @bitsize if index >= headbits bit = (byte >> (7 - ((index + skipbits) & 7))) & 1 else bit = (byte >> (7 - (index & 7))) & 1 end yield bit index += 1 break if (index & 7) == 0 end end return self end
ruby
def each_bit return enum_for(:each_bit) unless block_given? index = 0 headbits = (self.bytesize - 1) * 8 skipbits = 8 - @bits @data.each_byte do |byte| loop do break if index == @bitsize if index >= headbits bit = (byte >> (7 - ((index + skipbits) & 7))) & 1 else bit = (byte >> (7 - (index & 7))) & 1 end yield bit index += 1 break if (index & 7) == 0 end end return self end
[ "def", "each_bit", "return", "enum_for", "(", ":each_bit", ")", "unless", "block_given?", "index", "=", "0", "headbits", "=", "(", "self", ".", "bytesize", "-", "1", ")", "*", "8", "skipbits", "=", "8", "-", "@bits", "@data", ".", "each_byte", "do", "|", "byte", "|", "loop", "do", "break", "if", "index", "==", "@bitsize", "if", "index", ">=", "headbits", "bit", "=", "(", "byte", ">>", "(", "7", "-", "(", "(", "index", "+", "skipbits", ")", "&", "7", ")", ")", ")", "&", "1", "else", "bit", "=", "(", "byte", ">>", "(", "7", "-", "(", "index", "&", "7", ")", ")", ")", "&", "1", "end", "yield", "bit", "index", "+=", "1", "break", "if", "(", "index", "&", "7", ")", "==", "0", "end", "end", "return", "self", "end" ]
Call the given block once for each bit in the `Bitstring`, passing each bit from first to last successively to the block. If no block is given, returns an `Enumerator`. @return [self] @yield [Integer]
[ "Call", "the", "given", "block", "once", "for", "each", "bit", "in", "the", "Bitstring", "passing", "each", "bit", "from", "first", "to", "last", "successively", "to", "the", "block", ".", "If", "no", "block", "is", "given", "returns", "an", "Enumerator", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/bitstring.rb#L322-L341
2,421
TylerRick/capybara-chrome_dev_tools
lib/capybara/chrome_dev_tools/driver_extensions.rb
Capybara::ChromeDevTools.DriverExtensions.start_crmux!
def start_crmux!(opts) self.chrome_debugging_port = find_free_port(Capybara::ChromeDevTools.preferred_port) self.crmux_listen_port = find_free_port(chrome_debugging_port + 1) opts[:options].args << "--remote-debugging-port=#{chrome_debugging_port}" #opts[:options].add_preference 'debuggerAddress', "127.0.0.1:#{crmux_listen_port}" @debug_crmux = true command = "npx crmux #{'-d' if @debug_crmux} \ --port=#{chrome_debugging_port} \ --listen=#{crmux_listen_port}" puts %(command: #{command}) if Capybara::ChromeDevTools.verbose >= 3 if @debug_crmux spawn_opts = {[:out, :err] => 'log/crmux.log'} else spawn_opts = {} end @crmux_pid = spawn(command, spawn_opts) puts %(Started crmux [pid #{@crmux_pid}], listening at http://localhost:#{crmux_listen_port}, connected to localhost:#{chrome_debugging_port}) # You can also get the part later with: page.driver.browser.capabilities["goog:chromeOptions"]["debuggerAddress"] sleep 0.1 at_exit do puts "Killing crmux process #{@crmux_pid}..." if Capybara::ChromeDevTools.verbose >= 1 Process.kill 'TERM', @crmux_pid end end
ruby
def start_crmux!(opts) self.chrome_debugging_port = find_free_port(Capybara::ChromeDevTools.preferred_port) self.crmux_listen_port = find_free_port(chrome_debugging_port + 1) opts[:options].args << "--remote-debugging-port=#{chrome_debugging_port}" #opts[:options].add_preference 'debuggerAddress', "127.0.0.1:#{crmux_listen_port}" @debug_crmux = true command = "npx crmux #{'-d' if @debug_crmux} \ --port=#{chrome_debugging_port} \ --listen=#{crmux_listen_port}" puts %(command: #{command}) if Capybara::ChromeDevTools.verbose >= 3 if @debug_crmux spawn_opts = {[:out, :err] => 'log/crmux.log'} else spawn_opts = {} end @crmux_pid = spawn(command, spawn_opts) puts %(Started crmux [pid #{@crmux_pid}], listening at http://localhost:#{crmux_listen_port}, connected to localhost:#{chrome_debugging_port}) # You can also get the part later with: page.driver.browser.capabilities["goog:chromeOptions"]["debuggerAddress"] sleep 0.1 at_exit do puts "Killing crmux process #{@crmux_pid}..." if Capybara::ChromeDevTools.verbose >= 1 Process.kill 'TERM', @crmux_pid end end
[ "def", "start_crmux!", "(", "opts", ")", "self", ".", "chrome_debugging_port", "=", "find_free_port", "(", "Capybara", "::", "ChromeDevTools", ".", "preferred_port", ")", "self", ".", "crmux_listen_port", "=", "find_free_port", "(", "chrome_debugging_port", "+", "1", ")", "opts", "[", ":options", "]", ".", "args", "<<", "\"--remote-debugging-port=#{chrome_debugging_port}\"", "#opts[:options].add_preference 'debuggerAddress', \"127.0.0.1:#{crmux_listen_port}\"", "@debug_crmux", "=", "true", "command", "=", "\"npx crmux #{'-d' if @debug_crmux} \\\n --port=#{chrome_debugging_port} \\\n --listen=#{crmux_listen_port}\"", "puts", "%(command: #{command})", "if", "Capybara", "::", "ChromeDevTools", ".", "verbose", ">=", "3", "if", "@debug_crmux", "spawn_opts", "=", "{", "[", ":out", ",", ":err", "]", "=>", "'log/crmux.log'", "}", "else", "spawn_opts", "=", "{", "}", "end", "@crmux_pid", "=", "spawn", "(", "command", ",", "spawn_opts", ")", "puts", "%(Started crmux [pid #{@crmux_pid}], listening at http://localhost:#{crmux_listen_port}, connected to localhost:#{chrome_debugging_port})", "# You can also get the part later with: page.driver.browser.capabilities[\"goog:chromeOptions\"][\"debuggerAddress\"]", "sleep", "0.1", "at_exit", "do", "puts", "\"Killing crmux process #{@crmux_pid}...\"", "if", "Capybara", "::", "ChromeDevTools", ".", "verbose", ">=", "1", "Process", ".", "kill", "'TERM'", ",", "@crmux_pid", "end", "end" ]
Starts crmux and adds debuggerAddress to opts that points to crmux listen address.
[ "Starts", "crmux", "and", "adds", "debuggerAddress", "to", "opts", "that", "points", "to", "crmux", "listen", "address", "." ]
805fcc4ac84a125e15577a278de98d5590610f04
https://github.com/TylerRick/capybara-chrome_dev_tools/blob/805fcc4ac84a125e15577a278de98d5590610f04/lib/capybara/chrome_dev_tools/driver_extensions.rb#L36-L61
2,422
bbc/hive-runner
lib/hive/device.rb
Hive.Device.start
def start parent_pid = Process.pid @worker_pid = Process.fork do object = Object @worker_class.split('::').each { |sub| object = object.const_get(sub) } object.new(@options.merge('parent_pid' => parent_pid, 'device_identity' => self.identity, 'port_allocator' => self.port_allocator, 'hive_id' => Hive.hive_mind.device_details['id'])) end Process.detach @worker_pid Hive.logger.info("Worker started with pid #{@worker_pid}") end
ruby
def start parent_pid = Process.pid @worker_pid = Process.fork do object = Object @worker_class.split('::').each { |sub| object = object.const_get(sub) } object.new(@options.merge('parent_pid' => parent_pid, 'device_identity' => self.identity, 'port_allocator' => self.port_allocator, 'hive_id' => Hive.hive_mind.device_details['id'])) end Process.detach @worker_pid Hive.logger.info("Worker started with pid #{@worker_pid}") end
[ "def", "start", "parent_pid", "=", "Process", ".", "pid", "@worker_pid", "=", "Process", ".", "fork", "do", "object", "=", "Object", "@worker_class", ".", "split", "(", "'::'", ")", ".", "each", "{", "|", "sub", "|", "object", "=", "object", ".", "const_get", "(", "sub", ")", "}", "object", ".", "new", "(", "@options", ".", "merge", "(", "'parent_pid'", "=>", "parent_pid", ",", "'device_identity'", "=>", "self", ".", "identity", ",", "'port_allocator'", "=>", "self", ".", "port_allocator", ",", "'hive_id'", "=>", "Hive", ".", "hive_mind", ".", "device_details", "[", "'id'", "]", ")", ")", "end", "Process", ".", "detach", "@worker_pid", "Hive", ".", "logger", ".", "info", "(", "\"Worker started with pid #{@worker_pid}\"", ")", "end" ]
Initialise the device Start the worker process
[ "Initialise", "the", "device", "Start", "the", "worker", "process" ]
9079f23362e201c342f416b5038d935e83e0de84
https://github.com/bbc/hive-runner/blob/9079f23362e201c342f416b5038d935e83e0de84/lib/hive/device.rb#L23-L33
2,423
bbc/hive-runner
lib/hive/device.rb
Hive.Device.stop
def stop protect_file = File.expand_path("#{@worker_pid}.protect", PIDS_DIRECTORY) Hive.logger.debug("Checking for protected file: #{protect_file}") if File.exists? File.expand_path("#{@worker_pid}.protect", PIDS_DIRECTORY) Hive.logger.debug("PID #{@worker_pid} is protected") false else @stop_count = @stop_count.nil? ? 0 : @stop_count + 1 if self.running? if @stop_count < 30 Hive.logger.info("Attempting to terminate process #{@worker_pid} [#{@stop_count}]") Process.kill 'TERM', @worker_pid else Hive.logger.info("Killing process #{@worker_pid}") Process.kill 'KILL', @worker_pid if self.running? end end if self.running? false else @worker_pid = nil @stop_count = nil true end end end
ruby
def stop protect_file = File.expand_path("#{@worker_pid}.protect", PIDS_DIRECTORY) Hive.logger.debug("Checking for protected file: #{protect_file}") if File.exists? File.expand_path("#{@worker_pid}.protect", PIDS_DIRECTORY) Hive.logger.debug("PID #{@worker_pid} is protected") false else @stop_count = @stop_count.nil? ? 0 : @stop_count + 1 if self.running? if @stop_count < 30 Hive.logger.info("Attempting to terminate process #{@worker_pid} [#{@stop_count}]") Process.kill 'TERM', @worker_pid else Hive.logger.info("Killing process #{@worker_pid}") Process.kill 'KILL', @worker_pid if self.running? end end if self.running? false else @worker_pid = nil @stop_count = nil true end end end
[ "def", "stop", "protect_file", "=", "File", ".", "expand_path", "(", "\"#{@worker_pid}.protect\"", ",", "PIDS_DIRECTORY", ")", "Hive", ".", "logger", ".", "debug", "(", "\"Checking for protected file: #{protect_file}\"", ")", "if", "File", ".", "exists?", "File", ".", "expand_path", "(", "\"#{@worker_pid}.protect\"", ",", "PIDS_DIRECTORY", ")", "Hive", ".", "logger", ".", "debug", "(", "\"PID #{@worker_pid} is protected\"", ")", "false", "else", "@stop_count", "=", "@stop_count", ".", "nil?", "?", "0", ":", "@stop_count", "+", "1", "if", "self", ".", "running?", "if", "@stop_count", "<", "30", "Hive", ".", "logger", ".", "info", "(", "\"Attempting to terminate process #{@worker_pid} [#{@stop_count}]\"", ")", "Process", ".", "kill", "'TERM'", ",", "@worker_pid", "else", "Hive", ".", "logger", ".", "info", "(", "\"Killing process #{@worker_pid}\"", ")", "Process", ".", "kill", "'KILL'", ",", "@worker_pid", "if", "self", ".", "running?", "end", "end", "if", "self", ".", "running?", "false", "else", "@worker_pid", "=", "nil", "@stop_count", "=", "nil", "true", "end", "end", "end" ]
Terminate the worker process
[ "Terminate", "the", "worker", "process" ]
9079f23362e201c342f416b5038d935e83e0de84
https://github.com/bbc/hive-runner/blob/9079f23362e201c342f416b5038d935e83e0de84/lib/hive/device.rb#L36-L63
2,424
powerpak/tqdm-ruby
lib/tqdm/printer.rb
Tqdm.Printer.padded_line
def padded_line(iteration, elapsed_time) meter_line = line(iteration, elapsed_time) pad_size = [@last_printed_length - meter_line.size, 0].max @last_printed_length = meter_line.size meter_line + ' ' * pad_size end
ruby
def padded_line(iteration, elapsed_time) meter_line = line(iteration, elapsed_time) pad_size = [@last_printed_length - meter_line.size, 0].max @last_printed_length = meter_line.size meter_line + ' ' * pad_size end
[ "def", "padded_line", "(", "iteration", ",", "elapsed_time", ")", "meter_line", "=", "line", "(", "iteration", ",", "elapsed_time", ")", "pad_size", "=", "[", "@last_printed_length", "-", "meter_line", ".", "size", ",", "0", "]", ".", "max", "@last_printed_length", "=", "meter_line", ".", "size", "meter_line", "+", "' '", "*", "pad_size", "end" ]
Initialize a new Printer. @param options [Hash] the options for the instantiating Tqdm::Decorator @see Tqdm::Decorator#initialize Pads a status line so that it is long enough to overwrite the previously written line @param iteration [Integer] number of iterations, out of the total, that are completed @param elapsed_time [Float] number of seconds passed since start @return [String] the padded line
[ "Initialize", "a", "new", "Printer", "." ]
e67b364267e3d884329044b5cc9e1c4d1fd41637
https://github.com/powerpak/tqdm-ruby/blob/e67b364267e3d884329044b5cc9e1c4d1fd41637/lib/tqdm/printer.rb#L32-L37
2,425
potatosalad/ruby-erlang-terms
lib/erlang/tuple.rb
Erlang.Tuple.put
def put(index, element = yield(get(index))) raise IndexError, "index #{index} outside of tuple bounds" if index < -@size element = Erlang.from(element) index += @size if index < 0 if index > @size suffix = Array.new(index - @size, nil) suffix << element return replace_suffix(@size, suffix) else return update_root(index, element) end end
ruby
def put(index, element = yield(get(index))) raise IndexError, "index #{index} outside of tuple bounds" if index < -@size element = Erlang.from(element) index += @size if index < 0 if index > @size suffix = Array.new(index - @size, nil) suffix << element return replace_suffix(@size, suffix) else return update_root(index, element) end end
[ "def", "put", "(", "index", ",", "element", "=", "yield", "(", "get", "(", "index", ")", ")", ")", "raise", "IndexError", ",", "\"index #{index} outside of tuple bounds\"", "if", "index", "<", "-", "@size", "element", "=", "Erlang", ".", "from", "(", "element", ")", "index", "+=", "@size", "if", "index", "<", "0", "if", "index", ">", "@size", "suffix", "=", "Array", ".", "new", "(", "index", "-", "@size", ",", "nil", ")", "suffix", "<<", "element", "return", "replace_suffix", "(", "@size", ",", "suffix", ")", "else", "return", "update_root", "(", "index", ",", "element", ")", "end", "end" ]
Return a new `Tuple` with a new value at the given `index`. If `index` is greater than the length of the tuple, the returned tuple will be padded with `nil`s to the correct size. @overload put(index, element) Return a new `Tuple` with the element at `index` replaced by `element`. @param element [Object] The object to insert into that position @example Erlang::Tuple[1, 2, 3, 4].put(2, 99) # => Erlang::Tuple[1, 2, 99, 4] Erlang::Tuple[1, 2, 3, 4].put(-1, 99) # => Erlang::Tuple[1, 2, 3, 99] Erlang::Tuple[].put(2, 99) # => Erlang::Tuple[nil, nil, 99] @overload put(index) Return a new `Tuple` with the element at `index` replaced by the return value of the block. @yield (existing) Once with the existing value at the given `index`. @example Erlang::Tuple[1, 2, 3, 4].put(2) { |v| v * 10 } # => Erlang::Tuple[1, 2, 30, 4] @param index [Integer] The index to update. May be negative. @return [Tuple]
[ "Return", "a", "new", "Tuple", "with", "a", "new", "value", "at", "the", "given", "index", ".", "If", "index", "is", "greater", "than", "the", "length", "of", "the", "tuple", "the", "returned", "tuple", "will", "be", "padded", "with", "nil", "s", "to", "the", "correct", "size", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/tuple.rb#L202-L213
2,426
potatosalad/ruby-erlang-terms
lib/erlang/tuple.rb
Erlang.Tuple.fetch
def fetch(index, default = (missing_default = true)) if index >= -@size && index < @size return get(index) elsif block_given? return Erlang.from(yield(index)) elsif !missing_default return Erlang.from(default) else raise IndexError, "index #{index} outside of tuple bounds" end end
ruby
def fetch(index, default = (missing_default = true)) if index >= -@size && index < @size return get(index) elsif block_given? return Erlang.from(yield(index)) elsif !missing_default return Erlang.from(default) else raise IndexError, "index #{index} outside of tuple bounds" end end
[ "def", "fetch", "(", "index", ",", "default", "=", "(", "missing_default", "=", "true", ")", ")", "if", "index", ">=", "-", "@size", "&&", "index", "<", "@size", "return", "get", "(", "index", ")", "elsif", "block_given?", "return", "Erlang", ".", "from", "(", "yield", "(", "index", ")", ")", "elsif", "!", "missing_default", "return", "Erlang", ".", "from", "(", "default", ")", "else", "raise", "IndexError", ",", "\"index #{index} outside of tuple bounds\"", "end", "end" ]
Retrieve the value at `index` with optional default. @overload fetch(index) Retrieve the value at the given index, or raise an `IndexError` if not found. @param index [Integer] The index to look up @raise [IndexError] if index does not exist @example t = Erlang::Tuple["A", "B", "C", "D"] t.fetch(2) # => "C" t.fetch(-1) # => "D" t.fetch(4) # => IndexError: index 4 outside of tuple bounds @overload fetch(index) { |index| ... } Retrieve the value at the given index, or return the result of yielding the block if not found. @yield Once if the index is not found. @yieldparam [Integer] index The index which does not exist @yieldreturn [Object] Default value to return @param index [Integer] The index to look up @example t = Erlang::Tuple["A", "B", "C", "D"] t.fetch(2) { |i| i * i } # => "C" t.fetch(4) { |i| i * i } # => 16 @overload fetch(index, default) Retrieve the value at the given index, or return the provided `default` value if not found. @param index [Integer] The index to look up @param default [Object] Object to return if the key is not found @example t = Erlang::Tuple["A", "B", "C", "D"] t.fetch(2, "Z") # => "C" t.fetch(4, "Z") # => "Z" @return [Object]
[ "Retrieve", "the", "value", "at", "index", "with", "optional", "default", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/tuple.rb#L298-L308
2,427
potatosalad/ruby-erlang-terms
lib/erlang/tuple.rb
Erlang.Tuple.slice
def slice(arg, length = (missing_length = true)) if missing_length if arg.is_a?(Range) from, to = arg.begin, arg.end from += @size if from < 0 to += @size if to < 0 to += 1 if !arg.exclude_end? length = to - from length = 0 if length < 0 return subsequence(from, length) else return get(arg) end else arg += @size if arg < 0 return subsequence(arg, length) end end
ruby
def slice(arg, length = (missing_length = true)) if missing_length if arg.is_a?(Range) from, to = arg.begin, arg.end from += @size if from < 0 to += @size if to < 0 to += 1 if !arg.exclude_end? length = to - from length = 0 if length < 0 return subsequence(from, length) else return get(arg) end else arg += @size if arg < 0 return subsequence(arg, length) end end
[ "def", "slice", "(", "arg", ",", "length", "=", "(", "missing_length", "=", "true", ")", ")", "if", "missing_length", "if", "arg", ".", "is_a?", "(", "Range", ")", "from", ",", "to", "=", "arg", ".", "begin", ",", "arg", ".", "end", "from", "+=", "@size", "if", "from", "<", "0", "to", "+=", "@size", "if", "to", "<", "0", "to", "+=", "1", "if", "!", "arg", ".", "exclude_end?", "length", "=", "to", "-", "from", "length", "=", "0", "if", "length", "<", "0", "return", "subsequence", "(", "from", ",", "length", ")", "else", "return", "get", "(", "arg", ")", "end", "else", "arg", "+=", "@size", "if", "arg", "<", "0", "return", "subsequence", "(", "arg", ",", "length", ")", "end", "end" ]
Return specific objects from the `Tuple`. All overloads return `nil` if the starting index is out of range. @overload tuple.slice(index) Returns a single object at the given `index`. If `index` is negative, count backwards from the end. @param index [Integer] The index to retrieve. May be negative. @return [Object] @example t = Erlang::Tuple["A", "B", "C", "D", "E", "F"] t[2] # => "C" t[-1] # => "F" t[6] # => nil @overload tuple.slice(index, length) Return a subtuple starting at `index` and continuing for `length` elements or until the end of the `Tuple`, whichever occurs first. @param start [Integer] The index to start retrieving elements from. May be negative. @param length [Integer] The number of elements to retrieve. @return [Tuple] @example t = Erlang::Tuple["A", "B", "C", "D", "E", "F"] t[2, 3] # => Erlang::Tuple["C", "D", "E"] t[-2, 3] # => Erlang::Tuple["E", "F"] t[20, 1] # => nil @overload tuple.slice(index..end) Return a subtuple starting at `index` and continuing to index `end` or the end of the `Tuple`, whichever occurs first. @param range [Range] The range of indices to retrieve. @return [Tuple] @example t = Erlang::Tuple["A", "B", "C", "D", "E", "F"] t[2..3] # => Erlang::Tuple["C", "D"] t[-2..100] # => Erlang::Tuple["E", "F"] t[20..21] # => nil
[ "Return", "specific", "objects", "from", "the", "Tuple", ".", "All", "overloads", "return", "nil", "if", "the", "starting", "index", "is", "out", "of", "range", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/tuple.rb#L350-L367
2,428
potatosalad/ruby-erlang-terms
lib/erlang/tuple.rb
Erlang.Tuple.insert
def insert(index, *elements) raise IndexError if index < -@size index += @size if index < 0 elements = elements.map { |element| Erlang.from(element) } if index < @size suffix = flatten_suffix(@root, @levels * BITS_PER_LEVEL, index, []) suffix.unshift(*elements) elsif index == @size suffix = elements else suffix = Array.new(index - @size, nil).concat(elements) index = @size end return replace_suffix(index, suffix) end
ruby
def insert(index, *elements) raise IndexError if index < -@size index += @size if index < 0 elements = elements.map { |element| Erlang.from(element) } if index < @size suffix = flatten_suffix(@root, @levels * BITS_PER_LEVEL, index, []) suffix.unshift(*elements) elsif index == @size suffix = elements else suffix = Array.new(index - @size, nil).concat(elements) index = @size end return replace_suffix(index, suffix) end
[ "def", "insert", "(", "index", ",", "*", "elements", ")", "raise", "IndexError", "if", "index", "<", "-", "@size", "index", "+=", "@size", "if", "index", "<", "0", "elements", "=", "elements", ".", "map", "{", "|", "element", "|", "Erlang", ".", "from", "(", "element", ")", "}", "if", "index", "<", "@size", "suffix", "=", "flatten_suffix", "(", "@root", ",", "@levels", "*", "BITS_PER_LEVEL", ",", "index", ",", "[", "]", ")", "suffix", ".", "unshift", "(", "elements", ")", "elsif", "index", "==", "@size", "suffix", "=", "elements", "else", "suffix", "=", "Array", ".", "new", "(", "index", "-", "@size", ",", "nil", ")", ".", "concat", "(", "elements", ")", "index", "=", "@size", "end", "return", "replace_suffix", "(", "index", ",", "suffix", ")", "end" ]
Return a new `Tuple` with the given values inserted before the element at `index`. If `index` is greater than the current length, `nil` values are added to pad the `Tuple` to the required size. @example Erlang::Tuple["A", "B", "C", "D"].insert(2, "X", "Y", "Z") # => Erlang::Tuple["A", "B", "X", "Y", "Z", "C", "D"] Erlang::Tuple[].insert(2, "X", "Y", "Z") # => Erlang::Tuple[nil, nil, "X", "Y", "Z"] @param index [Integer] The index where the new elements should go @param elements [Array] The elements to add @return [Tuple] @raise [IndexError] if index exceeds negative range.
[ "Return", "a", "new", "Tuple", "with", "the", "given", "values", "inserted", "before", "the", "element", "at", "index", ".", "If", "index", "is", "greater", "than", "the", "current", "length", "nil", "values", "are", "added", "to", "pad", "the", "Tuple", "to", "the", "required", "size", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/tuple.rb#L384-L401
2,429
potatosalad/ruby-erlang-terms
lib/erlang/tuple.rb
Erlang.Tuple.delete_at
def delete_at(index) return self if index >= @size || index < -@size index += @size if index < 0 suffix = flatten_suffix(@root, @levels * BITS_PER_LEVEL, index, []) return replace_suffix(index, suffix.tap { |a| a.shift }) end
ruby
def delete_at(index) return self if index >= @size || index < -@size index += @size if index < 0 suffix = flatten_suffix(@root, @levels * BITS_PER_LEVEL, index, []) return replace_suffix(index, suffix.tap { |a| a.shift }) end
[ "def", "delete_at", "(", "index", ")", "return", "self", "if", "index", ">=", "@size", "||", "index", "<", "-", "@size", "index", "+=", "@size", "if", "index", "<", "0", "suffix", "=", "flatten_suffix", "(", "@root", ",", "@levels", "*", "BITS_PER_LEVEL", ",", "index", ",", "[", "]", ")", "return", "replace_suffix", "(", "index", ",", "suffix", ".", "tap", "{", "|", "a", "|", "a", ".", "shift", "}", ")", "end" ]
Return a new `Tuple` with the element at `index` removed. If the given `index` does not exist, return `self`. @example Erlang::Tuple["A", "B", "C", "D"].delete_at(2) # => Erlang::Tuple["A", "B", "D"] @param index [Integer] The index to remove @return [Tuple]
[ "Return", "a", "new", "Tuple", "with", "the", "element", "at", "index", "removed", ".", "If", "the", "given", "index", "does", "not", "exist", "return", "self", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/tuple.rb#L412-L418
2,430
potatosalad/ruby-erlang-terms
lib/erlang/tuple.rb
Erlang.Tuple.select
def select return enum_for(:select) unless block_given? return reduce(self.class.empty) { |tuple, element| yield(element) ? tuple.add(element) : tuple } end
ruby
def select return enum_for(:select) unless block_given? return reduce(self.class.empty) { |tuple, element| yield(element) ? tuple.add(element) : tuple } end
[ "def", "select", "return", "enum_for", "(", ":select", ")", "unless", "block_given?", "return", "reduce", "(", "self", ".", "class", ".", "empty", ")", "{", "|", "tuple", ",", "element", "|", "yield", "(", "element", ")", "?", "tuple", ".", "add", "(", "element", ")", ":", "tuple", "}", "end" ]
Return a new `Tuple` containing all elements for which the given block returns true. @example Erlang::Tuple["Bird", "Cow", "Elephant"].select { |e| e.size >= 4 } # => Erlang::Tuple["Bird", "Elephant"] @return [Tuple] @yield [element] Once for each element.
[ "Return", "a", "new", "Tuple", "containing", "all", "elements", "for", "which", "the", "given", "block", "returns", "true", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/tuple.rb#L501-L504
2,431
potatosalad/ruby-erlang-terms
lib/erlang/tuple.rb
Erlang.Tuple.shuffle
def shuffle return self.class.new(((array = to_a).frozen? ? array.shuffle : array.shuffle!).freeze) end
ruby
def shuffle return self.class.new(((array = to_a).frozen? ? array.shuffle : array.shuffle!).freeze) end
[ "def", "shuffle", "return", "self", ".", "class", ".", "new", "(", "(", "(", "array", "=", "to_a", ")", ".", "frozen?", "?", "array", ".", "shuffle", ":", "array", ".", "shuffle!", ")", ".", "freeze", ")", "end" ]
Return a new `Tuple` with the same elements as this one, but randomly permuted. @example Erlang::Tuple[1, 2, 3, 4].shuffle # => Erlang::Tuple[4, 1, 3, 2] @return [Tuple]
[ "Return", "a", "new", "Tuple", "with", "the", "same", "elements", "as", "this", "one", "but", "randomly", "permuted", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/tuple.rb#L556-L558
2,432
potatosalad/ruby-erlang-terms
lib/erlang/tuple.rb
Erlang.Tuple.reverse
def reverse return self.class.new(((array = to_a).frozen? ? array.reverse : array.reverse!).freeze) end
ruby
def reverse return self.class.new(((array = to_a).frozen? ? array.reverse : array.reverse!).freeze) end
[ "def", "reverse", "return", "self", ".", "class", ".", "new", "(", "(", "(", "array", "=", "to_a", ")", ".", "frozen?", "?", "array", ".", "reverse", ":", "array", ".", "reverse!", ")", ".", "freeze", ")", "end" ]
Return a new `Tuple` with the same elements as this one, but in reverse order. @example Erlang::Tuple["A", "B", "C"].reverse # => Erlang::Tuple["C", "B", "A"] @return [Tuple]
[ "Return", "a", "new", "Tuple", "with", "the", "same", "elements", "as", "this", "one", "but", "in", "reverse", "order", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/tuple.rb#L593-L595
2,433
potatosalad/ruby-erlang-terms
lib/erlang/tuple.rb
Erlang.Tuple.rotate
def rotate(count = 1) return self if (count % @size) == 0 return self.class.new(((array = to_a).frozen? ? array.rotate(count) : array.rotate!(count)).freeze) end
ruby
def rotate(count = 1) return self if (count % @size) == 0 return self.class.new(((array = to_a).frozen? ? array.rotate(count) : array.rotate!(count)).freeze) end
[ "def", "rotate", "(", "count", "=", "1", ")", "return", "self", "if", "(", "count", "%", "@size", ")", "==", "0", "return", "self", ".", "class", ".", "new", "(", "(", "(", "array", "=", "to_a", ")", ".", "frozen?", "?", "array", ".", "rotate", "(", "count", ")", ":", "array", ".", "rotate!", "(", "count", ")", ")", ".", "freeze", ")", "end" ]
Return a new `Tuple` with the same elements, but rotated so that the one at index `count` is the first element of the new tuple. If `count` is positive, the elements will be shifted left, and those shifted past the lowest position will be moved to the end. If `count` is negative, the elements will be shifted right, and those shifted past the last position will be moved to the beginning. @example t = Erlang::Tuple["A", "B", "C", "D", "E", "F"] t.rotate(2) # => Erlang::Tuple["C", "D", "E", "F", "A", "B"] t.rotate(-1) # => Erlang::Tuple["F", "A", "B", "C", "D", "E"] @param count [Integer] The number of positions to shift elements by @return [Tuple]
[ "Return", "a", "new", "Tuple", "with", "the", "same", "elements", "but", "rotated", "so", "that", "the", "one", "at", "index", "count", "is", "the", "first", "element", "of", "the", "new", "tuple", ".", "If", "count", "is", "positive", "the", "elements", "will", "be", "shifted", "left", "and", "those", "shifted", "past", "the", "lowest", "position", "will", "be", "moved", "to", "the", "end", ".", "If", "count", "is", "negative", "the", "elements", "will", "be", "shifted", "right", "and", "those", "shifted", "past", "the", "last", "position", "will", "be", "moved", "to", "the", "beginning", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/tuple.rb#L610-L613
2,434
potatosalad/ruby-erlang-terms
lib/erlang/tuple.rb
Erlang.Tuple.sort
def sort(&comparator) comparator = Erlang.method(:compare) unless block_given? array = super(&comparator) return self.class.new(array) end
ruby
def sort(&comparator) comparator = Erlang.method(:compare) unless block_given? array = super(&comparator) return self.class.new(array) end
[ "def", "sort", "(", "&", "comparator", ")", "comparator", "=", "Erlang", ".", "method", "(", ":compare", ")", "unless", "block_given?", "array", "=", "super", "(", "comparator", ")", "return", "self", ".", "class", ".", "new", "(", "array", ")", "end" ]
Return a new `Tuple` with the same elements, but sorted. @overload sort Compare elements with their natural sort key (`#<=>`). @example Erlang::Tuple["Elephant", "Dog", "Lion"].sort # => Erlang::Tuple["Dog", "Elephant", "Lion"] @overload sort Uses the block as a comparator to determine sorted order. @yield [a, b] Any number of times with different pairs of elements. @yieldreturn [Integer] Negative if the first element should be sorted lower, positive if the latter element, or 0 if equal. @example Erlang::Tuple["Elephant", "Dog", "Lion"].sort { |a,b| a.size <=> b.size } # => Erlang::Tuple["Dog", "Lion", "Elephant"] @return [Tuple]
[ "Return", "a", "new", "Tuple", "with", "the", "same", "elements", "but", "sorted", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/tuple.rb#L715-L719
2,435
potatosalad/ruby-erlang-terms
lib/erlang/tuple.rb
Erlang.Tuple.drop
def drop(n) return self if n == 0 return self.class.empty if n >= @size raise ArgumentError, "attempt to drop negative size" if n < 0 return self.class.new(flatten_suffix(@root, @levels * BITS_PER_LEVEL, n, [])) end
ruby
def drop(n) return self if n == 0 return self.class.empty if n >= @size raise ArgumentError, "attempt to drop negative size" if n < 0 return self.class.new(flatten_suffix(@root, @levels * BITS_PER_LEVEL, n, [])) end
[ "def", "drop", "(", "n", ")", "return", "self", "if", "n", "==", "0", "return", "self", ".", "class", ".", "empty", "if", "n", ">=", "@size", "raise", "ArgumentError", ",", "\"attempt to drop negative size\"", "if", "n", "<", "0", "return", "self", ".", "class", ".", "new", "(", "flatten_suffix", "(", "@root", ",", "@levels", "*", "BITS_PER_LEVEL", ",", "n", ",", "[", "]", ")", ")", "end" ]
Drop the first `n` elements and return the rest in a new `Tuple`. @example Erlang::Tuple["A", "B", "C", "D", "E", "F"].drop(2) # => Erlang::Tuple["C", "D", "E", "F"] @param n [Integer] The number of elements to remove @return [Tuple] @raise ArgumentError if `n` is negative.
[ "Drop", "the", "first", "n", "elements", "and", "return", "the", "rest", "in", "a", "new", "Tuple", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/tuple.rb#L749-L754
2,436
potatosalad/ruby-erlang-terms
lib/erlang/tuple.rb
Erlang.Tuple.*
def *(times) return self.class.empty if times == 0 return self if times == 1 result = (to_a * times) return result.is_a?(Array) ? self.class.new(result) : result end
ruby
def *(times) return self.class.empty if times == 0 return self if times == 1 result = (to_a * times) return result.is_a?(Array) ? self.class.new(result) : result end
[ "def", "*", "(", "times", ")", "return", "self", ".", "class", ".", "empty", "if", "times", "==", "0", "return", "self", "if", "times", "==", "1", "result", "=", "(", "to_a", "*", "times", ")", "return", "result", ".", "is_a?", "(", "Array", ")", "?", "self", ".", "class", ".", "new", "(", "result", ")", ":", "result", "end" ]
Repetition. Return a new `Tuple` built by concatenating `times` copies of this one together. @example Erlang::Tuple["A", "B"] * 3 # => Erlang::Tuple["A", "B", "A", "B", "A", "B"] @param times [Integer] The number of times to repeat the elements in this tuple @return [Tuple]
[ "Repetition", ".", "Return", "a", "new", "Tuple", "built", "by", "concatenating", "times", "copies", "of", "this", "one", "together", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/tuple.rb#L806-L811
2,437
potatosalad/ruby-erlang-terms
lib/erlang/tuple.rb
Erlang.Tuple.combination
def combination(n) return enum_for(:combination, n) if not block_given? return self if n < 0 || @size < n if n == 0 yield [] elsif n == 1 each { |element| yield [element] } elsif n == @size yield self.to_a else combos = lambda do |result,index,remaining| while @size - index > remaining if remaining == 1 yield result.dup << get(index) else combos[result.dup << get(index), index+1, remaining-1] end index += 1 end index.upto(@size-1) { |i| result << get(i) } yield result end combos[[], 0, n] end return self end
ruby
def combination(n) return enum_for(:combination, n) if not block_given? return self if n < 0 || @size < n if n == 0 yield [] elsif n == 1 each { |element| yield [element] } elsif n == @size yield self.to_a else combos = lambda do |result,index,remaining| while @size - index > remaining if remaining == 1 yield result.dup << get(index) else combos[result.dup << get(index), index+1, remaining-1] end index += 1 end index.upto(@size-1) { |i| result << get(i) } yield result end combos[[], 0, n] end return self end
[ "def", "combination", "(", "n", ")", "return", "enum_for", "(", ":combination", ",", "n", ")", "if", "not", "block_given?", "return", "self", "if", "n", "<", "0", "||", "@size", "<", "n", "if", "n", "==", "0", "yield", "[", "]", "elsif", "n", "==", "1", "each", "{", "|", "element", "|", "yield", "[", "element", "]", "}", "elsif", "n", "==", "@size", "yield", "self", ".", "to_a", "else", "combos", "=", "lambda", "do", "|", "result", ",", "index", ",", "remaining", "|", "while", "@size", "-", "index", ">", "remaining", "if", "remaining", "==", "1", "yield", "result", ".", "dup", "<<", "get", "(", "index", ")", "else", "combos", "[", "result", ".", "dup", "<<", "get", "(", "index", ")", ",", "index", "+", "1", ",", "remaining", "-", "1", "]", "end", "index", "+=", "1", "end", "index", ".", "upto", "(", "@size", "-", "1", ")", "{", "|", "i", "|", "result", "<<", "get", "(", "i", ")", "}", "yield", "result", "end", "combos", "[", "[", "]", ",", "0", ",", "n", "]", "end", "return", "self", "end" ]
When invoked with a block, yields all combinations of length `n` of elements from the `Tuple`, and then returns `self`. There is no guarantee about which order the combinations will be yielded. If no block is given, an `Enumerator` is returned instead. @example t = Erlang::Tuple[5, 6, 7, 8] t.combination(3) { |c| puts "Combination: #{c}" } Combination: [5, 6, 7] Combination: [5, 6, 8] Combination: [5, 7, 8] Combination: [6, 7, 8] #=> Erlang::Tuple[5, 6, 7, 8] @return [self, Enumerator]
[ "When", "invoked", "with", "a", "block", "yields", "all", "combinations", "of", "length", "n", "of", "elements", "from", "the", "Tuple", "and", "then", "returns", "self", ".", "There", "is", "no", "guarantee", "about", "which", "order", "the", "combinations", "will", "be", "yielded", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/tuple.rb#L886-L911
2,438
potatosalad/ruby-erlang-terms
lib/erlang/tuple.rb
Erlang.Tuple.repeated_combination
def repeated_combination(n) return enum_for(:repeated_combination, n) if not block_given? if n < 0 # yield nothing elsif n == 0 yield [] elsif n == 1 each { |element| yield [element] } elsif @size == 0 # yield nothing else combos = lambda do |result,index,remaining| while index < @size-1 if remaining == 1 yield result.dup << get(index) else combos[result.dup << get(index), index, remaining-1] end index += 1 end element = get(index) remaining.times { result << element } yield result end combos[[], 0, n] end return self end
ruby
def repeated_combination(n) return enum_for(:repeated_combination, n) if not block_given? if n < 0 # yield nothing elsif n == 0 yield [] elsif n == 1 each { |element| yield [element] } elsif @size == 0 # yield nothing else combos = lambda do |result,index,remaining| while index < @size-1 if remaining == 1 yield result.dup << get(index) else combos[result.dup << get(index), index, remaining-1] end index += 1 end element = get(index) remaining.times { result << element } yield result end combos[[], 0, n] end return self end
[ "def", "repeated_combination", "(", "n", ")", "return", "enum_for", "(", ":repeated_combination", ",", "n", ")", "if", "not", "block_given?", "if", "n", "<", "0", "# yield nothing", "elsif", "n", "==", "0", "yield", "[", "]", "elsif", "n", "==", "1", "each", "{", "|", "element", "|", "yield", "[", "element", "]", "}", "elsif", "@size", "==", "0", "# yield nothing", "else", "combos", "=", "lambda", "do", "|", "result", ",", "index", ",", "remaining", "|", "while", "index", "<", "@size", "-", "1", "if", "remaining", "==", "1", "yield", "result", ".", "dup", "<<", "get", "(", "index", ")", "else", "combos", "[", "result", ".", "dup", "<<", "get", "(", "index", ")", ",", "index", ",", "remaining", "-", "1", "]", "end", "index", "+=", "1", "end", "element", "=", "get", "(", "index", ")", "remaining", ".", "times", "{", "result", "<<", "element", "}", "yield", "result", "end", "combos", "[", "[", "]", ",", "0", ",", "n", "]", "end", "return", "self", "end" ]
When invoked with a block, yields all repeated combinations of length `n` of tuples from the `Tuple`, and then returns `self`. A "repeated combination" is one in which any tuple from the `Tuple` can appear consecutively any number of times. There is no guarantee about which order the combinations will be yielded in. If no block is given, an `Enumerator` is returned instead. @example t = Erlang::Tuple[5, 6, 7, 8] t.repeated_combination(2) { |c| puts "Combination: #{c}" } Combination: [5, 5] Combination: [5, 6] Combination: [5, 7] Combination: [5, 8] Combination: [6, 6] Combination: [6, 7] Combination: [6, 8] Combination: [7, 7] Combination: [7, 8] Combination: [8, 8] # => Erlang::Tuple[5, 6, 7, 8] @return [self, Enumerator]
[ "When", "invoked", "with", "a", "block", "yields", "all", "repeated", "combinations", "of", "length", "n", "of", "tuples", "from", "the", "Tuple", "and", "then", "returns", "self", ".", "A", "repeated", "combination", "is", "one", "in", "which", "any", "tuple", "from", "the", "Tuple", "can", "appear", "consecutively", "any", "number", "of", "times", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/tuple.rb#L939-L966
2,439
potatosalad/ruby-erlang-terms
lib/erlang/tuple.rb
Erlang.Tuple.permutation
def permutation(n = @size) return enum_for(:permutation, n) if not block_given? if n < 0 || @size < n # yield nothing elsif n == 0 yield [] elsif n == 1 each { |element| yield [element] } else used, result = [], [] perms = lambda do |index| 0.upto(@size-1) do |i| if !used[i] result[index] = get(i) if index < n-1 used[i] = true perms[index+1] used[i] = false else yield result.dup end end end end perms[0] end return self end
ruby
def permutation(n = @size) return enum_for(:permutation, n) if not block_given? if n < 0 || @size < n # yield nothing elsif n == 0 yield [] elsif n == 1 each { |element| yield [element] } else used, result = [], [] perms = lambda do |index| 0.upto(@size-1) do |i| if !used[i] result[index] = get(i) if index < n-1 used[i] = true perms[index+1] used[i] = false else yield result.dup end end end end perms[0] end return self end
[ "def", "permutation", "(", "n", "=", "@size", ")", "return", "enum_for", "(", ":permutation", ",", "n", ")", "if", "not", "block_given?", "if", "n", "<", "0", "||", "@size", "<", "n", "# yield nothing", "elsif", "n", "==", "0", "yield", "[", "]", "elsif", "n", "==", "1", "each", "{", "|", "element", "|", "yield", "[", "element", "]", "}", "else", "used", ",", "result", "=", "[", "]", ",", "[", "]", "perms", "=", "lambda", "do", "|", "index", "|", "0", ".", "upto", "(", "@size", "-", "1", ")", "do", "|", "i", "|", "if", "!", "used", "[", "i", "]", "result", "[", "index", "]", "=", "get", "(", "i", ")", "if", "index", "<", "n", "-", "1", "used", "[", "i", "]", "=", "true", "perms", "[", "index", "+", "1", "]", "used", "[", "i", "]", "=", "false", "else", "yield", "result", ".", "dup", "end", "end", "end", "end", "perms", "[", "0", "]", "end", "return", "self", "end" ]
Yields all permutations of length `n` of elements from the `Tuple`, and then returns `self`. If no length `n` is specified, permutations of all elements will be yielded. There is no guarantee about which order the permutations will be yielded in. If no block is given, an `Enumerator` is returned instead. @example t = Erlang::Tuple[5, 6, 7] t.permutation(2) { |p| puts "Permutation: #{p}" } Permutation: [5, 6] Permutation: [5, 7] Permutation: [6, 5] Permutation: [6, 7] Permutation: [7, 5] Permutation: [7, 6] # => Erlang::Tuple[5, 6, 7] @return [self, Enumerator]
[ "Yields", "all", "permutations", "of", "length", "n", "of", "elements", "from", "the", "Tuple", "and", "then", "returns", "self", ".", "If", "no", "length", "n", "is", "specified", "permutations", "of", "all", "elements", "will", "be", "yielded", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/tuple.rb#L989-L1016
2,440
potatosalad/ruby-erlang-terms
lib/erlang/tuple.rb
Erlang.Tuple.product
def product(*tuples) tuples = tuples.map { |tuple| Erlang.from(tuple) } # if no tuples passed, return "product" as in result of multiplying all elements return super if tuples.empty? tuples.unshift(self) if tuples.any?(&:empty?) return block_given? ? self : [] end counters = Array.new(tuples.size, 0) bump_counters = lambda do i = tuples.size-1 counters[i] += 1 while counters[i] == tuples[i].size counters[i] = 0 i -= 1 return true if i == -1 # we are done counters[i] += 1 end false # not done yet end build_array = lambda do array = [] counters.each_with_index { |index,i| array << tuples[i][index] } array end if block_given? while true yield build_array[] return self if bump_counters[] end else result = [] while true result << build_array[] return result if bump_counters[] end end end
ruby
def product(*tuples) tuples = tuples.map { |tuple| Erlang.from(tuple) } # if no tuples passed, return "product" as in result of multiplying all elements return super if tuples.empty? tuples.unshift(self) if tuples.any?(&:empty?) return block_given? ? self : [] end counters = Array.new(tuples.size, 0) bump_counters = lambda do i = tuples.size-1 counters[i] += 1 while counters[i] == tuples[i].size counters[i] = 0 i -= 1 return true if i == -1 # we are done counters[i] += 1 end false # not done yet end build_array = lambda do array = [] counters.each_with_index { |index,i| array << tuples[i][index] } array end if block_given? while true yield build_array[] return self if bump_counters[] end else result = [] while true result << build_array[] return result if bump_counters[] end end end
[ "def", "product", "(", "*", "tuples", ")", "tuples", "=", "tuples", ".", "map", "{", "|", "tuple", "|", "Erlang", ".", "from", "(", "tuple", ")", "}", "# if no tuples passed, return \"product\" as in result of multiplying all elements", "return", "super", "if", "tuples", ".", "empty?", "tuples", ".", "unshift", "(", "self", ")", "if", "tuples", ".", "any?", "(", ":empty?", ")", "return", "block_given?", "?", "self", ":", "[", "]", "end", "counters", "=", "Array", ".", "new", "(", "tuples", ".", "size", ",", "0", ")", "bump_counters", "=", "lambda", "do", "i", "=", "tuples", ".", "size", "-", "1", "counters", "[", "i", "]", "+=", "1", "while", "counters", "[", "i", "]", "==", "tuples", "[", "i", "]", ".", "size", "counters", "[", "i", "]", "=", "0", "i", "-=", "1", "return", "true", "if", "i", "==", "-", "1", "# we are done", "counters", "[", "i", "]", "+=", "1", "end", "false", "# not done yet", "end", "build_array", "=", "lambda", "do", "array", "=", "[", "]", "counters", ".", "each_with_index", "{", "|", "index", ",", "i", "|", "array", "<<", "tuples", "[", "i", "]", "[", "index", "]", "}", "array", "end", "if", "block_given?", "while", "true", "yield", "build_array", "[", "]", "return", "self", "if", "bump_counters", "[", "]", "end", "else", "result", "=", "[", "]", "while", "true", "result", "<<", "build_array", "[", "]", "return", "result", "if", "bump_counters", "[", "]", "end", "end", "end" ]
Cartesian product or multiplication. @overload product(*tuples) Return a `Tuple` of all combinations of elements from this `Tuple` and each of the given tuples or arrays. The length of the returned `Tuple` is the product of `self.size` and the size of each argument tuple or array. @example t1 = Erlang::Tuple[1, 2, 3] t2 = Erlang::Tuple["A", "B"] t1.product(t2) # => [[1, "A"], [1, "B"], [2, "A"], [2, "B"], [3, "A"], [3, "B"]] @overload product Return the result of multiplying all the elements in this `Tuple` together. @example Erlang::Tuple[1, 2, 3, 4, 5].product # => 120 @return [Tuple]
[ "Cartesian", "product", "or", "multiplication", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/tuple.rb#L1087-L1129
2,441
potatosalad/ruby-erlang-terms
lib/erlang/tuple.rb
Erlang.Tuple.rindex
def rindex(obj = (missing_arg = true)) obj = Erlang.from(obj) i = @size - 1 if missing_arg if block_given? reverse_each { |element| return i if yield element; i -= 1 } return nil else return enum_for(:rindex) end else reverse_each { |element| return i if element == obj; i -= 1 } return nil end end
ruby
def rindex(obj = (missing_arg = true)) obj = Erlang.from(obj) i = @size - 1 if missing_arg if block_given? reverse_each { |element| return i if yield element; i -= 1 } return nil else return enum_for(:rindex) end else reverse_each { |element| return i if element == obj; i -= 1 } return nil end end
[ "def", "rindex", "(", "obj", "=", "(", "missing_arg", "=", "true", ")", ")", "obj", "=", "Erlang", ".", "from", "(", "obj", ")", "i", "=", "@size", "-", "1", "if", "missing_arg", "if", "block_given?", "reverse_each", "{", "|", "element", "|", "return", "i", "if", "yield", "element", ";", "i", "-=", "1", "}", "return", "nil", "else", "return", "enum_for", "(", ":rindex", ")", "end", "else", "reverse_each", "{", "|", "element", "|", "return", "i", "if", "element", "==", "obj", ";", "i", "-=", "1", "}", "return", "nil", "end", "end" ]
Find the index of an element, starting from the end of the tuple. Returns `nil` if no element is found. @overload rindex(obj) Return the index of the last element which is `#==` to `obj`. @example t = Erlang::Tuple[7, 8, 9, 7, 8, 9] t.rindex(8) # => 4 @overload rindex Return the index of the last element for which the block returns true. @yield [element] Once for each element, last to first, until the block returns true. @example t = Erlang::Tuple[7, 8, 9, 7, 8, 9] t.rindex { |e| e.even? } # => 4 @return [Integer]
[ "Find", "the", "index", "of", "an", "element", "starting", "from", "the", "end", "of", "the", "tuple", ".", "Returns", "nil", "if", "no", "element", "is", "found", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/tuple.rb#L1266-L1280
2,442
potatosalad/ruby-erlang-terms
lib/erlang/enumerable.rb
Erlang.Enumerable.partition
def partition return enum_for(:partition) if not block_given? a,b = super return Erlang::Tuple[self.class.new(a), self.class.new(b)] end
ruby
def partition return enum_for(:partition) if not block_given? a,b = super return Erlang::Tuple[self.class.new(a), self.class.new(b)] end
[ "def", "partition", "return", "enum_for", "(", ":partition", ")", "if", "not", "block_given?", "a", ",", "b", "=", "super", "return", "Erlang", "::", "Tuple", "[", "self", ".", "class", ".", "new", "(", "a", ")", ",", "self", ".", "class", ".", "new", "(", "b", ")", "]", "end" ]
Return 2 collections, the first containing all the elements for which the block evaluates to true, the second containing the rest.
[ "Return", "2", "collections", "the", "first", "containing", "all", "the", "elements", "for", "which", "the", "block", "evaluates", "to", "true", "the", "second", "containing", "the", "rest", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/enumerable.rb#L89-L93
2,443
potatosalad/ruby-erlang-terms
lib/erlang/enumerable.rb
Erlang.Enumerable.inspect
def inspect result = "#{self.class}[" each_with_index { |obj, i| result << ', ' if i > 0; result << obj.inspect } return result << "]" end
ruby
def inspect result = "#{self.class}[" each_with_index { |obj, i| result << ', ' if i > 0; result << obj.inspect } return result << "]" end
[ "def", "inspect", "result", "=", "\"#{self.class}[\"", "each_with_index", "{", "|", "obj", ",", "i", "|", "result", "<<", "', '", "if", "i", ">", "0", ";", "result", "<<", "obj", ".", "inspect", "}", "return", "result", "<<", "\"]\"", "end" ]
Convert this collection to a programmer-readable `String` representation.
[ "Convert", "this", "collection", "to", "a", "programmer", "-", "readable", "String", "representation", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/enumerable.rb#L131-L135
2,444
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.each
def each raise Erlang::ImproperListError if improper? return to_enum unless block_given? list = self until list.empty? yield(list.head) list = list.tail end return self end
ruby
def each raise Erlang::ImproperListError if improper? return to_enum unless block_given? list = self until list.empty? yield(list.head) list = list.tail end return self end
[ "def", "each", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "return", "to_enum", "unless", "block_given?", "list", "=", "self", "until", "list", ".", "empty?", "yield", "(", "list", ".", "head", ")", "list", "=", "list", ".", "tail", "end", "return", "self", "end" ]
Call the given block once for each item in the list, passing each item from first to last successively to the block. If no block is given, returns an `Enumerator`. @return [self] @yield [item]
[ "Call", "the", "given", "block", "once", "for", "each", "item", "in", "the", "list", "passing", "each", "item", "from", "first", "to", "last", "successively", "to", "the", "block", ".", "If", "no", "block", "is", "given", "returns", "an", "Enumerator", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L151-L160
2,445
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.flat_map
def flat_map(&block) raise Erlang::ImproperListError if improper? return enum_for(:flat_map) unless block_given? return self if empty? out = tail = Erlang::Cons.allocate list = self until list.empty? head_list = Erlang::List.from_enum(yield(list.head)) if head_list.empty? list = list.tail elsif list.tail.empty? tail.instance_variable_set(:@head, head_list.head) tail.instance_variable_set(:@tail, head_list.tail) tail.immutable! list = list.tail else new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@head, head_list.head) tail.instance_variable_set(:@tail, head_list.tail + new_node) tail.immutable! list = list.tail tail = new_node end end if not tail.immutable? tail.instance_variable_set(:@tail, Erlang::Nil) tail.immutable! end if out === tail and not out.tail.kind_of?(Erlang::List) return out.tail else return out end end
ruby
def flat_map(&block) raise Erlang::ImproperListError if improper? return enum_for(:flat_map) unless block_given? return self if empty? out = tail = Erlang::Cons.allocate list = self until list.empty? head_list = Erlang::List.from_enum(yield(list.head)) if head_list.empty? list = list.tail elsif list.tail.empty? tail.instance_variable_set(:@head, head_list.head) tail.instance_variable_set(:@tail, head_list.tail) tail.immutable! list = list.tail else new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@head, head_list.head) tail.instance_variable_set(:@tail, head_list.tail + new_node) tail.immutable! list = list.tail tail = new_node end end if not tail.immutable? tail.instance_variable_set(:@tail, Erlang::Nil) tail.immutable! end if out === tail and not out.tail.kind_of?(Erlang::List) return out.tail else return out end end
[ "def", "flat_map", "(", "&", "block", ")", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "return", "enum_for", "(", ":flat_map", ")", "unless", "block_given?", "return", "self", "if", "empty?", "out", "=", "tail", "=", "Erlang", "::", "Cons", ".", "allocate", "list", "=", "self", "until", "list", ".", "empty?", "head_list", "=", "Erlang", "::", "List", ".", "from_enum", "(", "yield", "(", "list", ".", "head", ")", ")", "if", "head_list", ".", "empty?", "list", "=", "list", ".", "tail", "elsif", "list", ".", "tail", ".", "empty?", "tail", ".", "instance_variable_set", "(", ":@head", ",", "head_list", ".", "head", ")", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "head_list", ".", "tail", ")", "tail", ".", "immutable!", "list", "=", "list", ".", "tail", "else", "new_node", "=", "Erlang", "::", "Cons", ".", "allocate", "new_node", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "tail", ".", "instance_variable_set", "(", ":@head", ",", "head_list", ".", "head", ")", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "head_list", ".", "tail", "+", "new_node", ")", "tail", ".", "immutable!", "list", "=", "list", ".", "tail", "tail", "=", "new_node", "end", "end", "if", "not", "tail", ".", "immutable?", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "Erlang", "::", "Nil", ")", "tail", ".", "immutable!", "end", "if", "out", "===", "tail", "and", "not", "out", ".", "tail", ".", "kind_of?", "(", "Erlang", "::", "List", ")", "return", "out", ".", "tail", "else", "return", "out", "end", "end" ]
Return a `List` which is realized by transforming each item into a `List`, and flattening the resulting lists. @example Erlang::List[1, 2, 3].flat_map { |x| Erlang::List[x, 100] } # => Erlang::List[1, 100, 2, 100, 3, 100] @return [List]
[ "Return", "a", "List", "which", "is", "realized", "by", "transforming", "each", "item", "into", "a", "List", "and", "flattening", "the", "resulting", "lists", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L203-L237
2,446
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.select
def select(&block) raise Erlang::ImproperListError if improper? return enum_for(:select) unless block_given? out = tail = Erlang::Cons.allocate list = self while !list.empty? if yield(list.head) new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, false) tail.immutable! tail = new_node list = list.tail else list = list.tail end end if not tail.immutable? tail.instance_variable_set(:@tail, Erlang::Nil) tail.immutable! end return out.tail end
ruby
def select(&block) raise Erlang::ImproperListError if improper? return enum_for(:select) unless block_given? out = tail = Erlang::Cons.allocate list = self while !list.empty? if yield(list.head) new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, false) tail.immutable! tail = new_node list = list.tail else list = list.tail end end if not tail.immutable? tail.instance_variable_set(:@tail, Erlang::Nil) tail.immutable! end return out.tail end
[ "def", "select", "(", "&", "block", ")", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "return", "enum_for", "(", ":select", ")", "unless", "block_given?", "out", "=", "tail", "=", "Erlang", "::", "Cons", ".", "allocate", "list", "=", "self", "while", "!", "list", ".", "empty?", "if", "yield", "(", "list", ".", "head", ")", "new_node", "=", "Erlang", "::", "Cons", ".", "allocate", "new_node", ".", "instance_variable_set", "(", ":@head", ",", "list", ".", "head", ")", "new_node", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "new_node", ")", "tail", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "tail", ".", "immutable!", "tail", "=", "new_node", "list", "=", "list", ".", "tail", "else", "list", "=", "list", ".", "tail", "end", "end", "if", "not", "tail", ".", "immutable?", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "Erlang", "::", "Nil", ")", "tail", ".", "immutable!", "end", "return", "out", ".", "tail", "end" ]
Return a `List` which contains all the items for which the given block returns true. @example Erlang::List["Bird", "Cow", "Elephant"].select { |e| e.size >= 4 } # => Erlang::List["Bird", "Elephant"] @return [List] @yield [item] Once for each item.
[ "Return", "a", "List", "which", "contains", "all", "the", "items", "for", "which", "the", "given", "block", "returns", "true", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L248-L272
2,447
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.drop_while
def drop_while(&block) raise Erlang::ImproperListError if improper? return enum_for(:drop_while) unless block_given? list = self list = list.tail while !list.empty? && yield(list.head) return list end
ruby
def drop_while(&block) raise Erlang::ImproperListError if improper? return enum_for(:drop_while) unless block_given? list = self list = list.tail while !list.empty? && yield(list.head) return list end
[ "def", "drop_while", "(", "&", "block", ")", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "return", "enum_for", "(", ":drop_while", ")", "unless", "block_given?", "list", "=", "self", "list", "=", "list", ".", "tail", "while", "!", "list", ".", "empty?", "&&", "yield", "(", "list", ".", "head", ")", "return", "list", "end" ]
Return a `List` which contains all elements starting from the first element for which the block returns `nil` or `false`. @example Erlang::List[1, 3, 5, 7, 6, 4, 2].drop_while { |e| e < 5 } # => Erlang::List[5, 7, 6, 4, 2] @return [List, Enumerator] @yield [item]
[ "Return", "a", "List", "which", "contains", "all", "elements", "starting", "from", "the", "first", "element", "for", "which", "the", "block", "returns", "nil", "or", "false", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L317-L323
2,448
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.pop
def pop raise Erlang::ImproperListError if improper? return self if empty? new_size = size - 1 return Erlang::List.new(head, tail.take(new_size - 1)) if new_size >= 1 return Erlang::Nil end
ruby
def pop raise Erlang::ImproperListError if improper? return self if empty? new_size = size - 1 return Erlang::List.new(head, tail.take(new_size - 1)) if new_size >= 1 return Erlang::Nil end
[ "def", "pop", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "return", "self", "if", "empty?", "new_size", "=", "size", "-", "1", "return", "Erlang", "::", "List", ".", "new", "(", "head", ",", "tail", ".", "take", "(", "new_size", "-", "1", ")", ")", "if", "new_size", ">=", "1", "return", "Erlang", "::", "Nil", "end" ]
Return a `List` containing all but the last item from this `List`. @example Erlang::List["A", "B", "C"].pop # => Erlang::List["A", "B"] @return [List]
[ "Return", "a", "List", "containing", "all", "but", "the", "last", "item", "from", "this", "List", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L363-L369
2,449
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.drop
def drop(number) raise Erlang::ImproperListError if improper? list = self while !list.empty? && number > 0 number -= 1 list = list.tail end return list end
ruby
def drop(number) raise Erlang::ImproperListError if improper? list = self while !list.empty? && number > 0 number -= 1 list = list.tail end return list end
[ "def", "drop", "(", "number", ")", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "list", "=", "self", "while", "!", "list", ".", "empty?", "&&", "number", ">", "0", "number", "-=", "1", "list", "=", "list", ".", "tail", "end", "return", "list", "end" ]
Return a `List` containing all items after the first `number` items from this `List`. @example Erlang::List[1, 3, 5, 7, 6, 4, 2].drop(3) # => Erlang::List[7, 6, 4, 2] @param number [Integer] The number of items to skip over @return [List]
[ "Return", "a", "List", "containing", "all", "items", "after", "the", "first", "number", "items", "from", "this", "List", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L380-L388
2,450
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.append
def append(other) # raise Erlang::ImproperListError if improper? other = Erlang.from(other) return self if not improper? and Erlang.is_list(other) and other.empty? return other if Erlang.is_list(other) and empty? is_improper = Erlang.is_list(other) ? other.improper? : true out = tail = Erlang::Cons.allocate list = self until list.empty? new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, is_improper) tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, is_improper) tail.immutable! tail = new_node if not Erlang.is_list(list.tail) new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.tail) new_node.instance_variable_set(:@improper, is_improper) tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, is_improper) tail.immutable! tail = new_node list = Erlang::Nil else list = list.tail end end if not tail.immutable? tail.instance_variable_set(:@tail, other) tail.immutable! end return out.tail end
ruby
def append(other) # raise Erlang::ImproperListError if improper? other = Erlang.from(other) return self if not improper? and Erlang.is_list(other) and other.empty? return other if Erlang.is_list(other) and empty? is_improper = Erlang.is_list(other) ? other.improper? : true out = tail = Erlang::Cons.allocate list = self until list.empty? new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, is_improper) tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, is_improper) tail.immutable! tail = new_node if not Erlang.is_list(list.tail) new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.tail) new_node.instance_variable_set(:@improper, is_improper) tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, is_improper) tail.immutable! tail = new_node list = Erlang::Nil else list = list.tail end end if not tail.immutable? tail.instance_variable_set(:@tail, other) tail.immutable! end return out.tail end
[ "def", "append", "(", "other", ")", "# raise Erlang::ImproperListError if improper?", "other", "=", "Erlang", ".", "from", "(", "other", ")", "return", "self", "if", "not", "improper?", "and", "Erlang", ".", "is_list", "(", "other", ")", "and", "other", ".", "empty?", "return", "other", "if", "Erlang", ".", "is_list", "(", "other", ")", "and", "empty?", "is_improper", "=", "Erlang", ".", "is_list", "(", "other", ")", "?", "other", ".", "improper?", ":", "true", "out", "=", "tail", "=", "Erlang", "::", "Cons", ".", "allocate", "list", "=", "self", "until", "list", ".", "empty?", "new_node", "=", "Erlang", "::", "Cons", ".", "allocate", "new_node", ".", "instance_variable_set", "(", ":@head", ",", "list", ".", "head", ")", "new_node", ".", "instance_variable_set", "(", ":@improper", ",", "is_improper", ")", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "new_node", ")", "tail", ".", "instance_variable_set", "(", ":@improper", ",", "is_improper", ")", "tail", ".", "immutable!", "tail", "=", "new_node", "if", "not", "Erlang", ".", "is_list", "(", "list", ".", "tail", ")", "new_node", "=", "Erlang", "::", "Cons", ".", "allocate", "new_node", ".", "instance_variable_set", "(", ":@head", ",", "list", ".", "tail", ")", "new_node", ".", "instance_variable_set", "(", ":@improper", ",", "is_improper", ")", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "new_node", ")", "tail", ".", "instance_variable_set", "(", ":@improper", ",", "is_improper", ")", "tail", ".", "immutable!", "tail", "=", "new_node", "list", "=", "Erlang", "::", "Nil", "else", "list", "=", "list", ".", "tail", "end", "end", "if", "not", "tail", ".", "immutable?", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "other", ")", "tail", ".", "immutable!", "end", "return", "out", ".", "tail", "end" ]
Return a `List` with all items from this `List`, followed by all items from `other`. @example Erlang::List[1, 2, 3].append(Erlang::List[4, 5]) # => Erlang::List[1, 2, 3, 4, 5] @param other [List] The list to add onto the end of this one @return [List]
[ "Return", "a", "List", "with", "all", "items", "from", "this", "List", "followed", "by", "all", "items", "from", "other", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L399-L433
2,451
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.transpose
def transpose raise Erlang::ImproperListError if improper? return Erlang::Nil if empty? return Erlang::Nil if any? { |list| list.empty? } heads, tails = Erlang::Nil, Erlang::Nil reverse_each { |list| heads, tails = heads.cons(list.head), tails.cons(list.tail) } return Erlang::Cons.new(heads, tails.transpose) end
ruby
def transpose raise Erlang::ImproperListError if improper? return Erlang::Nil if empty? return Erlang::Nil if any? { |list| list.empty? } heads, tails = Erlang::Nil, Erlang::Nil reverse_each { |list| heads, tails = heads.cons(list.head), tails.cons(list.tail) } return Erlang::Cons.new(heads, tails.transpose) end
[ "def", "transpose", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "return", "Erlang", "::", "Nil", "if", "empty?", "return", "Erlang", "::", "Nil", "if", "any?", "{", "|", "list", "|", "list", ".", "empty?", "}", "heads", ",", "tails", "=", "Erlang", "::", "Nil", ",", "Erlang", "::", "Nil", "reverse_each", "{", "|", "list", "|", "heads", ",", "tails", "=", "heads", ".", "cons", "(", "list", ".", "head", ")", ",", "tails", ".", "cons", "(", "list", ".", "tail", ")", "}", "return", "Erlang", "::", "Cons", ".", "new", "(", "heads", ",", "tails", ".", "transpose", ")", "end" ]
Gather the first element of each nested list into a new `List`, then the second element of each nested list, then the third, and so on. In other words, if each nested list is a "row", return a `List` of "columns" instead. @return [List]
[ "Gather", "the", "first", "element", "of", "each", "nested", "list", "into", "a", "new", "List", "then", "the", "second", "element", "of", "each", "nested", "list", "then", "the", "third", "and", "so", "on", ".", "In", "other", "words", "if", "each", "nested", "list", "is", "a", "row", "return", "a", "List", "of", "columns", "instead", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L490-L497
2,452
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.rotate
def rotate(count = 1) raise Erlang::ImproperListError if improper? raise TypeError, "expected Integer" if not count.is_a?(Integer) return self if empty? || (count % size) == 0 count = (count >= 0) ? count % size : (size - (~count % size) - 1) return drop(count).append(take(count)) end
ruby
def rotate(count = 1) raise Erlang::ImproperListError if improper? raise TypeError, "expected Integer" if not count.is_a?(Integer) return self if empty? || (count % size) == 0 count = (count >= 0) ? count % size : (size - (~count % size) - 1) return drop(count).append(take(count)) end
[ "def", "rotate", "(", "count", "=", "1", ")", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "raise", "TypeError", ",", "\"expected Integer\"", "if", "not", "count", ".", "is_a?", "(", "Integer", ")", "return", "self", "if", "empty?", "||", "(", "count", "%", "size", ")", "==", "0", "count", "=", "(", "count", ">=", "0", ")", "?", "count", "%", "size", ":", "(", "size", "-", "(", "~", "count", "%", "size", ")", "-", "1", ")", "return", "drop", "(", "count", ")", ".", "append", "(", "take", "(", "count", ")", ")", "end" ]
Return a new `List` with the same elements, but rotated so that the one at index `count` is the first element of the new list. If `count` is positive, the elements will be shifted left, and those shifted past the lowest position will be moved to the end. If `count` is negative, the elements will be shifted right, and those shifted past the last position will be moved to the beginning. @example l = Erlang::List["A", "B", "C", "D", "E", "F"] l.rotate(2) # => Erlang::List["C", "D", "E", "F", "A", "B"] l.rotate(-1) # => Erlang::List["F", "A", "B", "C", "D", "E"] @param count [Integer] The number of positions to shift items by @return [List] @raise [TypeError] if count is not an integer.
[ "Return", "a", "new", "List", "with", "the", "same", "elements", "but", "rotated", "so", "that", "the", "one", "at", "index", "count", "is", "the", "first", "element", "of", "the", "new", "list", ".", "If", "count", "is", "positive", "the", "elements", "will", "be", "shifted", "left", "and", "those", "shifted", "past", "the", "lowest", "position", "will", "be", "moved", "to", "the", "end", ".", "If", "count", "is", "negative", "the", "elements", "will", "be", "shifted", "right", "and", "those", "shifted", "past", "the", "last", "position", "will", "be", "moved", "to", "the", "beginning", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L513-L519
2,453
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.sort
def sort(&comparator) comparator = Erlang.method(:compare) unless block_given? array = super(&comparator) return List.from_enum(array) end
ruby
def sort(&comparator) comparator = Erlang.method(:compare) unless block_given? array = super(&comparator) return List.from_enum(array) end
[ "def", "sort", "(", "&", "comparator", ")", "comparator", "=", "Erlang", ".", "method", "(", ":compare", ")", "unless", "block_given?", "array", "=", "super", "(", "comparator", ")", "return", "List", ".", "from_enum", "(", "array", ")", "end" ]
Return a new `List` with the same items, but sorted. @overload sort Compare elements with their natural sort key (`#<=>`). @example Erlang::List["Elephant", "Dog", "Lion"].sort # => Erlang::List["Dog", "Elephant", "Lion"] @overload sort Uses the block as a comparator to determine sorted order. @yield [a, b] Any number of times with different pairs of elements. @yieldreturn [Integer] Negative if the first element should be sorted lower, positive if the latter element, or 0 if equal. @example Erlang::List["Elephant", "Dog", "Lion"].sort { |a,b| a.size <=> b.size } # => Erlang::List["Dog", "Lion", "Elephant"] @return [List]
[ "Return", "a", "new", "List", "with", "the", "same", "items", "but", "sorted", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L613-L617
2,454
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.intersperse
def intersperse(sep) raise Erlang::ImproperListError if improper? return self if tail.empty? sep = Erlang.from(sep) out = tail = Erlang::Cons.allocate list = self until list.empty? new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, false) if not list.tail.empty? sep_node = Erlang::Cons.allocate sep_node.instance_variable_set(:@head, sep) sep_node.instance_variable_set(:@improper, false) new_node.instance_variable_set(:@tail, sep_node) new_node.immutable! end tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, false) tail.immutable! if list.tail.empty? tail = new_node else tail = new_node.tail end list = list.tail end if not tail.immutable? tail.instance_variable_set(:@tail, Erlang::Nil) tail.immutable! end return out.tail end
ruby
def intersperse(sep) raise Erlang::ImproperListError if improper? return self if tail.empty? sep = Erlang.from(sep) out = tail = Erlang::Cons.allocate list = self until list.empty? new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, false) if not list.tail.empty? sep_node = Erlang::Cons.allocate sep_node.instance_variable_set(:@head, sep) sep_node.instance_variable_set(:@improper, false) new_node.instance_variable_set(:@tail, sep_node) new_node.immutable! end tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, false) tail.immutable! if list.tail.empty? tail = new_node else tail = new_node.tail end list = list.tail end if not tail.immutable? tail.instance_variable_set(:@tail, Erlang::Nil) tail.immutable! end return out.tail end
[ "def", "intersperse", "(", "sep", ")", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "return", "self", "if", "tail", ".", "empty?", "sep", "=", "Erlang", ".", "from", "(", "sep", ")", "out", "=", "tail", "=", "Erlang", "::", "Cons", ".", "allocate", "list", "=", "self", "until", "list", ".", "empty?", "new_node", "=", "Erlang", "::", "Cons", ".", "allocate", "new_node", ".", "instance_variable_set", "(", ":@head", ",", "list", ".", "head", ")", "new_node", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "if", "not", "list", ".", "tail", ".", "empty?", "sep_node", "=", "Erlang", "::", "Cons", ".", "allocate", "sep_node", ".", "instance_variable_set", "(", ":@head", ",", "sep", ")", "sep_node", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "new_node", ".", "instance_variable_set", "(", ":@tail", ",", "sep_node", ")", "new_node", ".", "immutable!", "end", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "new_node", ")", "tail", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "tail", ".", "immutable!", "if", "list", ".", "tail", ".", "empty?", "tail", "=", "new_node", "else", "tail", "=", "new_node", ".", "tail", "end", "list", "=", "list", ".", "tail", "end", "if", "not", "tail", ".", "immutable?", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "Erlang", "::", "Nil", ")", "tail", ".", "immutable!", "end", "return", "out", ".", "tail", "end" ]
Return a new `List` with `sep` inserted between each of the existing elements. @example Erlang::List["one", "two", "three"].intersperse(" ") # => Erlang::List["one", " ", "two", " ", "three"] @return [List]
[ "Return", "a", "new", "List", "with", "sep", "inserted", "between", "each", "of", "the", "existing", "elements", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L645-L677
2,455
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.union
def union(other) raise Erlang::ImproperListError if improper? other = Erlang.from(other) raise ArgumentError, "other must be of Erlang::List type" if not Erlang.is_list(other) raise Erlang::ImproperListError if other.improper? items = ::Set.new return _uniq(items).append(other._uniq(items)) end
ruby
def union(other) raise Erlang::ImproperListError if improper? other = Erlang.from(other) raise ArgumentError, "other must be of Erlang::List type" if not Erlang.is_list(other) raise Erlang::ImproperListError if other.improper? items = ::Set.new return _uniq(items).append(other._uniq(items)) end
[ "def", "union", "(", "other", ")", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "other", "=", "Erlang", ".", "from", "(", "other", ")", "raise", "ArgumentError", ",", "\"other must be of Erlang::List type\"", "if", "not", "Erlang", ".", "is_list", "(", "other", ")", "raise", "Erlang", "::", "ImproperListError", "if", "other", ".", "improper?", "items", "=", "::", "Set", ".", "new", "return", "_uniq", "(", "items", ")", ".", "append", "(", "other", ".", "_uniq", "(", "items", ")", ")", "end" ]
Return a `List` with all the elements from both this list and `other`, with all duplicates removed. @example Erlang::List[1, 2].union(Erlang::List[2, 3]) # => Erlang::List[1, 2, 3] @param other [List] The list to merge with @return [List]
[ "Return", "a", "List", "with", "all", "the", "elements", "from", "both", "this", "list", "and", "other", "with", "all", "duplicates", "removed", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L750-L757
2,456
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.init
def init raise Erlang::ImproperListError if improper? return Erlang::Nil if tail.empty? out = tail = Erlang::Cons.allocate list = self until list.tail.empty? new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, false) tail.immutable! tail = new_node list = list.tail end if not tail.immutable? tail.instance_variable_set(:@tail, Erlang::Nil) tail.immutable! end return out.tail end
ruby
def init raise Erlang::ImproperListError if improper? return Erlang::Nil if tail.empty? out = tail = Erlang::Cons.allocate list = self until list.tail.empty? new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, false) tail.immutable! tail = new_node list = list.tail end if not tail.immutable? tail.instance_variable_set(:@tail, Erlang::Nil) tail.immutable! end return out.tail end
[ "def", "init", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "return", "Erlang", "::", "Nil", "if", "tail", ".", "empty?", "out", "=", "tail", "=", "Erlang", "::", "Cons", ".", "allocate", "list", "=", "self", "until", "list", ".", "tail", ".", "empty?", "new_node", "=", "Erlang", "::", "Cons", ".", "allocate", "new_node", ".", "instance_variable_set", "(", ":@head", ",", "list", ".", "head", ")", "new_node", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "new_node", ")", "tail", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "tail", ".", "immutable!", "tail", "=", "new_node", "list", "=", "list", ".", "tail", "end", "if", "not", "tail", ".", "immutable?", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "Erlang", "::", "Nil", ")", "tail", ".", "immutable!", "end", "return", "out", ".", "tail", "end" ]
Return a `List` with all elements except the last one. @example Erlang::List["a", "b", "c"].init # => Erlang::List["a", "b"] @return [List]
[ "Return", "a", "List", "with", "all", "elements", "except", "the", "last", "one", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L766-L786
2,457
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.last
def last(allow_improper = false) if allow_improper and improper? list = self list = list.tail while list.tail.kind_of?(Erlang::List) return list.tail else raise Erlang::ImproperListError if improper? list = self list = list.tail until list.tail.empty? return list.head end end
ruby
def last(allow_improper = false) if allow_improper and improper? list = self list = list.tail while list.tail.kind_of?(Erlang::List) return list.tail else raise Erlang::ImproperListError if improper? list = self list = list.tail until list.tail.empty? return list.head end end
[ "def", "last", "(", "allow_improper", "=", "false", ")", "if", "allow_improper", "and", "improper?", "list", "=", "self", "list", "=", "list", ".", "tail", "while", "list", ".", "tail", ".", "kind_of?", "(", "Erlang", "::", "List", ")", "return", "list", ".", "tail", "else", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "list", "=", "self", "list", "=", "list", ".", "tail", "until", "list", ".", "tail", ".", "empty?", "return", "list", ".", "head", "end", "end" ]
Return the last item in this list. @return [Object]
[ "Return", "the", "last", "item", "in", "this", "list", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L790-L801
2,458
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.tails
def tails raise Erlang::ImproperListError if improper? return self if empty? out = tail = Erlang::Cons.allocate list = self until list.empty? new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list) new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@tail, new_node) list = list.tail tail = new_node end tail.instance_variable_set(:@tail, Erlang::Nil) return out.tail end
ruby
def tails raise Erlang::ImproperListError if improper? return self if empty? out = tail = Erlang::Cons.allocate list = self until list.empty? new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list) new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@tail, new_node) list = list.tail tail = new_node end tail.instance_variable_set(:@tail, Erlang::Nil) return out.tail end
[ "def", "tails", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "return", "self", "if", "empty?", "out", "=", "tail", "=", "Erlang", "::", "Cons", ".", "allocate", "list", "=", "self", "until", "list", ".", "empty?", "new_node", "=", "Erlang", "::", "Cons", ".", "allocate", "new_node", ".", "instance_variable_set", "(", ":@head", ",", "list", ")", "new_node", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "new_node", ")", "list", "=", "list", ".", "tail", "tail", "=", "new_node", "end", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "Erlang", "::", "Nil", ")", "return", "out", ".", "tail", "end" ]
Return a `List` of all suffixes of this list. @example Erlang::List[1,2,3].tails # => Erlang::List[ # Erlang::List[1, 2, 3], # Erlang::List[2, 3], # Erlang::List[3]] @return [List]
[ "Return", "a", "List", "of", "all", "suffixes", "of", "this", "list", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L813-L828
2,459
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.inits
def inits raise Erlang::ImproperListError if improper? return self if empty? prev = nil return map do |head| if prev.nil? Erlang::List.from_enum(prev = [head]) else Erlang::List.from_enum(prev.push(head)) end end end
ruby
def inits raise Erlang::ImproperListError if improper? return self if empty? prev = nil return map do |head| if prev.nil? Erlang::List.from_enum(prev = [head]) else Erlang::List.from_enum(prev.push(head)) end end end
[ "def", "inits", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "return", "self", "if", "empty?", "prev", "=", "nil", "return", "map", "do", "|", "head", "|", "if", "prev", ".", "nil?", "Erlang", "::", "List", ".", "from_enum", "(", "prev", "=", "[", "head", "]", ")", "else", "Erlang", "::", "List", ".", "from_enum", "(", "prev", ".", "push", "(", "head", ")", ")", "end", "end", "end" ]
Return a `List` of all prefixes of this list. @example Erlang::List[1,2,3].inits # => Erlang::List[ # Erlang::List[1], # Erlang::List[1, 2], # Erlang::List[1, 2, 3]] @return [List]
[ "Return", "a", "List", "of", "all", "prefixes", "of", "this", "list", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L840-L851
2,460
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.combination
def combination(n) raise Erlang::ImproperListError if improper? return Erlang::Cons.new(Erlang::Nil) if n == 0 return self if empty? return tail.combination(n - 1).map { |list| list.cons(head) }.append(tail.combination(n)) end
ruby
def combination(n) raise Erlang::ImproperListError if improper? return Erlang::Cons.new(Erlang::Nil) if n == 0 return self if empty? return tail.combination(n - 1).map { |list| list.cons(head) }.append(tail.combination(n)) end
[ "def", "combination", "(", "n", ")", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "return", "Erlang", "::", "Cons", ".", "new", "(", "Erlang", "::", "Nil", ")", "if", "n", "==", "0", "return", "self", "if", "empty?", "return", "tail", ".", "combination", "(", "n", "-", "1", ")", ".", "map", "{", "|", "list", "|", "list", ".", "cons", "(", "head", ")", "}", ".", "append", "(", "tail", ".", "combination", "(", "n", ")", ")", "end" ]
Return a `List` of all combinations of length `n` of items from this `List`. @example Erlang::List[1,2,3].combination(2) # => Erlang::List[ # Erlang::List[1, 2], # Erlang::List[1, 3], # Erlang::List[2, 3]] @return [List]
[ "Return", "a", "List", "of", "all", "combinations", "of", "length", "n", "of", "items", "from", "this", "List", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L863-L868
2,461
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.chunk
def chunk(number) raise Erlang::ImproperListError if improper? return self if empty? out = tail = Erlang::Cons.allocate list = self until list.empty? first, list = list.split_at(number) new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, first) new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, false) tail.immutable! tail = new_node end if not tail.immutable? tail.instance_variable_set(:@tail, Erlang::Nil) tail.immutable! end return out.tail end
ruby
def chunk(number) raise Erlang::ImproperListError if improper? return self if empty? out = tail = Erlang::Cons.allocate list = self until list.empty? first, list = list.split_at(number) new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, first) new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, false) tail.immutable! tail = new_node end if not tail.immutable? tail.instance_variable_set(:@tail, Erlang::Nil) tail.immutable! end return out.tail end
[ "def", "chunk", "(", "number", ")", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "return", "self", "if", "empty?", "out", "=", "tail", "=", "Erlang", "::", "Cons", ".", "allocate", "list", "=", "self", "until", "list", ".", "empty?", "first", ",", "list", "=", "list", ".", "split_at", "(", "number", ")", "new_node", "=", "Erlang", "::", "Cons", ".", "allocate", "new_node", ".", "instance_variable_set", "(", ":@head", ",", "first", ")", "new_node", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "new_node", ")", "tail", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "tail", ".", "immutable!", "tail", "=", "new_node", "end", "if", "not", "tail", ".", "immutable?", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "Erlang", "::", "Nil", ")", "tail", ".", "immutable!", "end", "return", "out", ".", "tail", "end" ]
Split the items in this list in groups of `number`. Return a list of lists. @example ("a".."o").to_list.chunk(5) # => Erlang::List[ # Erlang::List["a", "b", "c", "d", "e"], # Erlang::List["f", "g", "h", "i", "j"], # Erlang::List["k", "l", "m", "n", "o"]] @return [List]
[ "Split", "the", "items", "in", "this", "list", "in", "groups", "of", "number", ".", "Return", "a", "list", "of", "lists", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L880-L900
2,462
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.flatten
def flatten raise Erlang::ImproperListError if improper? return self if empty? out = tail = Erlang::Cons.allocate list = self until list.empty? if list.head.is_a?(Erlang::Cons) list = list.head.append(list.tail) elsif Erlang::Nil.equal?(list.head) list = list.tail else new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@tail, new_node) tail.immutable! list = list.tail tail = new_node end end if not tail.immutable? tail.instance_variable_set(:@tail, Erlang::Nil) tail.immutable! end return out.tail end
ruby
def flatten raise Erlang::ImproperListError if improper? return self if empty? out = tail = Erlang::Cons.allocate list = self until list.empty? if list.head.is_a?(Erlang::Cons) list = list.head.append(list.tail) elsif Erlang::Nil.equal?(list.head) list = list.tail else new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@tail, new_node) tail.immutable! list = list.tail tail = new_node end end if not tail.immutable? tail.instance_variable_set(:@tail, Erlang::Nil) tail.immutable! end return out.tail end
[ "def", "flatten", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "return", "self", "if", "empty?", "out", "=", "tail", "=", "Erlang", "::", "Cons", ".", "allocate", "list", "=", "self", "until", "list", ".", "empty?", "if", "list", ".", "head", ".", "is_a?", "(", "Erlang", "::", "Cons", ")", "list", "=", "list", ".", "head", ".", "append", "(", "list", ".", "tail", ")", "elsif", "Erlang", "::", "Nil", ".", "equal?", "(", "list", ".", "head", ")", "list", "=", "list", ".", "tail", "else", "new_node", "=", "Erlang", "::", "Cons", ".", "allocate", "new_node", ".", "instance_variable_set", "(", ":@head", ",", "list", ".", "head", ")", "new_node", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "new_node", ")", "tail", ".", "immutable!", "list", "=", "list", ".", "tail", "tail", "=", "new_node", "end", "end", "if", "not", "tail", ".", "immutable?", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "Erlang", "::", "Nil", ")", "tail", ".", "immutable!", "end", "return", "out", ".", "tail", "end" ]
Return a new `List` with all nested lists recursively "flattened out", that is, their elements inserted into the new `List` in the place where the nested list originally was. @example Erlang::List[Erlang::List[1, 2], Erlang::List[3, 4]].flatten # => Erlang::List[1, 2, 3, 4] @return [List]
[ "Return", "a", "new", "List", "with", "all", "nested", "lists", "recursively", "flattened", "out", "that", "is", "their", "elements", "inserted", "into", "the", "new", "List", "in", "the", "place", "where", "the", "nested", "list", "originally", "was", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L925-L950
2,463
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.slice
def slice(arg, length = (missing_length = true)) raise Erlang::ImproperListError if improper? if missing_length if arg.is_a?(Range) from, to = arg.begin, arg.end from += size if from < 0 return nil if from < 0 to += size if to < 0 to += 1 if !arg.exclude_end? length = to - from length = 0 if length < 0 list = self while from > 0 return nil if list.empty? list = list.tail from -= 1 end return list.take(length) else return at(arg) end else return nil if length < 0 arg += size if arg < 0 return nil if arg < 0 list = self while arg > 0 return nil if list.empty? list = list.tail arg -= 1 end return list.take(length) end end
ruby
def slice(arg, length = (missing_length = true)) raise Erlang::ImproperListError if improper? if missing_length if arg.is_a?(Range) from, to = arg.begin, arg.end from += size if from < 0 return nil if from < 0 to += size if to < 0 to += 1 if !arg.exclude_end? length = to - from length = 0 if length < 0 list = self while from > 0 return nil if list.empty? list = list.tail from -= 1 end return list.take(length) else return at(arg) end else return nil if length < 0 arg += size if arg < 0 return nil if arg < 0 list = self while arg > 0 return nil if list.empty? list = list.tail arg -= 1 end return list.take(length) end end
[ "def", "slice", "(", "arg", ",", "length", "=", "(", "missing_length", "=", "true", ")", ")", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "if", "missing_length", "if", "arg", ".", "is_a?", "(", "Range", ")", "from", ",", "to", "=", "arg", ".", "begin", ",", "arg", ".", "end", "from", "+=", "size", "if", "from", "<", "0", "return", "nil", "if", "from", "<", "0", "to", "+=", "size", "if", "to", "<", "0", "to", "+=", "1", "if", "!", "arg", ".", "exclude_end?", "length", "=", "to", "-", "from", "length", "=", "0", "if", "length", "<", "0", "list", "=", "self", "while", "from", ">", "0", "return", "nil", "if", "list", ".", "empty?", "list", "=", "list", ".", "tail", "from", "-=", "1", "end", "return", "list", ".", "take", "(", "length", ")", "else", "return", "at", "(", "arg", ")", "end", "else", "return", "nil", "if", "length", "<", "0", "arg", "+=", "size", "if", "arg", "<", "0", "return", "nil", "if", "arg", "<", "0", "list", "=", "self", "while", "arg", ">", "0", "return", "nil", "if", "list", ".", "empty?", "list", "=", "list", ".", "tail", "arg", "-=", "1", "end", "return", "list", ".", "take", "(", "length", ")", "end", "end" ]
Return specific objects from the `List`. All overloads return `nil` if the starting index is out of range. @overload list.slice(index) Returns a single object at the given `index`. If `index` is negative, count backwards from the end. @param index [Integer] The index to retrieve. May be negative. @return [Object] @example l = Erlang::List["A", "B", "C", "D", "E", "F"] l[2] # => "C" l[-1] # => "F" l[6] # => nil @overload list.slice(index, length) Return a sublist starting at `index` and continuing for `length` elements or until the end of the `List`, whichever occurs first. @param start [Integer] The index to start retrieving items from. May be negative. @param length [Integer] The number of items to retrieve. @return [List] @example l = Erlang::List["A", "B", "C", "D", "E", "F"] l[2, 3] # => Erlang::List["C", "D", "E"] l[-2, 3] # => Erlang::List["E", "F"] l[20, 1] # => nil @overload list.slice(index..end) Return a sublist starting at `index` and continuing to index `end` or the end of the `List`, whichever occurs first. @param range [Range] The range of indices to retrieve. @return [Vector] @example l = Erlang::List["A", "B", "C", "D", "E", "F"] l[2..3] # => Erlang::List["C", "D"] l[-2..100] # => Erlang::List["E", "F"] l[20..21] # => nil
[ "Return", "specific", "objects", "from", "the", "List", ".", "All", "overloads", "return", "nil", "if", "the", "starting", "index", "is", "out", "of", "range", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L1027-L1060
2,464
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.indices
def indices(object = Erlang::Undefined, i = 0, &block) raise Erlang::ImproperListError if improper? object = Erlang.from(object) if object != Erlang::Undefined return indices { |item| item == object } if not block_given? return Erlang::Nil if empty? out = tail = Erlang::Cons.allocate list = self until list.empty? if yield(list.head) new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, i) new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, false) tail.immutable! tail = new_node list = list.tail else list = list.tail end i += 1 end if not tail.immutable? tail.instance_variable_set(:@tail, Erlang::Nil) tail.immutable! end return out.tail end
ruby
def indices(object = Erlang::Undefined, i = 0, &block) raise Erlang::ImproperListError if improper? object = Erlang.from(object) if object != Erlang::Undefined return indices { |item| item == object } if not block_given? return Erlang::Nil if empty? out = tail = Erlang::Cons.allocate list = self until list.empty? if yield(list.head) new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, i) new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, false) tail.immutable! tail = new_node list = list.tail else list = list.tail end i += 1 end if not tail.immutable? tail.instance_variable_set(:@tail, Erlang::Nil) tail.immutable! end return out.tail end
[ "def", "indices", "(", "object", "=", "Erlang", "::", "Undefined", ",", "i", "=", "0", ",", "&", "block", ")", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "object", "=", "Erlang", ".", "from", "(", "object", ")", "if", "object", "!=", "Erlang", "::", "Undefined", "return", "indices", "{", "|", "item", "|", "item", "==", "object", "}", "if", "not", "block_given?", "return", "Erlang", "::", "Nil", "if", "empty?", "out", "=", "tail", "=", "Erlang", "::", "Cons", ".", "allocate", "list", "=", "self", "until", "list", ".", "empty?", "if", "yield", "(", "list", ".", "head", ")", "new_node", "=", "Erlang", "::", "Cons", ".", "allocate", "new_node", ".", "instance_variable_set", "(", ":@head", ",", "i", ")", "new_node", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "new_node", ")", "tail", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "tail", ".", "immutable!", "tail", "=", "new_node", "list", "=", "list", ".", "tail", "else", "list", "=", "list", ".", "tail", "end", "i", "+=", "1", "end", "if", "not", "tail", ".", "immutable?", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "Erlang", "::", "Nil", ")", "tail", ".", "immutable!", "end", "return", "out", ".", "tail", "end" ]
Return a `List` of indices of matching objects. @overload indices(object) Return a `List` of indices where `object` is found. Use `#==` for testing equality. @example Erlang::List[1, 2, 3, 4].indices(2) # => Erlang::List[1] @overload indices Pass each item successively to the block. Return a list of indices where the block returns true. @yield [item] @example Erlang::List[1, 2, 3, 4].indices { |e| e.even? } # => Erlang::List[1, 3] @return [List]
[ "Return", "a", "List", "of", "indices", "of", "matching", "objects", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L1083-L1110
2,465
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.merge
def merge(&comparator) raise Erlang::ImproperListError if improper? return merge_by unless block_given? sorted = reject(&:empty?).sort do |a, b| yield(a.head, b.head) end return Erlang::Nil if sorted.empty? return Erlang::Cons.new(sorted.head.head, sorted.tail.cons(sorted.head.tail).merge(&comparator)) end
ruby
def merge(&comparator) raise Erlang::ImproperListError if improper? return merge_by unless block_given? sorted = reject(&:empty?).sort do |a, b| yield(a.head, b.head) end return Erlang::Nil if sorted.empty? return Erlang::Cons.new(sorted.head.head, sorted.tail.cons(sorted.head.tail).merge(&comparator)) end
[ "def", "merge", "(", "&", "comparator", ")", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "return", "merge_by", "unless", "block_given?", "sorted", "=", "reject", "(", ":empty?", ")", ".", "sort", "do", "|", "a", ",", "b", "|", "yield", "(", "a", ".", "head", ",", "b", ".", "head", ")", "end", "return", "Erlang", "::", "Nil", "if", "sorted", ".", "empty?", "return", "Erlang", "::", "Cons", ".", "new", "(", "sorted", ".", "head", ".", "head", ",", "sorted", ".", "tail", ".", "cons", "(", "sorted", ".", "head", ".", "tail", ")", ".", "merge", "(", "comparator", ")", ")", "end" ]
Merge all the nested lists into a single list, using the given comparator block to determine the order which items should be shifted out of the nested lists and into the output list. @example list_1 = Erlang::List[1, -3, -5] list_2 = Erlang::List[-2, 4, 6] Erlang::List[list_1, list_2].merge { |a,b| a.abs <=> b.abs } # => Erlang::List[1, -2, -3, 4, -5, 6] @return [List] @yield [a, b] Pairs of items from matching indices in each list. @yieldreturn [Integer] Negative if the first element should be selected first, positive if the latter element, or zero if either.
[ "Merge", "all", "the", "nested", "lists", "into", "a", "single", "list", "using", "the", "given", "comparator", "block", "to", "determine", "the", "order", "which", "items", "should", "be", "shifted", "out", "of", "the", "nested", "lists", "and", "into", "the", "output", "list", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L1127-L1135
2,466
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.insert
def insert(index, *items) raise Erlang::ImproperListError if improper? if index == 0 return Erlang::List.from_enum(items).append(self) elsif index > 0 out = tail = Erlang::Cons.allocate list = self while index > 0 new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, false) tail.immutable! tail = new_node list = list.tail index -= 1 end if not tail.immutable? tail.instance_variable_set(:@tail, Erlang::List.from_enum(items).append(list)) tail.immutable! end return out.tail else raise IndexError if index < -size return insert(index + size, *items) end end
ruby
def insert(index, *items) raise Erlang::ImproperListError if improper? if index == 0 return Erlang::List.from_enum(items).append(self) elsif index > 0 out = tail = Erlang::Cons.allocate list = self while index > 0 new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, false) tail.immutable! tail = new_node list = list.tail index -= 1 end if not tail.immutable? tail.instance_variable_set(:@tail, Erlang::List.from_enum(items).append(list)) tail.immutable! end return out.tail else raise IndexError if index < -size return insert(index + size, *items) end end
[ "def", "insert", "(", "index", ",", "*", "items", ")", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "if", "index", "==", "0", "return", "Erlang", "::", "List", ".", "from_enum", "(", "items", ")", ".", "append", "(", "self", ")", "elsif", "index", ">", "0", "out", "=", "tail", "=", "Erlang", "::", "Cons", ".", "allocate", "list", "=", "self", "while", "index", ">", "0", "new_node", "=", "Erlang", "::", "Cons", ".", "allocate", "new_node", ".", "instance_variable_set", "(", ":@head", ",", "list", ".", "head", ")", "new_node", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "new_node", ")", "tail", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "tail", ".", "immutable!", "tail", "=", "new_node", "list", "=", "list", ".", "tail", "index", "-=", "1", "end", "if", "not", "tail", ".", "immutable?", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "Erlang", "::", "List", ".", "from_enum", "(", "items", ")", ".", "append", "(", "list", ")", ")", "tail", ".", "immutable!", "end", "return", "out", ".", "tail", "else", "raise", "IndexError", "if", "index", "<", "-", "size", "return", "insert", "(", "index", "+", "size", ",", "items", ")", "end", "end" ]
Return a new `List` with the given items inserted before the item at `index`. @example Erlang::List["A", "D", "E"].insert(1, "B", "C") # => Erlang::List["A", "B", "C", "D", "E"] @param index [Integer] The index where the new items should go @param items [Array] The items to add @return [List]
[ "Return", "a", "new", "List", "with", "the", "given", "items", "inserted", "before", "the", "item", "at", "index", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L1176-L1203
2,467
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.delete_at
def delete_at(index) raise Erlang::ImproperListError if improper? if index == 0 tail elsif index < 0 index += size if index < 0 return self if index < 0 delete_at(index) else out = tail = Erlang::Cons.allocate list = self while index > 0 new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, false) tail.immutable! tail = new_node list = list.tail index -= 1 end if not tail.immutable? tail.instance_variable_set(:@tail, list.tail) tail.immutable! end return out.tail end end
ruby
def delete_at(index) raise Erlang::ImproperListError if improper? if index == 0 tail elsif index < 0 index += size if index < 0 return self if index < 0 delete_at(index) else out = tail = Erlang::Cons.allocate list = self while index > 0 new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, false) tail.instance_variable_set(:@tail, new_node) tail.instance_variable_set(:@improper, false) tail.immutable! tail = new_node list = list.tail index -= 1 end if not tail.immutable? tail.instance_variable_set(:@tail, list.tail) tail.immutable! end return out.tail end end
[ "def", "delete_at", "(", "index", ")", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "if", "index", "==", "0", "tail", "elsif", "index", "<", "0", "index", "+=", "size", "if", "index", "<", "0", "return", "self", "if", "index", "<", "0", "delete_at", "(", "index", ")", "else", "out", "=", "tail", "=", "Erlang", "::", "Cons", ".", "allocate", "list", "=", "self", "while", "index", ">", "0", "new_node", "=", "Erlang", "::", "Cons", ".", "allocate", "new_node", ".", "instance_variable_set", "(", ":@head", ",", "list", ".", "head", ")", "new_node", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "new_node", ")", "tail", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "tail", ".", "immutable!", "tail", "=", "new_node", "list", "=", "list", ".", "tail", "index", "-=", "1", "end", "if", "not", "tail", ".", "immutable?", "tail", ".", "instance_variable_set", "(", ":@tail", ",", "list", ".", "tail", ")", "tail", ".", "immutable!", "end", "return", "out", ".", "tail", "end", "end" ]
Return a `List` containing the same items, minus the one at `index`. If `index` is negative, it counts back from the end of the list. @example Erlang::List[1, 2, 3].delete_at(1) # => Erlang::List[1, 3] Erlang::List[1, 2, 3].delete_at(-1) # => Erlang::List[1, 2] @param index [Integer] The index of the item to remove @return [List]
[ "Return", "a", "List", "containing", "the", "same", "items", "minus", "the", "one", "at", "index", ".", "If", "index", "is", "negative", "it", "counts", "back", "from", "the", "end", "of", "the", "list", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L1250-L1278
2,468
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.permutation
def permutation(length = size, &block) raise Erlang::ImproperListError if improper? return enum_for(:permutation, length) if not block_given? if length == 0 yield Erlang::Nil elsif length == 1 each { |obj| yield Erlang::Cons.new(obj, Erlang::Nil) } elsif not empty? if length < size tail.permutation(length, &block) end tail.permutation(length-1) do |p| 0.upto(length-1) do |i| left,right = p.split_at(i) yield left.append(right.cons(head)) end end end return self end
ruby
def permutation(length = size, &block) raise Erlang::ImproperListError if improper? return enum_for(:permutation, length) if not block_given? if length == 0 yield Erlang::Nil elsif length == 1 each { |obj| yield Erlang::Cons.new(obj, Erlang::Nil) } elsif not empty? if length < size tail.permutation(length, &block) end tail.permutation(length-1) do |p| 0.upto(length-1) do |i| left,right = p.split_at(i) yield left.append(right.cons(head)) end end end return self end
[ "def", "permutation", "(", "length", "=", "size", ",", "&", "block", ")", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "return", "enum_for", "(", ":permutation", ",", "length", ")", "if", "not", "block_given?", "if", "length", "==", "0", "yield", "Erlang", "::", "Nil", "elsif", "length", "==", "1", "each", "{", "|", "obj", "|", "yield", "Erlang", "::", "Cons", ".", "new", "(", "obj", ",", "Erlang", "::", "Nil", ")", "}", "elsif", "not", "empty?", "if", "length", "<", "size", "tail", ".", "permutation", "(", "length", ",", "block", ")", "end", "tail", ".", "permutation", "(", "length", "-", "1", ")", "do", "|", "p", "|", "0", ".", "upto", "(", "length", "-", "1", ")", "do", "|", "i", "|", "left", ",", "right", "=", "p", ".", "split_at", "(", "i", ")", "yield", "left", ".", "append", "(", "right", ".", "cons", "(", "head", ")", ")", "end", "end", "end", "return", "self", "end" ]
Yields all permutations of length `n` of the items in the list, and then returns `self`. If no length `n` is specified, permutations of the entire list will be yielded. There is no guarantee about which order the permutations will be yielded in. If no block is given, an `Enumerator` is returned instead. @example Erlang::List[1, 2, 3].permutation.to_a # => [Erlang::List[1, 2, 3], # Erlang::List[2, 1, 3], # Erlang::List[2, 3, 1], # Erlang::List[1, 3, 2], # Erlang::List[3, 1, 2], # Erlang::List[3, 2, 1]] @return [self, Enumerator] @yield [list] Once for each permutation.
[ "Yields", "all", "permutations", "of", "length", "n", "of", "the", "items", "in", "the", "list", "and", "then", "returns", "self", ".", "If", "no", "length", "n", "is", "specified", "permutations", "of", "the", "entire", "list", "will", "be", "yielded", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L1400-L1420
2,469
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.partition
def partition(&block) raise Erlang::ImproperListError if improper? return enum_for(:partition) if not block_given? left = left_tail = Erlang::Cons.allocate right = right_tail = Erlang::Cons.allocate list = self while !list.empty? if yield(list.head) new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, false) left_tail.instance_variable_set(:@tail, new_node) left_tail.instance_variable_set(:@improper, false) left_tail.immutable! left_tail = new_node list = list.tail else new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, false) right_tail.instance_variable_set(:@tail, new_node) right_tail.instance_variable_set(:@improper, false) right_tail.immutable! right_tail = new_node list = list.tail end end if not left_tail.immutable? left_tail.instance_variable_set(:@tail, Erlang::Nil) left_tail.immutable! end if not right_tail.immutable? right_tail.instance_variable_set(:@tail, Erlang::Nil) right_tail.immutable! end return Erlang::Tuple[left.tail, right.tail] end
ruby
def partition(&block) raise Erlang::ImproperListError if improper? return enum_for(:partition) if not block_given? left = left_tail = Erlang::Cons.allocate right = right_tail = Erlang::Cons.allocate list = self while !list.empty? if yield(list.head) new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, false) left_tail.instance_variable_set(:@tail, new_node) left_tail.instance_variable_set(:@improper, false) left_tail.immutable! left_tail = new_node list = list.tail else new_node = Erlang::Cons.allocate new_node.instance_variable_set(:@head, list.head) new_node.instance_variable_set(:@improper, false) right_tail.instance_variable_set(:@tail, new_node) right_tail.instance_variable_set(:@improper, false) right_tail.immutable! right_tail = new_node list = list.tail end end if not left_tail.immutable? left_tail.instance_variable_set(:@tail, Erlang::Nil) left_tail.immutable! end if not right_tail.immutable? right_tail.instance_variable_set(:@tail, Erlang::Nil) right_tail.immutable! end return Erlang::Tuple[left.tail, right.tail] end
[ "def", "partition", "(", "&", "block", ")", "raise", "Erlang", "::", "ImproperListError", "if", "improper?", "return", "enum_for", "(", ":partition", ")", "if", "not", "block_given?", "left", "=", "left_tail", "=", "Erlang", "::", "Cons", ".", "allocate", "right", "=", "right_tail", "=", "Erlang", "::", "Cons", ".", "allocate", "list", "=", "self", "while", "!", "list", ".", "empty?", "if", "yield", "(", "list", ".", "head", ")", "new_node", "=", "Erlang", "::", "Cons", ".", "allocate", "new_node", ".", "instance_variable_set", "(", ":@head", ",", "list", ".", "head", ")", "new_node", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "left_tail", ".", "instance_variable_set", "(", ":@tail", ",", "new_node", ")", "left_tail", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "left_tail", ".", "immutable!", "left_tail", "=", "new_node", "list", "=", "list", ".", "tail", "else", "new_node", "=", "Erlang", "::", "Cons", ".", "allocate", "new_node", ".", "instance_variable_set", "(", ":@head", ",", "list", ".", "head", ")", "new_node", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "right_tail", ".", "instance_variable_set", "(", ":@tail", ",", "new_node", ")", "right_tail", ".", "instance_variable_set", "(", ":@improper", ",", "false", ")", "right_tail", ".", "immutable!", "right_tail", "=", "new_node", "list", "=", "list", ".", "tail", "end", "end", "if", "not", "left_tail", ".", "immutable?", "left_tail", ".", "instance_variable_set", "(", ":@tail", ",", "Erlang", "::", "Nil", ")", "left_tail", ".", "immutable!", "end", "if", "not", "right_tail", ".", "immutable?", "right_tail", ".", "instance_variable_set", "(", ":@tail", ",", "Erlang", "::", "Nil", ")", "right_tail", ".", "immutable!", "end", "return", "Erlang", "::", "Tuple", "[", "left", ".", "tail", ",", "right", ".", "tail", "]", "end" ]
Return two `List`s, the first containing all the elements for which the block evaluates to true, the second containing the rest. @example Erlang::List[1, 2, 3, 4, 5, 6].partition { |x| x.even? } # => Erlang::Tuple[Erlang::List[2, 4, 6], Erlang::List[1, 3, 5]] @return [Tuple] @yield [item] Once for each item.
[ "Return", "two", "List", "s", "the", "first", "containing", "all", "the", "elements", "for", "which", "the", "block", "evaluates", "to", "true", "the", "second", "containing", "the", "rest", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L1458-L1494
2,470
potatosalad/ruby-erlang-terms
lib/erlang/list.rb
Erlang.List.inspect
def inspect if improper? result = 'Erlang::List[' list = to_proper_list list.each_with_index { |obj, i| result << ', ' if i > 0; result << obj.inspect } result << ']' result << " + #{last(true).inspect}" return result else result = '[' list = self list.each_with_index { |obj, i| result << ', ' if i > 0; result << obj.inspect } result << ']' return result end end
ruby
def inspect if improper? result = 'Erlang::List[' list = to_proper_list list.each_with_index { |obj, i| result << ', ' if i > 0; result << obj.inspect } result << ']' result << " + #{last(true).inspect}" return result else result = '[' list = self list.each_with_index { |obj, i| result << ', ' if i > 0; result << obj.inspect } result << ']' return result end end
[ "def", "inspect", "if", "improper?", "result", "=", "'Erlang::List['", "list", "=", "to_proper_list", "list", ".", "each_with_index", "{", "|", "obj", ",", "i", "|", "result", "<<", "', '", "if", "i", ">", "0", ";", "result", "<<", "obj", ".", "inspect", "}", "result", "<<", "']'", "result", "<<", "\" + #{last(true).inspect}\"", "return", "result", "else", "result", "=", "'['", "list", "=", "self", "list", ".", "each_with_index", "{", "|", "obj", ",", "i", "|", "result", "<<", "', '", "if", "i", ">", "0", ";", "result", "<<", "obj", ".", "inspect", "}", "result", "<<", "']'", "return", "result", "end", "end" ]
Return the contents of this `List` as a programmer-readable `String`. If all the items in the list are serializable as Ruby literal strings, the returned string can be passed to `eval` to reconstitute an equivalent `List`. @return [::String]
[ "Return", "the", "contents", "of", "this", "List", "as", "a", "programmer", "-", "readable", "String", ".", "If", "all", "the", "items", "in", "the", "list", "are", "serializable", "as", "Ruby", "literal", "strings", "the", "returned", "string", "can", "be", "passed", "to", "eval", "to", "reconstitute", "an", "equivalent", "List", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/list.rb#L1647-L1662
2,471
potatosalad/ruby-erlang-terms
lib/erlang/binary.rb
Erlang.Binary.copy
def copy(n = 1) raise ArgumentError, 'n must be a non-negative Integer' if not n.is_a?(::Integer) or n < 0 return self if n == 1 return Erlang::Binary[(@data * n)] end
ruby
def copy(n = 1) raise ArgumentError, 'n must be a non-negative Integer' if not n.is_a?(::Integer) or n < 0 return self if n == 1 return Erlang::Binary[(@data * n)] end
[ "def", "copy", "(", "n", "=", "1", ")", "raise", "ArgumentError", ",", "'n must be a non-negative Integer'", "if", "not", "n", ".", "is_a?", "(", "::", "Integer", ")", "or", "n", "<", "0", "return", "self", "if", "n", "==", "1", "return", "Erlang", "::", "Binary", "[", "(", "@data", "*", "n", ")", "]", "end" ]
Returns a new `Binary` containing `n` copies of itself. `n` must be greater than or equal to 0. @param n [Integer] The number of copies @return [Binary] @raise [ArgumentError] if `n` is less than 0
[ "Returns", "a", "new", "Binary", "containing", "n", "copies", "of", "itself", ".", "n", "must", "be", "greater", "than", "or", "equal", "to", "0", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/binary.rb#L225-L229
2,472
potatosalad/ruby-erlang-terms
lib/erlang/binary.rb
Erlang.Binary.each_bit
def each_bit return enum_for(:each_bit) unless block_given? index = 0 bitsize = self.bitsize @data.each_byte do |byte| loop do break if index == bitsize bit = (byte >> (7 - (index & 7))) & 1 yield bit index += 1 break if (index & 7) == 0 end end return self end
ruby
def each_bit return enum_for(:each_bit) unless block_given? index = 0 bitsize = self.bitsize @data.each_byte do |byte| loop do break if index == bitsize bit = (byte >> (7 - (index & 7))) & 1 yield bit index += 1 break if (index & 7) == 0 end end return self end
[ "def", "each_bit", "return", "enum_for", "(", ":each_bit", ")", "unless", "block_given?", "index", "=", "0", "bitsize", "=", "self", ".", "bitsize", "@data", ".", "each_byte", "do", "|", "byte", "|", "loop", "do", "break", "if", "index", "==", "bitsize", "bit", "=", "(", "byte", ">>", "(", "7", "-", "(", "index", "&", "7", ")", ")", ")", "&", "1", "yield", "bit", "index", "+=", "1", "break", "if", "(", "index", "&", "7", ")", "==", "0", "end", "end", "return", "self", "end" ]
Call the given block once for each bit in the `Binary`, passing each bit from first to last successively to the block. If no block is given, returns an `Enumerator`. @return [self] @yield [Integer]
[ "Call", "the", "given", "block", "once", "for", "each", "bit", "in", "the", "Binary", "passing", "each", "bit", "from", "first", "to", "last", "successively", "to", "the", "block", ".", "If", "no", "block", "is", "given", "returns", "an", "Enumerator", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/binary.rb#L245-L259
2,473
potatosalad/ruby-erlang-terms
lib/erlang/binary.rb
Erlang.Binary.part
def part(position, length) raise ArgumentError, 'position must be an Integer' if not position.is_a?(::Integer) raise ArgumentError, 'length must be a non-negative Integer' if not length.is_a?(::Integer) or length < 0 return Erlang::Binary[@data.byteslice(position, length)] end
ruby
def part(position, length) raise ArgumentError, 'position must be an Integer' if not position.is_a?(::Integer) raise ArgumentError, 'length must be a non-negative Integer' if not length.is_a?(::Integer) or length < 0 return Erlang::Binary[@data.byteslice(position, length)] end
[ "def", "part", "(", "position", ",", "length", ")", "raise", "ArgumentError", ",", "'position must be an Integer'", "if", "not", "position", ".", "is_a?", "(", "::", "Integer", ")", "raise", "ArgumentError", ",", "'length must be a non-negative Integer'", "if", "not", "length", ".", "is_a?", "(", "::", "Integer", ")", "or", "length", "<", "0", "return", "Erlang", "::", "Binary", "[", "@data", ".", "byteslice", "(", "position", ",", "length", ")", "]", "end" ]
Returns the section of this `Binary` starting at `position` of `length`. @param position [Integer] The starting position @param length [Integer] The non-negative length @return [Binary] @raise [ArgumentError] if `position` is not an `Integer` or `length` is not a non-negative `Integer`
[ "Returns", "the", "section", "of", "this", "Binary", "starting", "at", "position", "of", "length", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/binary.rb#L336-L340
2,474
langalex/culerity
lib/culerity/remote_browser_proxy.rb
Culerity.RemoteBrowserProxy.wait_until
def wait_until time_to_wait=30, &block time_limit = Time.now + time_to_wait until block.call if Time.now > time_limit raise "wait_until timeout after #{time_to_wait} seconds" end sleep 0.1 end true end
ruby
def wait_until time_to_wait=30, &block time_limit = Time.now + time_to_wait until block.call if Time.now > time_limit raise "wait_until timeout after #{time_to_wait} seconds" end sleep 0.1 end true end
[ "def", "wait_until", "time_to_wait", "=", "30", ",", "&", "block", "time_limit", "=", "Time", ".", "now", "+", "time_to_wait", "until", "block", ".", "call", "if", "Time", ".", "now", ">", "time_limit", "raise", "\"wait_until timeout after #{time_to_wait} seconds\"", "end", "sleep", "0.1", "end", "true", "end" ]
Calls the block until it returns true or +time_to_wait+ is reached. +time_to_wait+ is 30 seconds by default Returns true upon success Raises RuntimeError when +time_to_wait+ is reached.
[ "Calls", "the", "block", "until", "it", "returns", "true", "or", "+", "time_to_wait", "+", "is", "reached", ".", "+", "time_to_wait", "+", "is", "30", "seconds", "by", "default" ]
8e330ce79e88d00a213ece7755a22f4e0ca4f042
https://github.com/langalex/culerity/blob/8e330ce79e88d00a213ece7755a22f4e0ca4f042/lib/culerity/remote_browser_proxy.rb#L18-L27
2,475
langalex/culerity
lib/culerity/remote_browser_proxy.rb
Culerity.RemoteBrowserProxy.confirm
def confirm(bool, &block) blk = "lambda { #{bool} }" self.send_remote(:add_listener, :confirm) { blk } block.call self.send_remote(:remove_listener, :confirm, lambda {blk}) end
ruby
def confirm(bool, &block) blk = "lambda { #{bool} }" self.send_remote(:add_listener, :confirm) { blk } block.call self.send_remote(:remove_listener, :confirm, lambda {blk}) end
[ "def", "confirm", "(", "bool", ",", "&", "block", ")", "blk", "=", "\"lambda { #{bool} }\"", "self", ".", "send_remote", "(", ":add_listener", ",", ":confirm", ")", "{", "blk", "}", "block", ".", "call", "self", ".", "send_remote", "(", ":remove_listener", ",", ":confirm", ",", "lambda", "{", "blk", "}", ")", "end" ]
Specify whether to accept or reject all confirm js dialogs for the code in the block that's run.
[ "Specify", "whether", "to", "accept", "or", "reject", "all", "confirm", "js", "dialogs", "for", "the", "code", "in", "the", "block", "that", "s", "run", "." ]
8e330ce79e88d00a213ece7755a22f4e0ca4f042
https://github.com/langalex/culerity/blob/8e330ce79e88d00a213ece7755a22f4e0ca4f042/lib/culerity/remote_browser_proxy.rb#L52-L58
2,476
uasi/pixiv
lib/pixiv/page.rb
Pixiv.Page.bind
def bind(client) if self.class.const_defined?(:WithClient) mod = self.class.const_get(:WithClient) else mod = Page::WithClient end unless singleton_class.include?(mod) extend(mod) end self.client = client self end
ruby
def bind(client) if self.class.const_defined?(:WithClient) mod = self.class.const_get(:WithClient) else mod = Page::WithClient end unless singleton_class.include?(mod) extend(mod) end self.client = client self end
[ "def", "bind", "(", "client", ")", "if", "self", ".", "class", ".", "const_defined?", "(", ":WithClient", ")", "mod", "=", "self", ".", "class", ".", "const_get", "(", ":WithClient", ")", "else", "mod", "=", "Page", "::", "WithClient", "end", "unless", "singleton_class", ".", "include?", "(", "mod", ")", "extend", "(", "mod", ")", "end", "self", ".", "client", "=", "client", "self", "end" ]
Bind +self+ to +client+ @api private @return self
[ "Bind", "+", "self", "+", "to", "+", "client", "+" ]
6750829692fa9c3f56605d3319616072756c9d7c
https://github.com/uasi/pixiv/blob/6750829692fa9c3f56605d3319616072756c9d7c/lib/pixiv/page.rb#L58-L69
2,477
uasi/pixiv
lib/pixiv/client.rb
Pixiv.Client.login
def login(pixiv_id, password) doc = agent.get("https://accounts.pixiv.net/login?lang=ja&source=pc&view_type=page") return if doc && doc.body =~ /logout/ form = doc.forms_with(action: '/login').first puts doc.body and raise Error::LoginFailed, 'login form is not available' unless form form.pixiv_id = pixiv_id form.password = password doc = agent.submit(form) raise Error::LoginFailed unless doc && doc.body =~ /logout/ @member_id = member_id_from_mypage(doc) end
ruby
def login(pixiv_id, password) doc = agent.get("https://accounts.pixiv.net/login?lang=ja&source=pc&view_type=page") return if doc && doc.body =~ /logout/ form = doc.forms_with(action: '/login').first puts doc.body and raise Error::LoginFailed, 'login form is not available' unless form form.pixiv_id = pixiv_id form.password = password doc = agent.submit(form) raise Error::LoginFailed unless doc && doc.body =~ /logout/ @member_id = member_id_from_mypage(doc) end
[ "def", "login", "(", "pixiv_id", ",", "password", ")", "doc", "=", "agent", ".", "get", "(", "\"https://accounts.pixiv.net/login?lang=ja&source=pc&view_type=page\"", ")", "return", "if", "doc", "&&", "doc", ".", "body", "=~", "/", "/", "form", "=", "doc", ".", "forms_with", "(", "action", ":", "'/login'", ")", ".", "first", "puts", "doc", ".", "body", "and", "raise", "Error", "::", "LoginFailed", ",", "'login form is not available'", "unless", "form", "form", ".", "pixiv_id", "=", "pixiv_id", "form", ".", "password", "=", "password", "doc", "=", "agent", ".", "submit", "(", "form", ")", "raise", "Error", "::", "LoginFailed", "unless", "doc", "&&", "doc", ".", "body", "=~", "/", "/", "@member_id", "=", "member_id_from_mypage", "(", "doc", ")", "end" ]
A new instance of Client, logged in with the given credentials @overload initialize(pixiv_id, password) @param [String] pixiv_id @param [String] password @yield [agent] (optional) gives a chance to customize the +agent+ before logging in @overload initialize(agent) @param [Mechanize::HTTP::Agent] agent @return [Pixiv::Client] Log in to Pixiv @param [String] pixiv_id @param [String] password
[ "A", "new", "instance", "of", "Client", "logged", "in", "with", "the", "given", "credentials" ]
6750829692fa9c3f56605d3319616072756c9d7c
https://github.com/uasi/pixiv/blob/6750829692fa9c3f56605d3319616072756c9d7c/lib/pixiv/client.rb#L43-L53
2,478
uasi/pixiv
lib/pixiv/client.rb
Pixiv.Client.download_illust
def download_illust(illust, io_or_filename, size = :original) size = {:s => :small, :m => :medium, :o => :original}[size] || size url = illust.__send__("#{size}_image_url") referer = case size when :small then nil when :medium then illust.url when :original then illust.url else raise ArgumentError, "unknown size `#{size}`" end save_to = io_or_filename if save_to.is_a?(Array) save_to = filename_from_pattern(save_to, illust, url) end FileUtils.mkdir_p(File.dirname(save_to)) unless save_to.respond_to?(:write) @agent.download(url, save_to, [], referer) end
ruby
def download_illust(illust, io_or_filename, size = :original) size = {:s => :small, :m => :medium, :o => :original}[size] || size url = illust.__send__("#{size}_image_url") referer = case size when :small then nil when :medium then illust.url when :original then illust.url else raise ArgumentError, "unknown size `#{size}`" end save_to = io_or_filename if save_to.is_a?(Array) save_to = filename_from_pattern(save_to, illust, url) end FileUtils.mkdir_p(File.dirname(save_to)) unless save_to.respond_to?(:write) @agent.download(url, save_to, [], referer) end
[ "def", "download_illust", "(", "illust", ",", "io_or_filename", ",", "size", "=", ":original", ")", "size", "=", "{", ":s", "=>", ":small", ",", ":m", "=>", ":medium", ",", ":o", "=>", ":original", "}", "[", "size", "]", "||", "size", "url", "=", "illust", ".", "__send__", "(", "\"#{size}_image_url\"", ")", "referer", "=", "case", "size", "when", ":small", "then", "nil", "when", ":medium", "then", "illust", ".", "url", "when", ":original", "then", "illust", ".", "url", "else", "raise", "ArgumentError", ",", "\"unknown size `#{size}`\"", "end", "save_to", "=", "io_or_filename", "if", "save_to", ".", "is_a?", "(", "Array", ")", "save_to", "=", "filename_from_pattern", "(", "save_to", ",", "illust", ",", "url", ")", "end", "FileUtils", ".", "mkdir_p", "(", "File", ".", "dirname", "(", "save_to", ")", ")", "unless", "save_to", ".", "respond_to?", "(", ":write", ")", "@agent", ".", "download", "(", "url", ",", "save_to", ",", "[", "]", ",", "referer", ")", "end" ]
Downloads the image to +io_or_filename+ @param [Pixiv::Illust] illust @param [#write, String, Array<String, Symbol, #call>] io_or_filename io or filename or pattern for {#filename_from_pattern} @param [Symbol] size image size (+:small+, +:medium+, or +:original+)
[ "Downloads", "the", "image", "to", "+", "io_or_filename", "+" ]
6750829692fa9c3f56605d3319616072756c9d7c
https://github.com/uasi/pixiv/blob/6750829692fa9c3f56605d3319616072756c9d7c/lib/pixiv/client.rb#L141-L156
2,479
uasi/pixiv
lib/pixiv/client.rb
Pixiv.Client.download_manga
def download_manga(illust, pattern, &block) action = DownloadActionRegistry.new(&block) illust.original_image_urls.each_with_index do |url, n| begin action.before_each.call(url, n) if action.before_each filename = filename_from_pattern(pattern, illust, url) FileUtils.mkdir_p(File.dirname(filename)) @agent.download(url, filename, [], illust.original_image_referer) action.after_each.call(url, n) if action.after_each rescue action.on_error ? action.on_error.call($!) : raise end end end
ruby
def download_manga(illust, pattern, &block) action = DownloadActionRegistry.new(&block) illust.original_image_urls.each_with_index do |url, n| begin action.before_each.call(url, n) if action.before_each filename = filename_from_pattern(pattern, illust, url) FileUtils.mkdir_p(File.dirname(filename)) @agent.download(url, filename, [], illust.original_image_referer) action.after_each.call(url, n) if action.after_each rescue action.on_error ? action.on_error.call($!) : raise end end end
[ "def", "download_manga", "(", "illust", ",", "pattern", ",", "&", "block", ")", "action", "=", "DownloadActionRegistry", ".", "new", "(", "block", ")", "illust", ".", "original_image_urls", ".", "each_with_index", "do", "|", "url", ",", "n", "|", "begin", "action", ".", "before_each", ".", "call", "(", "url", ",", "n", ")", "if", "action", ".", "before_each", "filename", "=", "filename_from_pattern", "(", "pattern", ",", "illust", ",", "url", ")", "FileUtils", ".", "mkdir_p", "(", "File", ".", "dirname", "(", "filename", ")", ")", "@agent", ".", "download", "(", "url", ",", "filename", ",", "[", "]", ",", "illust", ".", "original_image_referer", ")", "action", ".", "after_each", ".", "call", "(", "url", ",", "n", ")", "if", "action", ".", "after_each", "rescue", "action", ".", "on_error", "?", "action", ".", "on_error", ".", "call", "(", "$!", ")", ":", "raise", "end", "end", "end" ]
Downloads the images to +pattern+ @param [Pixiv::Illust] illust the manga to download @param [Array<String, Symbol, #call>] pattern pattern for {#filename_from_pattern} @note +illust#manga?+ must be +true+ @todo Document +&block+
[ "Downloads", "the", "images", "to", "+", "pattern", "+" ]
6750829692fa9c3f56605d3319616072756c9d7c
https://github.com/uasi/pixiv/blob/6750829692fa9c3f56605d3319616072756c9d7c/lib/pixiv/client.rb#L163-L176
2,480
uasi/pixiv
lib/pixiv/client.rb
Pixiv.Client.filename_from_pattern
def filename_from_pattern(pattern, illust, url) pattern.map {|i| if i == :image_name name = File.basename(url) if name =~ /\.(\w+)\?\d+$/ name += '.' + $1 end name elsif i.is_a?(Symbol) then illust.send(i) elsif i.respond_to?(:call) then i.call(illust) else i end }.join('') end
ruby
def filename_from_pattern(pattern, illust, url) pattern.map {|i| if i == :image_name name = File.basename(url) if name =~ /\.(\w+)\?\d+$/ name += '.' + $1 end name elsif i.is_a?(Symbol) then illust.send(i) elsif i.respond_to?(:call) then i.call(illust) else i end }.join('') end
[ "def", "filename_from_pattern", "(", "pattern", ",", "illust", ",", "url", ")", "pattern", ".", "map", "{", "|", "i", "|", "if", "i", "==", ":image_name", "name", "=", "File", ".", "basename", "(", "url", ")", "if", "name", "=~", "/", "\\.", "\\w", "\\?", "\\d", "/", "name", "+=", "'.'", "+", "$1", "end", "name", "elsif", "i", ".", "is_a?", "(", "Symbol", ")", "then", "illust", ".", "send", "(", "i", ")", "elsif", "i", ".", "respond_to?", "(", ":call", ")", "then", "i", ".", "call", "(", "illust", ")", "else", "i", "end", "}", ".", "join", "(", "''", ")", "end" ]
Generate filename from +pattern+ in context of +illust+ and +url+ @api private @param [Array<String, Symbol, #call>] pattern @param [Pixiv::Illust] illust @param [String] url @return [String] filename The +pattern+ is an array of string, symbol, or object that responds to +#call+. Each component of the +pattern+ is replaced by the following rules and then the +pattern+ is concatenated as the returning +filename+. * +:image_name+ in the +pattern+ is replaced with the base name of the +url+ * Any other symbol is replaced with the value of +illust.send(the_symbol)+ * +#call+-able object is replaced with the value of +the_object.call(illust)+ * String is left as-is
[ "Generate", "filename", "from", "+", "pattern", "+", "in", "context", "of", "+", "illust", "+", "and", "+", "url", "+" ]
6750829692fa9c3f56605d3319616072756c9d7c
https://github.com/uasi/pixiv/blob/6750829692fa9c3f56605d3319616072756c9d7c/lib/pixiv/client.rb#L194-L207
2,481
lantins/dns-zone
lib/dns/zone.rb
DNS.Zone.soa
def soa # return the first SOA we find in the records array. rr = @records.find { |rr| rr.type == "SOA" } return rr if rr # otherwise create a new SOA rr = DNS::Zone::RR::SOA.new rr.serial = Time.now.utc.strftime("%Y%m%d01") rr.refresh_ttl = '3h' rr.retry_ttl = '15m' rr.expiry_ttl = '4w' rr.minimum_ttl = '30m' # store and return new SOA @records << rr return rr end
ruby
def soa # return the first SOA we find in the records array. rr = @records.find { |rr| rr.type == "SOA" } return rr if rr # otherwise create a new SOA rr = DNS::Zone::RR::SOA.new rr.serial = Time.now.utc.strftime("%Y%m%d01") rr.refresh_ttl = '3h' rr.retry_ttl = '15m' rr.expiry_ttl = '4w' rr.minimum_ttl = '30m' # store and return new SOA @records << rr return rr end
[ "def", "soa", "# return the first SOA we find in the records array.", "rr", "=", "@records", ".", "find", "{", "|", "rr", "|", "rr", ".", "type", "==", "\"SOA\"", "}", "return", "rr", "if", "rr", "# otherwise create a new SOA", "rr", "=", "DNS", "::", "Zone", "::", "RR", "::", "SOA", ".", "new", "rr", ".", "serial", "=", "Time", ".", "now", ".", "utc", ".", "strftime", "(", "\"%Y%m%d01\"", ")", "rr", ".", "refresh_ttl", "=", "'3h'", "rr", ".", "retry_ttl", "=", "'15m'", "rr", ".", "expiry_ttl", "=", "'4w'", "rr", ".", "minimum_ttl", "=", "'30m'", "# store and return new SOA", "@records", "<<", "rr", "return", "rr", "end" ]
Create an empty instance of a DNS zone that you can drive programmatically. @api public Helper method to access the zones SOA RR. @api public
[ "Create", "an", "empty", "instance", "of", "a", "DNS", "zone", "that", "you", "can", "drive", "programmatically", "." ]
f60cbfd7ec72217288be6c6ece9a83c22350cb58
https://github.com/lantins/dns-zone/blob/f60cbfd7ec72217288be6c6ece9a83c22350cb58/lib/dns/zone.rb#L36-L50
2,482
lantins/dns-zone
lib/dns/zone.rb
DNS.Zone.dump_pretty
def dump_pretty content = [] last_type = "SOA" sorted_records.each do |rr| content << '' if last_type != rr.type content << rr.dump last_type = rr.type end content.join("\n") << "\n" end
ruby
def dump_pretty content = [] last_type = "SOA" sorted_records.each do |rr| content << '' if last_type != rr.type content << rr.dump last_type = rr.type end content.join("\n") << "\n" end
[ "def", "dump_pretty", "content", "=", "[", "]", "last_type", "=", "\"SOA\"", "sorted_records", ".", "each", "do", "|", "rr", "|", "content", "<<", "''", "if", "last_type", "!=", "rr", ".", "type", "content", "<<", "rr", ".", "dump", "last_type", "=", "rr", ".", "type", "end", "content", ".", "join", "(", "\"\\n\"", ")", "<<", "\"\\n\"", "end" ]
Generates pretty output of the zone and its records. @api public
[ "Generates", "pretty", "output", "of", "the", "zone", "and", "its", "records", "." ]
f60cbfd7ec72217288be6c6ece9a83c22350cb58
https://github.com/lantins/dns-zone/blob/f60cbfd7ec72217288be6c6ece9a83c22350cb58/lib/dns/zone.rb#L68-L79
2,483
lantins/dns-zone
lib/dns/zone.rb
DNS.Zone.sorted_records
def sorted_records # pull out RRs we want to stick near the top top_rrs = {} top = %w{SOA NS MX SPF TXT} top.each { |t| top_rrs[t] = @records.select { |rr| rr.type == t } } remaining = @records.reject { |rr| top.include?(rr.type) } # sort remaining RRs by type, alphabeticly remaining.sort! { |a,b| a.type <=> b.type } top_rrs.values.flatten + remaining end
ruby
def sorted_records # pull out RRs we want to stick near the top top_rrs = {} top = %w{SOA NS MX SPF TXT} top.each { |t| top_rrs[t] = @records.select { |rr| rr.type == t } } remaining = @records.reject { |rr| top.include?(rr.type) } # sort remaining RRs by type, alphabeticly remaining.sort! { |a,b| a.type <=> b.type } top_rrs.values.flatten + remaining end
[ "def", "sorted_records", "# pull out RRs we want to stick near the top", "top_rrs", "=", "{", "}", "top", "=", "%w{", "SOA", "NS", "MX", "SPF", "TXT", "}", "top", ".", "each", "{", "|", "t", "|", "top_rrs", "[", "t", "]", "=", "@records", ".", "select", "{", "|", "rr", "|", "rr", ".", "type", "==", "t", "}", "}", "remaining", "=", "@records", ".", "reject", "{", "|", "rr", "|", "top", ".", "include?", "(", "rr", ".", "type", ")", "}", "# sort remaining RRs by type, alphabeticly", "remaining", ".", "sort!", "{", "|", "a", ",", "b", "|", "a", ".", "type", "<=>", "b", ".", "type", "}", "top_rrs", ".", "values", ".", "flatten", "+", "remaining", "end" ]
Records sorted with more important types being at the top. @api private
[ "Records", "sorted", "with", "more", "important", "types", "being", "at", "the", "top", "." ]
f60cbfd7ec72217288be6c6ece9a83c22350cb58
https://github.com/lantins/dns-zone/blob/f60cbfd7ec72217288be6c6ece9a83c22350cb58/lib/dns/zone.rb#L191-L203
2,484
potatosalad/ruby-erlang-terms
lib/erlang/associable.rb
Erlang.Associable.update_in
def update_in(*key_path, &block) if key_path.empty? raise ArgumentError, "must have at least one key in path" end key = key_path[0] if key_path.size == 1 new_value = block.call(fetch(key, nil)) else value = fetch(key, EmptyMap) new_value = value.update_in(*key_path[1..-1], &block) end return put(key, new_value) end
ruby
def update_in(*key_path, &block) if key_path.empty? raise ArgumentError, "must have at least one key in path" end key = key_path[0] if key_path.size == 1 new_value = block.call(fetch(key, nil)) else value = fetch(key, EmptyMap) new_value = value.update_in(*key_path[1..-1], &block) end return put(key, new_value) end
[ "def", "update_in", "(", "*", "key_path", ",", "&", "block", ")", "if", "key_path", ".", "empty?", "raise", "ArgumentError", ",", "\"must have at least one key in path\"", "end", "key", "=", "key_path", "[", "0", "]", "if", "key_path", ".", "size", "==", "1", "new_value", "=", "block", ".", "call", "(", "fetch", "(", "key", ",", "nil", ")", ")", "else", "value", "=", "fetch", "(", "key", ",", "EmptyMap", ")", "new_value", "=", "value", ".", "update_in", "(", "key_path", "[", "1", "..", "-", "1", "]", ",", "block", ")", "end", "return", "put", "(", "key", ",", "new_value", ")", "end" ]
Return a new container with a deeply nested value modified to the result of the given code block. When traversing the nested containers non-existing keys are created with empty `Hash` values. The code block receives the existing value of the deeply nested key/index (or `nil` if it doesn't exist). This is useful for "transforming" the value associated with a certain key/index. Naturally, the original container and sub-containers are left unmodified; new data structure copies are created along the path as needed. @example t = Erlang::Tuple[123, 456, 789, Erlang::Map["a" => Erlang::Tuple[5, 6, 7]]] t.update_in(3, "a", 1) { |value| value + 9 } # => Erlang::Tuple[123, 456, 789, Erlang::Map["a'" => Erlang::Tuple[5, 15, 7]]] map = Erlang::Map["a" => Erlang::Map["b" => Erlang::Map["c" => 42]]] map.update_in("a", "b", "c") { |value| value + 5 } # => Erlang::Map["a" => Erlang::Map["b" => Erlang::Map["c" => 47]]] @param key_path [Object(s)] List of keys/indexes which form the path to the key to be modified @yield [value] The previously stored value @yieldreturn [Object] The new value to store @return [Associable]
[ "Return", "a", "new", "container", "with", "a", "deeply", "nested", "value", "modified", "to", "the", "result", "of", "the", "given", "code", "block", ".", "When", "traversing", "the", "nested", "containers", "non", "-", "existing", "keys", "are", "created", "with", "empty", "Hash", "values", "." ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/associable.rb#L63-L75
2,485
potatosalad/ruby-erlang-terms
lib/erlang/associable.rb
Erlang.Associable.dig
def dig(key, *rest) value = get(key) if rest.empty? || value.nil? return value elsif value.respond_to?(:dig) return value.dig(*rest) end end
ruby
def dig(key, *rest) value = get(key) if rest.empty? || value.nil? return value elsif value.respond_to?(:dig) return value.dig(*rest) end end
[ "def", "dig", "(", "key", ",", "*", "rest", ")", "value", "=", "get", "(", "key", ")", "if", "rest", ".", "empty?", "||", "value", ".", "nil?", "return", "value", "elsif", "value", ".", "respond_to?", "(", ":dig", ")", "return", "value", ".", "dig", "(", "rest", ")", "end", "end" ]
Return the value of successively indexing into a collection. If any of the keys is not present in the collection, return `nil`. keys that the Erlang type doesn't understand, raises an argument error @example m = Erlang::Map[:a => 9, :b => Erlang::Tuple['a', 'b'], :e => nil] m.dig(:b, 0) # => "a" m.dig(:b, 5) # => nil m.dig(:b, 0, 0) # => nil m.dig(:b, :a) # ArgumentError @param key to fetch from the collection @return [Object]
[ "Return", "the", "value", "of", "successively", "indexing", "into", "a", "collection", ".", "If", "any", "of", "the", "keys", "is", "not", "present", "in", "the", "collection", "return", "nil", ".", "keys", "that", "the", "Erlang", "type", "doesn", "t", "understand", "raises", "an", "argument", "error" ]
a3eaa3d976610466a5f5da177109a1248dac020d
https://github.com/potatosalad/ruby-erlang-terms/blob/a3eaa3d976610466a5f5da177109a1248dac020d/lib/erlang/associable.rb#L89-L96
2,486
ustasb/pandata
lib/pandata/scraper.rb
Pandata.Scraper.download_all_data
def download_all_data(url) next_data_indices = {} while next_data_indices html = Downloader.read_page(url) # Sometimes Pandora returns the same next_data_indices as the previous page. # If we don't check for this, an infinite loop occurs. # This problem occurs with tconrad. prev_next_data_indices = next_data_indices next_data_indices = @parser.get_next_data_indices(html) next_data_indices = false if prev_next_data_indices == next_data_indices url = yield(html, next_data_indices) end end
ruby
def download_all_data(url) next_data_indices = {} while next_data_indices html = Downloader.read_page(url) # Sometimes Pandora returns the same next_data_indices as the previous page. # If we don't check for this, an infinite loop occurs. # This problem occurs with tconrad. prev_next_data_indices = next_data_indices next_data_indices = @parser.get_next_data_indices(html) next_data_indices = false if prev_next_data_indices == next_data_indices url = yield(html, next_data_indices) end end
[ "def", "download_all_data", "(", "url", ")", "next_data_indices", "=", "{", "}", "while", "next_data_indices", "html", "=", "Downloader", ".", "read_page", "(", "url", ")", "# Sometimes Pandora returns the same next_data_indices as the previous page.", "# If we don't check for this, an infinite loop occurs.", "# This problem occurs with tconrad.", "prev_next_data_indices", "=", "next_data_indices", "next_data_indices", "=", "@parser", ".", "get_next_data_indices", "(", "html", ")", "next_data_indices", "=", "false", "if", "prev_next_data_indices", "==", "next_data_indices", "url", "=", "yield", "(", "html", ",", "next_data_indices", ")", "end", "end" ]
Downloads all data given a starting URL. Some Pandora feeds only return 5 - 10 items per page but contain a link to the next set of data. Threads cannot be used because page A be must visited to know how to obtain page B. @param url [String]
[ "Downloads", "all", "data", "given", "a", "starting", "URL", ".", "Some", "Pandora", "feeds", "only", "return", "5", "-", "10", "items", "per", "page", "but", "contain", "a", "link", "to", "the", "next", "set", "of", "data", ".", "Threads", "cannot", "be", "used", "because", "page", "A", "be", "must", "visited", "to", "know", "how", "to", "obtain", "page", "B", "." ]
c75f83813171bc6149eca53ff310dedca1a7a1cb
https://github.com/ustasb/pandata/blob/c75f83813171bc6149eca53ff310dedca1a7a1cb/lib/pandata/scraper.rb#L119-L134
2,487
ustasb/pandata
lib/pandata/scraper.rb
Pandata.Scraper.get_url
def get_url(data_name, next_data_indices = {}) if next_data_indices.empty? next_data_indices = { nextStartIndex: 0, nextLikeStartIndex: 0, nextThumbStartIndex: 0 } else next_data_indices = next_data_indices.dup end next_data_indices[:webname] = @webname next_data_indices[:pat] = Downloader.get_pat DATA_FEED_URLS[data_name] % next_data_indices end
ruby
def get_url(data_name, next_data_indices = {}) if next_data_indices.empty? next_data_indices = { nextStartIndex: 0, nextLikeStartIndex: 0, nextThumbStartIndex: 0 } else next_data_indices = next_data_indices.dup end next_data_indices[:webname] = @webname next_data_indices[:pat] = Downloader.get_pat DATA_FEED_URLS[data_name] % next_data_indices end
[ "def", "get_url", "(", "data_name", ",", "next_data_indices", "=", "{", "}", ")", "if", "next_data_indices", ".", "empty?", "next_data_indices", "=", "{", "nextStartIndex", ":", "0", ",", "nextLikeStartIndex", ":", "0", ",", "nextThumbStartIndex", ":", "0", "}", "else", "next_data_indices", "=", "next_data_indices", ".", "dup", "end", "next_data_indices", "[", ":webname", "]", "=", "@webname", "next_data_indices", "[", ":pat", "]", "=", "Downloader", ".", "get_pat", "DATA_FEED_URLS", "[", "data_name", "]", "%", "next_data_indices", "end" ]
Grabs a URL from DATA_FEED_URLS and formats it appropriately. @param data_name [Symbol] @param next_data_indices [Symbol] query parameters to get the next set of data
[ "Grabs", "a", "URL", "from", "DATA_FEED_URLS", "and", "formats", "it", "appropriately", "." ]
c75f83813171bc6149eca53ff310dedca1a7a1cb
https://github.com/ustasb/pandata/blob/c75f83813171bc6149eca53ff310dedca1a7a1cb/lib/pandata/scraper.rb#L139-L150
2,488
dennisreimann/masq
app/models/masq/account.rb
Masq.Account.yubikey_authenticated?
def yubikey_authenticated?(otp) if yubico_identity? && Account.verify_yubico_otp(otp) (Account.extract_yubico_identity_from_otp(otp) == yubico_identity) else false end end
ruby
def yubikey_authenticated?(otp) if yubico_identity? && Account.verify_yubico_otp(otp) (Account.extract_yubico_identity_from_otp(otp) == yubico_identity) else false end end
[ "def", "yubikey_authenticated?", "(", "otp", ")", "if", "yubico_identity?", "&&", "Account", ".", "verify_yubico_otp", "(", "otp", ")", "(", "Account", ".", "extract_yubico_identity_from_otp", "(", "otp", ")", "==", "yubico_identity", ")", "else", "false", "end", "end" ]
Is the Yubico OTP valid and belongs to this account?
[ "Is", "the", "Yubico", "OTP", "valid", "and", "belongs", "to", "this", "account?" ]
bc6b6d84fe06811b9de19e7863c53c6bfad201fe
https://github.com/dennisreimann/masq/blob/bc6b6d84fe06811b9de19e7863c53c6bfad201fe/app/models/masq/account.rb#L127-L133
2,489
qoobaa/vcard
lib/vcard/dirinfo.rb
Vcard.DirectoryInfo.[]
def [](name) enum_by_name(name).each { |f| return f.value if f.value != ""} enum_by_name(name).each { |f| return f.value } nil end
ruby
def [](name) enum_by_name(name).each { |f| return f.value if f.value != ""} enum_by_name(name).each { |f| return f.value } nil end
[ "def", "[]", "(", "name", ")", "enum_by_name", "(", "name", ")", ".", "each", "{", "|", "f", "|", "return", "f", ".", "value", "if", "f", ".", "value", "!=", "\"\"", "}", "enum_by_name", "(", "name", ")", ".", "each", "{", "|", "f", "|", "return", "f", ".", "value", "}", "nil", "end" ]
The value of the first field named +name+, or nil if no match is found.
[ "The", "value", "of", "the", "first", "field", "named", "+", "name", "+", "or", "nil", "if", "no", "match", "is", "found", "." ]
0cab080676df262555e3adadc9a82fedc128d337
https://github.com/qoobaa/vcard/blob/0cab080676df262555e3adadc9a82fedc128d337/lib/vcard/dirinfo.rb#L113-L117
2,490
qoobaa/vcard
lib/vcard/dirinfo.rb
Vcard.DirectoryInfo.push_unique
def push_unique(field) push(field) unless @fields.detect { |f| f.name? field.name } self end
ruby
def push_unique(field) push(field) unless @fields.detect { |f| f.name? field.name } self end
[ "def", "push_unique", "(", "field", ")", "push", "(", "field", ")", "unless", "@fields", ".", "detect", "{", "|", "f", "|", "f", ".", "name?", "field", ".", "name", "}", "self", "end" ]
Push +field+ onto the fields, unless there is already a field with this name.
[ "Push", "+", "field", "+", "onto", "the", "fields", "unless", "there", "is", "already", "a", "field", "with", "this", "name", "." ]
0cab080676df262555e3adadc9a82fedc128d337
https://github.com/qoobaa/vcard/blob/0cab080676df262555e3adadc9a82fedc128d337/lib/vcard/dirinfo.rb#L212-L215
2,491
qoobaa/vcard
lib/vcard/dirinfo.rb
Vcard.DirectoryInfo.delete
def delete(field) case when field.name?("BEGIN"), field.name?("END") raise ArgumentError, "Cannot delete BEGIN or END fields." else @fields.delete field end self end
ruby
def delete(field) case when field.name?("BEGIN"), field.name?("END") raise ArgumentError, "Cannot delete BEGIN or END fields." else @fields.delete field end self end
[ "def", "delete", "(", "field", ")", "case", "when", "field", ".", "name?", "(", "\"BEGIN\"", ")", ",", "field", ".", "name?", "(", "\"END\"", ")", "raise", "ArgumentError", ",", "\"Cannot delete BEGIN or END fields.\"", "else", "@fields", ".", "delete", "field", "end", "self", "end" ]
Delete +field+. Warning: You can't delete BEGIN: or END: fields, but other profile-specific fields can be deleted, including mandatory ones. For vCards in particular, in order to avoid destroying them, I suggest creating a new Vcard, and copying over all the fields that you still want, rather than using #delete. This is easy with Vcard::Maker#copy, see the Vcard::Maker examples.
[ "Delete", "+", "field", "+", "." ]
0cab080676df262555e3adadc9a82fedc128d337
https://github.com/qoobaa/vcard/blob/0cab080676df262555e3adadc9a82fedc128d337/lib/vcard/dirinfo.rb#L233-L242
2,492
tomharris/random_data
lib/random_data/numbers.rb
RandomData.Numbers.number
def number(n) n.is_a?(Range) ? n.to_a.rand : rand(n) end
ruby
def number(n) n.is_a?(Range) ? n.to_a.rand : rand(n) end
[ "def", "number", "(", "n", ")", "n", ".", "is_a?", "(", "Range", ")", "?", "n", ".", "to_a", ".", "rand", ":", "rand", "(", "n", ")", "end" ]
n can be an Integer or a Range. If it is an Integer, it just returns a random number greater than or equal to 0 and less than n. If it is a Range, it returns a random number within the range Examples >> Random.number(5) => 4 >> Random.number(5) => 2 >> Random.number(5) => 1
[ "n", "can", "be", "an", "Integer", "or", "a", "Range", ".", "If", "it", "is", "an", "Integer", "it", "just", "returns", "a", "random", "number", "greater", "than", "or", "equal", "to", "0", "and", "less", "than", "n", ".", "If", "it", "is", "a", "Range", "it", "returns", "a", "random", "number", "within", "the", "range", "Examples" ]
641271ea66e7837b2c4a9efa034d9ac75b4f487d
https://github.com/tomharris/random_data/blob/641271ea66e7837b2c4a9efa034d9ac75b4f487d/lib/random_data/numbers.rb#L14-L16
2,493
rmosolgo/css_modules
lib/css_modules/rewrite.rb
CSSModules.Rewrite.rewrite_css
def rewrite_css(css_module_code) # Parse incoming CSS into an AST css_root = Sass::SCSS::CssParser.new(css_module_code, "(CSSModules)", 1).parse Sass::Tree::Visitors::SetOptions.visit(css_root, {}) ModuleVisitor.visit(css_root) css_root.render end
ruby
def rewrite_css(css_module_code) # Parse incoming CSS into an AST css_root = Sass::SCSS::CssParser.new(css_module_code, "(CSSModules)", 1).parse Sass::Tree::Visitors::SetOptions.visit(css_root, {}) ModuleVisitor.visit(css_root) css_root.render end
[ "def", "rewrite_css", "(", "css_module_code", ")", "# Parse incoming CSS into an AST", "css_root", "=", "Sass", "::", "SCSS", "::", "CssParser", ".", "new", "(", "css_module_code", ",", "\"(CSSModules)\"", ",", "1", ")", ".", "parse", "Sass", "::", "Tree", "::", "Visitors", "::", "SetOptions", ".", "visit", "(", "css_root", ",", "{", "}", ")", "ModuleVisitor", ".", "visit", "(", "css_root", ")", "css_root", ".", "render", "end" ]
Take css module code as input, and rewrite it as browser-friendly CSS code. Apply opaque transformations so that selectors can only be accessed programatically, not by class name literals.
[ "Take", "css", "module", "code", "as", "input", "and", "rewrite", "it", "as", "browser", "-", "friendly", "CSS", "code", ".", "Apply", "opaque", "transformations", "so", "that", "selectors", "can", "only", "be", "accessed", "programatically", "not", "by", "class", "name", "literals", "." ]
c1a80f6b7b76193c7dda616877a75eec6bbe600d
https://github.com/rmosolgo/css_modules/blob/c1a80f6b7b76193c7dda616877a75eec6bbe600d/lib/css_modules/rewrite.rb#L15-L23
2,494
tomharris/random_data
lib/random_data/grammar.rb
RandomData.Grammar.grammatical_construct
def grammatical_construct(grammar, what=nil) output = "" if what.nil? case grammar when Hash a_key = grammar.keys.sort_by{rand}[0] output += grammatical_construct(grammar, a_key) when Array grammar.each do |item| output += grammatical_construct(item) end when String output += grammar end else rhs = grammar[what] case rhs when Array rhs.each do |item| case item when Symbol output += grammatical_construct(grammar,item) when String output += item when Hash output += grammatical_construct(item) else raise "#{item.inspect} must be a symbol or string or Hash" end end when Hash output+= grammatical_construct(rhs) when Symbol output += grammatical_construct(rhs) when String output += rhs else raise "#{rhs.inspect} must be a symbol, string, Array or Hash" end end return output end
ruby
def grammatical_construct(grammar, what=nil) output = "" if what.nil? case grammar when Hash a_key = grammar.keys.sort_by{rand}[0] output += grammatical_construct(grammar, a_key) when Array grammar.each do |item| output += grammatical_construct(item) end when String output += grammar end else rhs = grammar[what] case rhs when Array rhs.each do |item| case item when Symbol output += grammatical_construct(grammar,item) when String output += item when Hash output += grammatical_construct(item) else raise "#{item.inspect} must be a symbol or string or Hash" end end when Hash output+= grammatical_construct(rhs) when Symbol output += grammatical_construct(rhs) when String output += rhs else raise "#{rhs.inspect} must be a symbol, string, Array or Hash" end end return output end
[ "def", "grammatical_construct", "(", "grammar", ",", "what", "=", "nil", ")", "output", "=", "\"\"", "if", "what", ".", "nil?", "case", "grammar", "when", "Hash", "a_key", "=", "grammar", ".", "keys", ".", "sort_by", "{", "rand", "}", "[", "0", "]", "output", "+=", "grammatical_construct", "(", "grammar", ",", "a_key", ")", "when", "Array", "grammar", ".", "each", "do", "|", "item", "|", "output", "+=", "grammatical_construct", "(", "item", ")", "end", "when", "String", "output", "+=", "grammar", "end", "else", "rhs", "=", "grammar", "[", "what", "]", "case", "rhs", "when", "Array", "rhs", ".", "each", "do", "|", "item", "|", "case", "item", "when", "Symbol", "output", "+=", "grammatical_construct", "(", "grammar", ",", "item", ")", "when", "String", "output", "+=", "item", "when", "Hash", "output", "+=", "grammatical_construct", "(", "item", ")", "else", "raise", "\"#{item.inspect} must be a symbol or string or Hash\"", "end", "end", "when", "Hash", "output", "+=", "grammatical_construct", "(", "rhs", ")", "when", "Symbol", "output", "+=", "grammatical_construct", "(", "rhs", ")", "when", "String", "output", "+=", "rhs", "else", "raise", "\"#{rhs.inspect} must be a symbol, string, Array or Hash\"", "end", "end", "return", "output", "end" ]
Returns simple sentences based on a supplied grammar, which must be a hash, the keys of which are symbols. The values are either an array of successive values or a grammar (i.e, hash with symbols as keys, and hashes or arrays as values. The arrays contain symbols referencing the keys in the present grammar, or strings to be output. The keys are always symbols. Example: Random.grammatical_construct({:story => [:man, " bites ", :dog], :man => { :bob => "Bob"}, :dog => {:a =>"Rex", :b =>"Rover"}}, :story) => "Bob bites Rover"
[ "Returns", "simple", "sentences", "based", "on", "a", "supplied", "grammar", "which", "must", "be", "a", "hash", "the", "keys", "of", "which", "are", "symbols", ".", "The", "values", "are", "either", "an", "array", "of", "successive", "values", "or", "a", "grammar", "(", "i", ".", "e", "hash", "with", "symbols", "as", "keys", "and", "hashes", "or", "arrays", "as", "values", ".", "The", "arrays", "contain", "symbols", "referencing", "the", "keys", "in", "the", "present", "grammar", "or", "strings", "to", "be", "output", ".", "The", "keys", "are", "always", "symbols", "." ]
641271ea66e7837b2c4a9efa034d9ac75b4f487d
https://github.com/tomharris/random_data/blob/641271ea66e7837b2c4a9efa034d9ac75b4f487d/lib/random_data/grammar.rb#L17-L58
2,495
thhermansen/google_static_maps_helper
lib/google_static_maps_helper/location.rb
GoogleStaticMapsHelper.Location.endpoints_for_circle_with_radius
def endpoints_for_circle_with_radius(radius, steps = 30) raise ArgumentError, "Number of points has to be in range of 1..360!" unless (1..360).include? steps points = [] steps.times do |i| points << endpoint(radius, i * 360 / steps) end points << points.first points end
ruby
def endpoints_for_circle_with_radius(radius, steps = 30) raise ArgumentError, "Number of points has to be in range of 1..360!" unless (1..360).include? steps points = [] steps.times do |i| points << endpoint(radius, i * 360 / steps) end points << points.first points end
[ "def", "endpoints_for_circle_with_radius", "(", "radius", ",", "steps", "=", "30", ")", "raise", "ArgumentError", ",", "\"Number of points has to be in range of 1..360!\"", "unless", "(", "1", "..", "360", ")", ".", "include?", "steps", "points", "=", "[", "]", "steps", ".", "times", "do", "|", "i", "|", "points", "<<", "endpoint", "(", "radius", ",", "i", "*", "360", "/", "steps", ")", "end", "points", "<<", "points", ".", "first", "points", "end" ]
Returns ends poionts which will make up a circle around current location and have given radius
[ "Returns", "ends", "poionts", "which", "will", "make", "up", "a", "circle", "around", "current", "location", "and", "have", "given", "radius" ]
31d2af983e17be736566bfac686b56c57385d64d
https://github.com/thhermansen/google_static_maps_helper/blob/31d2af983e17be736566bfac686b56c57385d64d/lib/google_static_maps_helper/location.rb#L81-L91
2,496
tomharris/random_data
lib/random_data/names.rb
RandomData.Names.companyname
def companyname num = rand(5) if num == 0 num = 1 end final = num.times.collect { @@lastnames.rand.capitalize } if final.count == 1 "#{final.first} #{@@company_types.rand}, #{@@incorporation_types.rand}" else incorporation_type = rand(17) % 2 == 0 ? @@incorporation_types.rand : nil company_type = rand(17) % 2 == 0 ? @@company_types.rand : nil trailer = company_type.nil? ? "" : " #{company_type}" trailer << ", #{incorporation_type}" unless incorporation_type.nil? "#{final[0..-1].join(', ')} & #{final.last}#{trailer}" end end
ruby
def companyname num = rand(5) if num == 0 num = 1 end final = num.times.collect { @@lastnames.rand.capitalize } if final.count == 1 "#{final.first} #{@@company_types.rand}, #{@@incorporation_types.rand}" else incorporation_type = rand(17) % 2 == 0 ? @@incorporation_types.rand : nil company_type = rand(17) % 2 == 0 ? @@company_types.rand : nil trailer = company_type.nil? ? "" : " #{company_type}" trailer << ", #{incorporation_type}" unless incorporation_type.nil? "#{final[0..-1].join(', ')} & #{final.last}#{trailer}" end end
[ "def", "companyname", "num", "=", "rand", "(", "5", ")", "if", "num", "==", "0", "num", "=", "1", "end", "final", "=", "num", ".", "times", ".", "collect", "{", "@@lastnames", ".", "rand", ".", "capitalize", "}", "if", "final", ".", "count", "==", "1", "\"#{final.first} #{@@company_types.rand}, #{@@incorporation_types.rand}\"", "else", "incorporation_type", "=", "rand", "(", "17", ")", "%", "2", "==", "0", "?", "@@incorporation_types", ".", "rand", ":", "nil", "company_type", "=", "rand", "(", "17", ")", "%", "2", "==", "0", "?", "@@company_types", ".", "rand", ":", "nil", "trailer", "=", "company_type", ".", "nil?", "?", "\"\"", ":", "\" #{company_type}\"", "trailer", "<<", "\", #{incorporation_type}\"", "unless", "incorporation_type", ".", "nil?", "\"#{final[0..-1].join(', ')} & #{final.last}#{trailer}\"", "end", "end" ]
Returns a random company name >> Random.company_name "Harris & Thomas"
[ "Returns", "a", "random", "company", "name" ]
641271ea66e7837b2c4a9efa034d9ac75b4f487d
https://github.com/tomharris/random_data/blob/641271ea66e7837b2c4a9efa034d9ac75b4f487d/lib/random_data/names.rb#L26-L42
2,497
jhass/open_graph_reader
lib/open_graph_reader/builder.rb
OpenGraphReader.Builder.base
def base base = Base.new type = @parser.graph.fetch("og:type", "website").downcase validate_type type @parser.graph.each do |property| build_property base, property end synthesize_required_properties base drop_empty_children base validate base base end
ruby
def base base = Base.new type = @parser.graph.fetch("og:type", "website").downcase validate_type type @parser.graph.each do |property| build_property base, property end synthesize_required_properties base drop_empty_children base validate base base end
[ "def", "base", "base", "=", "Base", ".", "new", "type", "=", "@parser", ".", "graph", ".", "fetch", "(", "\"og:type\"", ",", "\"website\"", ")", ".", "downcase", "validate_type", "type", "@parser", ".", "graph", ".", "each", "do", "|", "property", "|", "build_property", "base", ",", "property", "end", "synthesize_required_properties", "base", "drop_empty_children", "base", "validate", "base", "base", "end" ]
Create a new builder. @param [Parser] parser @see Parser#graph @see Parser#additional_namespaces Build and return the base. @return [Base]
[ "Create", "a", "new", "builder", "." ]
5488354b7dd75b5411a881d734aa7176546e7cb4
https://github.com/jhass/open_graph_reader/blob/5488354b7dd75b5411a881d734aa7176546e7cb4/lib/open_graph_reader/builder.rb#L24-L40
2,498
dennisreimann/masq
app/helpers/masq/application_helper.rb
Masq.ApplicationHelper.nav
def nav(name, url, pages = nil, active = false) content_tag :li, link_to(name, url), :class => (active || (pages && active_page?(pages)) ? 'act' : nil) end
ruby
def nav(name, url, pages = nil, active = false) content_tag :li, link_to(name, url), :class => (active || (pages && active_page?(pages)) ? 'act' : nil) end
[ "def", "nav", "(", "name", ",", "url", ",", "pages", "=", "nil", ",", "active", "=", "false", ")", "content_tag", ":li", ",", "link_to", "(", "name", ",", "url", ")", ",", ":class", "=>", "(", "active", "||", "(", "pages", "&&", "active_page?", "(", "pages", ")", ")", "?", "'act'", ":", "nil", ")", "end" ]
Renders a navigation element and marks it as active where appropriate. See active_page? for details
[ "Renders", "a", "navigation", "element", "and", "marks", "it", "as", "active", "where", "appropriate", ".", "See", "active_page?", "for", "details" ]
bc6b6d84fe06811b9de19e7863c53c6bfad201fe
https://github.com/dennisreimann/masq/blob/bc6b6d84fe06811b9de19e7863c53c6bfad201fe/app/helpers/masq/application_helper.rb#L45-L47
2,499
dennisreimann/masq
app/helpers/masq/application_helper.rb
Masq.ApplicationHelper.active_page?
def active_page?(pages = {}) is_active = pages.include?(params[:controller]) is_active = pages[params[:controller]].include?(params[:action]) if is_active && !pages[params[:controller]].empty? is_active end
ruby
def active_page?(pages = {}) is_active = pages.include?(params[:controller]) is_active = pages[params[:controller]].include?(params[:action]) if is_active && !pages[params[:controller]].empty? is_active end
[ "def", "active_page?", "(", "pages", "=", "{", "}", ")", "is_active", "=", "pages", ".", "include?", "(", "params", "[", ":controller", "]", ")", "is_active", "=", "pages", "[", "params", "[", ":controller", "]", "]", ".", "include?", "(", "params", "[", ":action", "]", ")", "if", "is_active", "&&", "!", "pages", "[", "params", "[", ":controller", "]", "]", ".", "empty?", "is_active", "end" ]
Takes a hash with pages and tells whether the current page is among them. The keys must be controller names and their value must be an array of action names. If the array is empty, every action is supposed to be valid.
[ "Takes", "a", "hash", "with", "pages", "and", "tells", "whether", "the", "current", "page", "is", "among", "them", ".", "The", "keys", "must", "be", "controller", "names", "and", "their", "value", "must", "be", "an", "array", "of", "action", "names", ".", "If", "the", "array", "is", "empty", "every", "action", "is", "supposed", "to", "be", "valid", "." ]
bc6b6d84fe06811b9de19e7863c53c6bfad201fe
https://github.com/dennisreimann/masq/blob/bc6b6d84fe06811b9de19e7863c53c6bfad201fe/app/helpers/masq/application_helper.rb#L52-L56