Skip to content

Instantly share code, notes, and snippets.

View iNecas's full-sized avatar
💭
Happy hacking 👨 ⌨️ 💻

Ivan Necas iNecas

💭
Happy hacking 👨 ⌨️ 💻
View GitHub Profile
@iNecas
iNecas / read_documentation.rb
Created December 7, 2010 16:12
What happens, when you don't read documentation
# 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 {}
@iNecas
iNecas / versions ordering
Created December 27, 2010 15:39
Ordering major.minor.revision version numbers
# 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"]
@iNecas
iNecas / hash_constructor_differences
Created February 1, 2011 10:29
Difference between Hash.new(arg) and Hash.new(&block)
hash = Hash.new("") # => {}
hash[1] << "Hello" # => "Hello"
hash[2] # => "Hello"
hash = Hash.new{|h,k| h[k] = ""} # => {}
hash[1] << "Hello" # => "Hello"
hash[2] # => ""
@iNecas
iNecas / grouping_block_arguments_example
Created February 1, 2011 11:14
Grouping block arguments example
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
@iNecas
iNecas / convention-foreign-keys.php
Created February 10, 2011 22:37
Convention foreign keys: plugin for Adminer (http://www.adminer.org)
<?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 {
@iNecas
iNecas / split_with_regexp.rb
Created February 13, 2011 20:54
Advanced string splitting with regexp
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(/(<.*?>)/)
@iNecas
iNecas / find_duplicities.rb
Created February 21, 2011 12:44
finding duplicities in array in one line
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"]
@iNecas
iNecas / files_tree_steps.rb
Created March 18, 2011 18:08
Cucumber step definition checking content of directory
# Usage:
#
# Then directory "tmp/sample" should have the following tree:
# """
# monday
# | beer.rb
# tuesday
# | vodka.php
# | wine.rb
# wednesday
@iNecas
iNecas / rescue_exception.rb
Created April 29, 2011 14:12
Why it is not a good idea to rescue without explicit exception type?
# 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)
def get_distribution env, id
self.repos(env).map do |repo|
repo.distributions.find_all {|d| d.id == id }
end
end