Last active
March 30, 2016 16:53
-
-
Save pricees/07906da2f951fe35d5f01663ef6e3387 to your computer and use it in GitHub Desktop.
Some fun core extension 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
# Read here: http://edgeguides.rubyonrails.org/active_support_core_extensions.html | |
# require "active_support/core_ext/object/blank.rb" # Load Blank module | |
# require "active_support/core_ext/object" # Load all Object core ext | |
# require "active_support/core_ext" # Load all core ext | |
# require "active_support/all" # Load all active support | |
require "active_support/dependencies/autoload" | |
require "active_support/core_ext" | |
################################################### | |
################################################### | |
class MyClass | |
def initialize(name, age) | |
@name = name | |
@age = age | |
end | |
end | |
my_class = MyClass.new("Ted", 36) | |
p my_class.instance_variable_names | |
# => ["@name", "@age"] | |
p my_class.instance_values | |
# => {"name"=>"Ted", "age"=>36} | |
# Responds_to vs Acts like? | |
MyString = Class.new do | |
def acts_like_string? # | |
end | |
end | |
puts MyString.new.acts_like?(:string) | |
# => true | |
################################################### | |
################################################### | |
Person = Struct.new(:name) do | |
def hello | |
puts "Hello, my name is #{name}" | |
end | |
end | |
jane = Person.new("Jane") | |
puts jane.name.presence || "Female" | |
# => Jane | |
puts Person.new.name.presence || "What's in a name?" | |
# => What's in a name? | |
puts({a: 1, b: 2}[:b].presence || "D'oh!") | |
# => 2 | |
puts({a: 1, b: 2}[:c].presence || "Define :c") | |
# => Define :c | |
"Person".constantize.new("James").hello | |
# => Hello, my name is James | |
################################################### | |
################################################### | |
# Nil | |
# try, try!, try with a block | |
person = nil | |
_ = "Hello, person's name is #{person.try :name}, age: #{person.try :age}" | |
person.try { "Hello, person's name is #{person.name}, age: #{person.age}" } | |
################################################### | |
################################################### | |
# String | |
# to_s :human, :human_size, :percentage, :delimited, :currency, :rounded | |
puts 5551234444.to_s(:phone, area_code: true) | |
# => (555) 123-4444 | |
puts "this is a title".titleize | |
# => This Is A Title | |
################################################### | |
################################################### | |
# Numeric | |
puts 1.ordinalize | |
# => 1st | |
5.times { puts rand(1E6).ordinalize } | |
# => 809793rd | |
# => 539498th | |
# => 256822nd | |
# => 232360th | |
# => 790239th | |
################################################### | |
################################################### | |
# Array | |
puts [:a].many? # false | |
puts [:a, :b].many? # true | |
# exclude? == !include? | |
puts [:a].exclude?(:b) | |
# => true | |
puts %w(Earth Wind Fire).to_sentence # => "Earth, Wind, and Fire" | |
p (1..10).to_a.to(2) | |
# => [1, 2, 3] | |
p (1..10).to_a.from(5) | |
# => [6, 7, 8, 9, 10] | |
p (1..10).to_a.third | |
# => 3 | |
p (1..10).to_a.fifth | |
# => 5 | |
ary = %w[a b c d] | |
ary.append(10) | |
# => ["a", "b", "c", "d", 10] | |
p ary | |
ary.prepend(1) | |
p ary | |
# => [1, "a", "b", "c", "d", 10] | |
################################################### | |
################################################### | |
# Hash | |
p({a: {b: 1}}).deep_merge(a: {c: 2}) | |
# => {:a=>{:b=>1, :c=>2}} | |
#deep_dup | |
#symbolize_keys, | |
#stringify_keys, | |
#deep_symbolize_keys, | |
#deep_stringify_keys, | |
p({a: 1, b: 2, c: 3}).except(:b) | |
# => {a: 1, c: 3} | |
# slice/! v extract! | |
hash = {a: 1, b: 2} | |
hash.slice(:a) # {a: 1} | |
rest = hash.slice!(:a) # {a: 1} | |
p rest | |
# => {:b=>2} | |
p hash | |
# => {:a=>1} | |
hash = {a: 1, b: 2} | |
rest = hash.extract!(:a) | |
p rest | |
# => {:a=>1} | |
p hash | |
# => {:b=>2} | |
################################################### | |
################################################### | |
# Ranges | |
puts (1..10).overlaps?(7..11) | |
# => true | |
puts (1..10).include?(7..11) | |
# => false | |
puts (1..10).include?(7..10) | |
# => true | |
puts (1..10).include?(7...11) | |
# => false | |
################################################### | |
################################################### | |
# Dates & Times | |
# beginning_of_ / end_of_ : year, month, week, day, hour, minute | |
# prev_ / next_: year, month, week, day, hour, minute | |
# year, month, week, day, hour, minute + "_since | |
now = Time.now | |
p now | |
# => 2016-03-30 11:37:20 -0500 | |
p now.all_day | |
# => 2016-03-30 00:00:00 -0500..2016-03-30 23:59:59 -0500 | |
p 2.months.ago | |
# => 2016-01-30 11:37:20 -0600 | |
p now.months_since(2) | |
# => 2016-05-30 11:37:20 -0500 | |
p now.prev_week(:thursday).strftime("%B %d %Y") | |
# => "March 24 2016" | |
p now.beginning_of_week(:friday).strftime("%B %d %Y") | |
# => "March 25 2016" | |
p now.end_of_week(:friday).strftime("%B %d %Y") | |
# => "March 31 2016" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment