This file contains hidden or 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 IntegerExtension | |
def to_s | |
"this is an integer: #{super}" | |
end | |
end | |
Integer.prepend(IntegerExtension) | |
puts 1.to_s |
This file contains hidden or 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
client = {client: { firstname: "bobby", lastname: "brown"}} | |
# Using the the [] getter | |
firstname = client[:client][:firstname] | |
=> "bobby" | |
# Using the new dig method, it gives you the | |
# same result the best is coming | |
firstname = client.dig(:client, :firstname) | |
=> "bobby" |
This file contains hidden or 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
# This refinement will monkey patch the Integer#to_s method | |
# this change will be applied only in the current context | |
module IntegerRefinement | |
refine Integer do | |
def to_s | |
"this is an integer: #{super}" | |
end | |
end | |
end |
This file contains hidden or 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
# You can create an array of one string element with * | |
my_array = *"hello" | |
=> ["hello"] | |
arr = %w(a b c) | |
=> ["a", "b", "c"] | |
head, *tail = arr | |
=> ["a", "b", "c"] |
This file contains hidden or 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
name = "Stuart" | |
# To build a string and escape double quote at the same time | |
%q(Hello, my name is "#{name}") | |
=> "Hello, my name is \"\#{name}\"" | |
# The same exist for single quote | |
%Q(Hello, my name is "#{name}") | |
=> "Hello, my name is \"Stuart\"" |
This file contains hidden or 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
# http://ruby-doc.org/core-2.5.0/Enumerable.html#method-i-min | |
# Ruby 2.2 | |
[1, 2, 3, 4, 5].min(2) | |
=> [1, 2] | |
[3, 4, 5].max(3) | |
=> [5, 4, 3] |
This file contains hidden or 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
# http://ruby-doc.org/core-2.5.0/Float.html#method-i-round | |
# Ruby >= 2.4 | |
(2.5).round(half: :up) | |
=> 3 | |
(2.5).round(half: :down) | |
=> 2 |
This file contains hidden or 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
# http://ruby-doc.org/core-2.5.0/MatchData.html#method-i-named_captures | |
# Ruby >= 2.4 | |
/(?<first_name>.+) (?<last_name>.+)/.match('Bobby Brown').named_captures | |
=> {"first_name"=>"Bobby", "last_name"=>"Brown"} |
This file contains hidden or 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
# http://ruby-doc.org/core-2.5.0/MatchData.html#method-i-named_captures | |
# Ruby >= 2.4 | |
/(?<first_name>.+) (?<last_name>.+)/.match('Bobby Brown').named_captures |
NewerOlder