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 autovivifying_hash | |
Hash.new {|ht,k| ht[k] = autovivifying_hash} | |
end | |
hash = autovivifying_hash | |
hash['a']['b']['c']['d']['e'] = 'hello' | |
puts hash['c'] | |
puts hash['a'] | |
puts hash['b'] |
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 uri?(string) | |
uri = URI.parse(string) | |
%w( http https ).include?(uri.scheme) | |
rescue URI::BadURIError | |
false | |
rescue URI::InvalidURIError | |
false | |
end | |
See https://stackoverflow.com/questions/5331014/check-if-given-string-is-an-url |
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
Some Model Name | |
... | |
# scopes | |
scope :by_category, ->(category) { where(category: category) if category.present? } | |
scope :by_project_id, ->(id) { where(project_id: id ) } | |
... | |
# class methods |
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 CoolFileGenerator < Rails::Generators::Base | |
def set_file_name | |
@file_name = "cool_file.rb" | |
end | |
def create_cool_file | |
body = ask "What should I write into the file?" | |
create_file @file_name, "Insert after me \n" + body | |
end |