Today I Learned

A Hashrocket project

3 posts by joshuadavey

Ruby optional arguments can come before required

I’m used to seeing optional arguments after required ones, as so:

require 'date'

def age(date, today=Date.today)
  (today - date) / 365.25 # good enough
end

birthdate = Date.parse("2011-06-15")

# How old am I today?
age(birthdate) #=> 4.583

# How old was I on the first?
age(birthdate, Date.parse("2016-01-01")) #=> 4.547

The second argument is optional.

However, you can also make the first argument optional:

def age(today=Date.today, date)
  (today - date) / 365.25 # good enough
end

birthdate = Date.parse("2011-06-15")

# How old am I today?
age(birthdate) #=> 4.583

# How old was I on the first?
age(Date.parse("2016-01-01"), birthdate) #=> 4.547

I don’t recommend doing this. It’s confusing.

URI::regexp

You can generate a regular expression that matches URIs using URI from Ruby’s stdlib:

require 'uri'

matcher = URI.regexp(["http"])
md = matcher.match("I like http://google.com, for real")
#=> #<MatchData "http://google.com" 1:"http" 2:nil 3:nil 4:"google.com" 5:nil 6:nil 7:nil 8:nil 9:nil>
md[0]
#=> "http://google.com"

You can use this with String#scan to extract all URIs from a string:

urls= []
"I like http://google.com, for real. Also, http://localhost:3000 is useful.".scan(matcher) { urls << Regexp.last_match[0] }
urls
#=> ["http://google.com", "http://localhost:3000"]

See http://ruby-doc.org/stdlib-2.2.2/libdoc/uri/rdoc/URI.html#method-c-regexp for more information.

Protocol-relative URLs are now discouraged

For a long time, I’ve used protocol-relative URLs as a way to serve assets from a page using the same protocol (i.e. http or https) as the parent page. They look like this:

<img src="//google.com/path/to/image.jpg"/>

But now that SSL is encouraged everywhere on the web, one should always use https when an asset supports it.

<img src="https://google.com/path/to/image.jpg"/>

According to Paul Irish, It’s always safe to request HTTPS assets even if your site is on HTTP, however the reverse is not true.