@vinothiniBJ
Active Support Improvements
– Rails5
©2014
• https://goo.gl/ZTl8oO
• prev_day
• next_day
• on_weekend
• on_weekday
• next_weekday
• prev_weekday
• prev_week(same_time: true)
• next_week(same_time: true)
Improvements in Date, Time and DateTime
©2014
• days_in_year
class Time
include DateAndTime::Calculations
COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
class << self
# Returns the number of days in the given month.
# If no year is specified, it will use the current year.
def days_in_month(month, year = current.year)
if month == 2 && ::Date.gregorian_leap?(year)
29
else
COMMON_YEAR_DAYS_IN_MONTH[month]
end
end
# Returns the number of days in the given year.
# If no year is specified, it will use the current year.
def days_in_year(year = current.year)
days_in_month(2, year) + 337
end
end
end
Improvements in Date, Time and DateTime
©2014
• Pluck ( https://goo.gl/yjuBki )
module Enumerable
# Convert an enumerable to an array based on the given key.
#
# [{ name: "David" }, { name: "Rafael" }, { name: "Aaron" }].pluck(:name)
# => ["David", "Rafael", "Aaron"]
#
# [{ id: 1, name: "David" }, { id: 2, name: "Rafael" }].pluck(:id, :name)
# => [[1, "David"], [2, "Rafael"]]
def pluck(*keys)
if keys.many?
map { |element| keys.map { |key| element[key] } }
else
map { |element| element[keys.first] }
end
end
end
©2014
• Without ( https://goo.gl/etJN7h )
class Array
# Returns a copy of the Array without the specified elements.
#
# people = ["David", "Rafael", "Aaron", "Todd"]
# people.without "Aaron", "Todd"
# => ["David", "Rafael"]
#
# Note: This is an optimization of `Enumerable#without` that uses `Array#-`
# instead of `Array#reject` for performance reasons.
def without(*elements)
self - elements
end
end
©2014
• second_to_last (https://goo.gl/Ig0pOY )
• third_to_last
class Array
# Equal to <tt>self[-3]</tt>.
#
# %w( a b c d e ).third_to_last # => "c"
def third_to_last
self[-3]
end
# Equal to <tt>self[-2]</tt>.
#
# %w( a b c d e ).second_to_last # => "d"
def second_to_last
self[-2]
end
end
©2014
• positive? (https://goo.gl/UsTnWl )
• negative?
class Integer
# Returns true if the number is positive.
#
# 1.positive? # => true
# 0.positive? # => false
# -1.positive? # => false
def positive?
self > 0
end
# Returns true if the number is negative.
#
# -1.positive? # => true
# 0.positive? # => false
# 1.positive? # => false
def negative?
self < 0
end
end
©2014
• Inquiry ( https://goo.gl/47bjv7 , https://goo.gl/QpVmU3 )
class ArrayInquirer < Array
def any?(*candidates, &block)
if candidates.none?
super
else
candidates.any? do |candidate|
include?(candidate) || include?(candidate.to_sym)
end
end
end
private
def respond_to_missing?(name, include_private = false)
name[-1] == '?'
end
def method_missing(name, *args)
if name[-1] == '?'
any?(name[0..-2])
else
super
end
end
end
©2014
Q&A ?
Question and Answers
©2014
WE
BUILD
APPS
THAT
PEOPLE
LOVE TO
USE
Enterprise Web Applications
Development
Cross-Platform Business Mobile
Apps Development
Social Media Integrated
Applications Development
Product Development Services

More Related Content

PDF
Join optimization (tpch 11)
PDF
Assignment 4
PPT
PlazeItNow! - simple way for managing data on schemes
PPTX
Indices
PDF
201707 CSE110 Lecture 05
PPTX
Comparing and Ordering numbers
PDF
Go smallmanifesto6
DOCX
Diary template
Join optimization (tpch 11)
Assignment 4
PlazeItNow! - simple way for managing data on schemes
Indices
201707 CSE110 Lecture 05
Comparing and Ordering numbers
Go smallmanifesto6
Diary template

Viewers also liked (17)

DOCX
Diary template
PPT
Soal konsep geo
PDF
Campus Assessment Solution And Importance
PPTX
Inner Harmony through Mindfulness Meditation by Gustavo Estrada
PPTX
World Tour Keynote Presentation - London
PDF
Currrency Trading Tips & News
DOCX
During my meditation
PDF
M01.JNB.000159_EC 6 Schools Marketing Project Reference
PDF
PDF
Product Catalog
PPTX
#Anchor #comedian #Host #CharacterArtist
PPTX
Return Path World Tour Keynote - San Francisco
PPT
Kelas x perkenalan ilmu geografi
PPS
Construyendo la web semantica
PPTX
Cloud computing disadvantages
PPTX
Boosting Skilling Performance - Solutions for Training Institutes Skill India
Diary template
Soal konsep geo
Campus Assessment Solution And Importance
Inner Harmony through Mindfulness Meditation by Gustavo Estrada
World Tour Keynote Presentation - London
Currrency Trading Tips & News
During my meditation
M01.JNB.000159_EC 6 Schools Marketing Project Reference
Product Catalog
#Anchor #comedian #Host #CharacterArtist
Return Path World Tour Keynote - San Francisco
Kelas x perkenalan ilmu geografi
Construyendo la web semantica
Cloud computing disadvantages
Boosting Skilling Performance - Solutions for Training Institutes Skill India
Ad

Similar to Active Support Improvements in Rails 5 (10)

PDF
Simplifying Code: Monster to Elegant in 5 Steps
PDF
Saigon Ruby Meetup 06/10/2015 - 5 Random Ruby Tips
PDF
New features in Ruby 2.4
KEY
Advanced Testing on RubyEnRails '09
KEY
Grouping (MOTM 2010.02)
PDF
DDW Clinic Session 1.pdf
PDF
Simplifying code monster to elegant in n 5 steps
PDF
Slides chapter3part1 ruby-forjavaprogrammers
PPTX
Ruby's Arrays and Hashes with examples
PDF
Ruby iterators
Simplifying Code: Monster to Elegant in 5 Steps
Saigon Ruby Meetup 06/10/2015 - 5 Random Ruby Tips
New features in Ruby 2.4
Advanced Testing on RubyEnRails '09
Grouping (MOTM 2010.02)
DDW Clinic Session 1.pdf
Simplifying code monster to elegant in n 5 steps
Slides chapter3part1 ruby-forjavaprogrammers
Ruby's Arrays and Hashes with examples
Ruby iterators
Ad

Recently uploaded (20)

PDF
Getting Started with Data Integration: FME Form 101
PDF
WOOl fibre morphology and structure.pdf for textiles
PPTX
Chapter 5: Probability Theory and Statistics
PPTX
The various Industrial Revolutions .pptx
PPTX
Modernising the Digital Integration Hub
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
Hybrid model detection and classification of lung cancer
PPTX
observCloud-Native Containerability and monitoring.pptx
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
Five Habits of High-Impact Board Members
PPTX
Tartificialntelligence_presentation.pptx
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
STKI Israel Market Study 2025 version august
Getting Started with Data Integration: FME Form 101
WOOl fibre morphology and structure.pdf for textiles
Chapter 5: Probability Theory and Statistics
The various Industrial Revolutions .pptx
Modernising the Digital Integration Hub
NewMind AI Weekly Chronicles – August ’25 Week III
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Hybrid model detection and classification of lung cancer
observCloud-Native Containerability and monitoring.pptx
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
Five Habits of High-Impact Board Members
Tartificialntelligence_presentation.pptx
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
A review of recent deep learning applications in wood surface defect identifi...
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
A contest of sentiment analysis: k-nearest neighbor versus neural network
Assigned Numbers - 2025 - Bluetooth® Document
STKI Israel Market Study 2025 version august

Active Support Improvements in Rails 5

  • 2. ©2014 • https://goo.gl/ZTl8oO • prev_day • next_day • on_weekend • on_weekday • next_weekday • prev_weekday • prev_week(same_time: true) • next_week(same_time: true) Improvements in Date, Time and DateTime
  • 3. ©2014 • days_in_year class Time include DateAndTime::Calculations COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] class << self # Returns the number of days in the given month. # If no year is specified, it will use the current year. def days_in_month(month, year = current.year) if month == 2 && ::Date.gregorian_leap?(year) 29 else COMMON_YEAR_DAYS_IN_MONTH[month] end end # Returns the number of days in the given year. # If no year is specified, it will use the current year. def days_in_year(year = current.year) days_in_month(2, year) + 337 end end end Improvements in Date, Time and DateTime
  • 4. ©2014 • Pluck ( https://goo.gl/yjuBki ) module Enumerable # Convert an enumerable to an array based on the given key. # # [{ name: "David" }, { name: "Rafael" }, { name: "Aaron" }].pluck(:name) # => ["David", "Rafael", "Aaron"] # # [{ id: 1, name: "David" }, { id: 2, name: "Rafael" }].pluck(:id, :name) # => [[1, "David"], [2, "Rafael"]] def pluck(*keys) if keys.many? map { |element| keys.map { |key| element[key] } } else map { |element| element[keys.first] } end end end
  • 5. ©2014 • Without ( https://goo.gl/etJN7h ) class Array # Returns a copy of the Array without the specified elements. # # people = ["David", "Rafael", "Aaron", "Todd"] # people.without "Aaron", "Todd" # => ["David", "Rafael"] # # Note: This is an optimization of `Enumerable#without` that uses `Array#-` # instead of `Array#reject` for performance reasons. def without(*elements) self - elements end end
  • 6. ©2014 • second_to_last (https://goo.gl/Ig0pOY ) • third_to_last class Array # Equal to <tt>self[-3]</tt>. # # %w( a b c d e ).third_to_last # => "c" def third_to_last self[-3] end # Equal to <tt>self[-2]</tt>. # # %w( a b c d e ).second_to_last # => "d" def second_to_last self[-2] end end
  • 7. ©2014 • positive? (https://goo.gl/UsTnWl ) • negative? class Integer # Returns true if the number is positive. # # 1.positive? # => true # 0.positive? # => false # -1.positive? # => false def positive? self > 0 end # Returns true if the number is negative. # # -1.positive? # => true # 0.positive? # => false # 1.positive? # => false def negative? self < 0 end end
  • 8. ©2014 • Inquiry ( https://goo.gl/47bjv7 , https://goo.gl/QpVmU3 ) class ArrayInquirer < Array def any?(*candidates, &block) if candidates.none? super else candidates.any? do |candidate| include?(candidate) || include?(candidate.to_sym) end end end private def respond_to_missing?(name, include_private = false) name[-1] == '?' end def method_missing(name, *args) if name[-1] == '?' any?(name[0..-2]) else super end end end
  • 10. ©2014 WE BUILD APPS THAT PEOPLE LOVE TO USE Enterprise Web Applications Development Cross-Platform Business Mobile Apps Development Social Media Integrated Applications Development Product Development Services