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
# getting first item of array or empty hash, when empty: | |
# A) Without documentation: | |
def get_first_item_from_array(array) | |
unless array.blank? | |
array.each do |single| | |
return single | |
end | |
end | |
return {} |
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
# wrong | |
["9.13.3", "9.3.2", "10.1.1", "9.3.5"].sort | |
# => ["10.1.1", "9.13.3", "9.3.2", "9.3.5"] | |
# right | |
["9.13.3", "9.3.2", "10.1.1", "9.3.5"].sort_by{|version| version.scan(/\d+/).map(&:to_i)} | |
# => ["9.3.2", "9.3.5", "9.13.3", "10.1.1"] |
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
hash = Hash.new("") # => {} | |
hash[1] << "Hello" # => "Hello" | |
hash[2] # => "Hello" | |
hash = Hash.new{|h,k| h[k] = ""} # => {} | |
hash[1] << "Hello" # => "Hello" | |
hash[2] # => "" |
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
pscs = { | |
["Brno", "Hradecká"] => [61200,62100], | |
["Brno", "Merhautova"] => [61300,61400], | |
["Brno", "Nezamyslova"] => [61500,63600], | |
} | |
pscs.each do |(city, street), street_pscs| | |
puts "#{street} in #{city} has following PSCs: #{street_pscs.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
<?php | |
/** Convention foreign keys: plugin for Adminer | |
* Links for foreign keys by convention user_id => users.id. Useful for Ruby On Rails like standard schema conventions. | |
* @author Ivan Nečas, @inecas | |
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 | |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other) | |
*/ | |
class ConventionForeignKeys { |
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
string = "<div>ruby</div><div>string</div><div>methods</div>" | |
# == splitting with regexp | |
string.split(/<.*?>/) | |
# => ["", "ruby", "", "string", "", "methods"] | |
# == and keeping separator in the result | |
string.split(/(<.*?>)/) |
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
comps = ["PC", "MAC", "PC", "PC", "LINUX", "MAC", "DIDAKTIK"] | |
duplicities = comps.group_by{|x| x.first}.find_all{|k,v| v.size > 1}.map{|x| x.first} | |
duplicities # => ["MAC","PC"] |
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
# Usage: | |
# | |
# Then directory "tmp/sample" should have the following tree: | |
# """ | |
# monday | |
# | beer.rb | |
# tuesday | |
# | vodka.php | |
# | wine.rb | |
# wednesday |
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
# Why it is not a good idea to rescue without explicit exception type? | |
# Because: | |
# | |
# begin | |
# "..." | |
# rescue | |
# end | |
# | |
# Handles only StandardError descendants (no Excpetion descendants catched) |
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
def get_distribution env, id | |
self.repos(env).map do |repo| | |
repo.distributions.find_all {|d| d.id == id } | |
end | |
end |
OlderNewer