Skip to content

Instantly share code, notes, and snippets.

View mehdi-farsi's full-sized avatar

Mehdi FARSI mehdi-farsi

View GitHub Profile
trap("SIGINT") { exit! }
total_width = `stty size`.scan(/\d+/)[1].to_i # terminal width
snowflakes = {}
puts "\033[2J"; # clearing output
loop do
snowflakes[rand(total_width)] = 0
en:
errors:
messages:
http_unauthorized: 'Authentication required'
http_not_found: 'Not found'
http_bad_request: 'Incorrect or incomplete input'
http_service_unavailable: 'Service unavailable'
http_forbidden: 'Forbidden'
http_unprocessable_entity: 'Unprocessable Entity'
resource_not_found: 'Resource not found'
@mehdi-farsi
mehdi-farsi / rails_project_loc.rb
Last active February 9, 2024 07:13
Count the lines of code of a rails project - in pure Ruby
# Workflow:
#
# 1- if the path points to a directory
# 1.1- if the directory isn't in the exclusion list then: count LOC
# 1.2- else: prune directory
# 2- else
# 1.1- if the file extension is whitelisted then: count LOC
# 1.2- else: next
require 'find'
def hello
yield
end
hello do
puts 'hello'
redo
end
[1,2,3].each do |n|
puts "Step #{n}"
n += 1 and redo if n < 2
end
# produces:
#
# Step 1
# Step 2
for i in 1..3 do
puts "Iteration #{i}"
i += 1 and redo if i == 1
end
# produces
#
# Iteration 1
# Iteration 2
for i in 1..3 do
puts "Iteration #{i}"
redo if i == 1
puts 'After redo'
end
# produces:
#
module Patch
@@res = refine(Array) {}
def self.res; @@res; end
end
p Patch.res # => #<refinement:Array@Patch>
p Patch.res.class # => Module
p Patch.res.ancestors # => [#<refinement:Array@Patch>, Array, ...]
module TemporaryPatch
refine Hash do
def to_s
''
end
end
end
my_ebook = {
ebook: 'Ruby Object Model',
@mehdi-farsi
mehdi-farsi / hash.rb
Last active September 8, 2018 21:53
dummy_rails_app
# lib/core_ext/hash.rb
class Hash
# monkey-patch to temporarily
# disabling the MyLib#print_config output
def to_json
''
end
end