This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Given an anonymous function with a previously unspecified number of arguments like so: */ | |
$f = function ($x, $y, $z) { | |
/* Do something interesting with $x, $y and $z. */ | |
return; | |
}; | |
/* How can I convert it into the following nested function (each function yielding one argument), | |
* finally calling $f when there are enough arguments? | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* From https://github.com/guzzle/iterator/blob/master/MapIterator.php */ | |
class MapIterator extends \IteratorIterator | |
{ | |
/** @var mixed Callback */ | |
protected $callback; | |
/** | |
* @param \Traversable $iterator Traversable iterator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
before :deploy, "solr:stop" | |
after :deploy, "solr:start" | |
namespace :solr do | |
task :stop, :roles => :app do | |
run "#{sudo} monit stop solr" | |
end | |
task :start, :roles => :app do | |
run "#{sudo} monit start solr" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Nameable | |
# Assuming first_name and last_name are accessors. | |
def name=(name) | |
self.first_name, *_, self.last_name = name.split | |
end | |
def name | |
[first_name, last_name].compact.join(" ") | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The ASF licenses this file to You under the Apache License, Version 2.0 | |
# (the "License"); you may not use this file except in compliance with | |
# the License. You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hash | |
def to_proc | |
method(:[]).to_proc | |
end | |
end | |
%w(a b c d).map(&{"a" => 1, "b" => 2, "c" => 3}) | |
# => [1, 2, 3, nil] | |
# Or, a more readable example: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Averageable | |
def mean(&block) | |
sum(&block) / length.to_f | |
end | |
def count(elem) | |
length - (self - [elem]).length | |
end | |
def mode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
def count(elem) | |
length - (self - [elem]).length | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
class Array | |
def count_with_loop(elem) | |
enum = 0 | |
each { |e| enum += 1 if elem == e } | |
enum | |
end | |
def count_without_loop(elem) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MongoDB | |
class URI | |
attr_reader :uri | |
def initialize(uri) | |
@uri = uri | |
end | |
def username | |
matches[:username] |
OlderNewer