Skip to content

Instantly share code, notes, and snippets.

Avatar

Mehdi FARSI mehdi-farsi

View GitHub Profile
@mehdi-farsi
mehdi-farsi / rails_project_loc.rb
Last active Apr 25, 2019
Count the lines of code of a rails project - in pure Ruby
View rails_project_loc.rb
# 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'
View redo_04.rb
def hello
yield
end
hello do
puts 'hello'
redo
end
View redo_03.rb
[1,2,3].each do |n|
puts "Step #{n}"
n += 1 and redo if n < 2
end
# produces:
#
# Step 1
# Step 2
View redo_02.rb
for i in 1..3 do
puts "Iteration #{i}"
i += 1 and redo if i == 1
end
# produces
#
# Iteration 1
# Iteration 2
View redo_01.rb
for i in 1..3 do
puts "Iteration #{i}"
redo if i == 1
puts 'After redo'
end
# produces:
#
View refine_04.rb
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, ...]
View refine_03.rb
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 Sep 8, 2018
dummy_rails_app
View hash.rb
# lib/core_ext/hash.rb
class Hash
# monkey-patch to temporarily
# disabling the MyLib#print_config output
def to_json
''
end
end
View refine_01.rb
class Hash
def to_s
'hash'
end
end
h = {}
p h.to_s # => "hash"
View sym_03.rb
Symbol.all_symbols.length # => 3893
Symbol.all_symbols.grep(/Struct/) # => [:Struct]
:dummy_symbol
Symbol.all_symbols.length # => 3894
Symbol.all_symbols.grep(/dummy_symbol/) # => [:dummy_symbol]
dummy_variable = nil
Symbol.all_symbols.length # => 3895
Symbol.all_symbols.grep(/dummy_variable/) # => [:dummy_variable]