Count number of weekdays between two dates
If you need to quickly find out the number of weekdays (Monday to Friday) between to given dates, try this:
require 'date'
a = Date.parse "11.04.2014"
b = Date.parse "31.12.2014"
(a..b).count {|date| (1..5).include?(date.wday) }
Related cards:
Deal with different ways of counting weeks and weekdays in Ruby
Depending on where you live, different rules are used to determine the number of the week and a weekday. You have no chance whatsoever to get this...
Grep the number of occurences in a file, counting multiple hits per line
Grep prints one line per match. To return the number if matches, use the -c
switch:
grep -c "something" filename
However, if a word appears more than once in a line, it is only counted once.
To count every match, you can use [sed
](http:...
Count number of existing objects in Ruby
Sometimes you want to know exactly how many objects exist within your running Ruby process. Here is how:
stats = {}
ObjectSpace.each_object {|o| stats[o.class].nil? ? stats[o.class] = 0 : stats[o.class] += 1 }; stats
=> {String=...
Ruby: Counting occurrences of an item in an array / enumerable
Enumerable#count
can do three things.
- With no argument provided, it returns the number of items.
- With an argument, it returns the number of items matching the given value.
- With a block, ...
Quick git contributors list
git shortlog -s -n [commit-range]
-n
, --numbered
Sort output according to the number of commits per author
-s
, --summary
Suppress commit descriptions, only provide commit count
[commit-range]
E.g. $tagname..
for "everything afte...
Retrieve the total number of records when paginating with will_paginate
When you use will_paginage to paginate a scope, and you want to obtain the total number of records matched before pagination, you can use total_entries
:
users = User.active.paginate
puts users....
Rails: Comparison of Dates - before? and after?
tl;dr
Since Rails
6+
you can usebefore?
andafter?
to check if a date/time is before or after another date/time.
Example
christmas = Date.parse('24.12.2022')
date_of_buying_a_gift = Date.parse('12.12.2022')
date_of_b...
Getting rid of space between inline-block elements
When two elements with display: inline-block
are sitting next to each other, whitespace between becomes a space character.
Solutions, in decreasing order of awesomeness:
- Don't have whitespace between two elements! See [Whitespace Removal in...
The challenging setup of an offshore project
After managing a few agile projects from India and learning the tricks of the trade, I have been wondering why life is much easier when you are working closely with the client as compared to working offshore. What worried me more was that I had to...
Count lines of code
The following counts all the lines in all *.rb
files in the app
directory. Run several of these commands to get a rough estimate of the LOC.
find app -name *.rb -exec wc {} \; | awk '{a+=$1;print a}' | tail -1