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
# The =~ operator returns the index of the first letter of the first matching occurence | |
/(rubycademy)/ =~ "https://www.rubycademy.com" # => 12 | |
"https://www.rubycademy.com" =~ /(rubycademy)/ # => 12 | |
# Note that the operator returns nil if no matching found | |
/lol/ =~ "It's not funny" # => nil |
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
# Flat the array layer by layer | |
# | |
# Insights: | |
# | |
# - Using recursion. | |
# | |
class Array | |
def my_flatten(depth = nil) | |
if depth.nil? || depth.is_a?(Fixnum) |
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
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] |
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
for i in 1..3 do | |
puts "Iteration #{i}" | |
redo if i == 1 | |
puts 'After redo' | |
end | |
# produces: | |
# |
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 hello | |
yield | |
end | |
hello do | |
puts 'hello' | |
redo | |
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
[1,2,3].each do |n| | |
puts "Step #{n}" | |
n += 1 and redo if n < 2 | |
end | |
# produces: | |
# | |
# Step 1 | |
# Step 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
for i in 1..3 do | |
puts "Iteration #{i}" | |
i += 1 and redo if i == 1 | |
end | |
# produces | |
# | |
# Iteration 1 | |
# Iteration 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
class Hash | |
def to_s | |
'hash' | |
end | |
end | |
h = {} | |
p h.to_s # => "hash" |
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
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, ...] |
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
# lib/core_ext/hash.rb | |
class Hash | |
# monkey-patch to temporarily | |
# disabling the MyLib#print_config output | |
def to_json | |
'' | |
end | |
end |