Follow

Seeing code like this makes my teeth ache:

if "#{self.key}".start_with?("XYZ")

*sigh*

@michaelfromuk some other ones I've seen:

"#{var.to_s}"

YAML.load(File.read(path))
# YAML.load_file exists for this

URL.parse(url.to_s)
# was only necessary before the URI() method was added

@michaelfromuk sometimes I try to understand the reasoning for that kind of code was. Sometimes the context gets lots with the churn, other it's excessive pragmatism.

In that particular case it looks like they try to avoid the infamous 'Undefined method 'starts_with?` for Nil class' and also making the sentence shorter (cool?); instead of guard it,

return false if key.nil? # blank?

or using a slower regex

if key ~= /AXYZ/

Not sure, archeology solves the mystery.

Sign in to participate in the conversation
Ruby.social

A Mastodon instance for Rubyists & friends