#ActiveSupport's `.presence` lets us write cleaner code. It returns the object if it's present, nil otherwise. No more tedious nil checks! Easily assign a default value when the parameter is nil or empty! #RailsTips#RubyTips#CleanCode
Ever heard of String#inquiry in Rails? It turns strings into objects that respond to question methods, making code more readable. Instead of status == 'active', you can write . Super underrated! #RubyOnRails#RubyTips#CodeCleaner
#RubyTips
String class provides handy methods `start_with?` and `end_with?` which basically do what they say.
As argument they can take multiple strings and return true if any one of them matches with start or end of the string respectively.
#Ruby
Ruby Tip:
You can convert an array into a hash using the Hash#[] method.
Example:
Hash[Array('a'..'z').zip(Array('a'..'z').rotate)]
#ruby#rubyguides#rubytips
[Ruby Tip]
You can set the default value of a hash after creating it (either with {}, or to_h)
Like this:
h = {}
h.default = 0
You can also set the default proc like this:
h.default_proc = ->(h, k) { 123 }
#ruby#rubyguides#rubytips
Ruby Tip:
You can use Regexp.last_match instead of the ugly $~ variable to get access to the results of your last regex.
/c(.)t/ =~ 'cat'
Regexp.last_match(0)
# "cat"
Regexp.last_match(1)
# "a"
#Ruby#RubyGuides#RubyTips
Ruby Tip:
Since Ruby 2.4 you can use the String#match? method to find out if a regular expression matches a string.
This method returns true / false & it will not update $~ and related variables.
This translates into more performance (3x) :)
#ruby#rubytips#rubyguides