I’ve been digging into the Ruby docs a lot recently and I’m reminded of lots of small things I love about the language. Setting myself a little goal for march — each day this month I’m going to try to tweet a small fun Ruby feature or tip in this thread 🧵
1/ Starting us off, String#casecmp? does a case insensitve comparison of Strings
2/ Second up, Array#assoc takes an argument, and finds the first element of the Array which is itself an Array whose first element is the argument. That sounds a little confusing, I'll let the code clear it up:
3/ Day three: We sometimes need both the quotient and remainder of a division operation. Instead of calculating them both separately, Numeric#divmod returns an array of two elements representing the quotient and remainder
4/ Fourth up: String#squeeze removes consecutive letter repetition from a string
5/ Day five: Array#one? tells us whether precisely one element of an array matches an argument
6/ Sixth day (Happy birthday, dad!):

String#delete_prefix and String#delete_suffix are both faster than String#sub or String#gsub. Useful for parsing input, and perhaps even metaprogramming
7/ Week one! There are three different ways to start Heredocs: "<<", "<<-" and "<<~". They each allow different indentations. I tried out a funky way of explaining this within the heredocs themselves
8/ Day eight: Integer#digits gives us an array of the digits in a number. It reduces string allocations since we don't need to convert ints to a string and back, so has practical applications in cases like checksum formulas. It also can be useful in interview-type settings.
9/ Ninth day: Almost forgot about this with all the @wnb_rb excitement today!

Array#* behaves differently based on arg type - join for Strings and mult for ints.

I knew about ints, but the String behavior is new to me. Unsure I find it more readable than Array#join... thoughts?
10/ One third! FileUtils#uptodate? tells us if a file is newer than all files in a list of files. Seems to me that it'd be most useful for checking if files have been compiled. I can also see use cases for checking if data has been updated in data pipelines
11/ Was just noticing 3/11 is a fun date because 11 is 3 in binary. I guess only 1/1 and 2/10 share this property.

So, I looked into the DateTime class and learned === on DateTimes returns true if two dates fall on the same day, regardless of their time.
12/ Day 12: Integer#gcdlcm gives us the greatest common divisor and least common multiple of two ints. I don't know if there are practical use cases for this outside of something like building a calculator / math application. Any other ideas?
13/ Lucky thirteen: I was looking at global vars and learned $~ returns the same data as Regexp.last_match. Both give us a MatchData object from the last successful pattern match, locally scoped. There were some other global vars I didn't know about for later in this thread!
14/ Happy Pi day! Array#cycle lets us loop over an array continuously. It takes optional integer and block parameters which gives it quite a few different use cases that we can look at below! There is also an Enumerator#cycle method, similarly useful
15/ Day 15: String#strip strips whitespace from the beginning and end of a string, but there are also String#lstrip and String#rstrip if we only need to strip whitespace from one side. Curious to hear use cases for these two (specifically when String#strip wasn't a better option)
16/ Over halfway!! Integer#upto and Integer#downto will return Enumerators when called without blocks. With blocks, they'll iterate over all ints from start to end calling the block. Similar in some ways to both ranges and Integer#times
17/ Integer#pow with two args does modular exponentiation. It's the same as (integer ** first_arg) % second_arg, but with Integer#pow it doesn't store large temporary values, making it much faster. Could be have practical cryptographic applications!
18/ Day 18: I love Ruby's method method. So today, I looked at the Method class, and found Method#owner which tells us the class or module that defines a method. Favorite use cases?
19/ Happy Friday! File.split splits a string (meant to be a file path) into a two element array representing the directory and the base name. Useful if we want those things from the file path, maybe for moving files around, or opening files, or anything else
20/ Day 20: Array#rotate rotates the contents of an array. It can take an optional arg for how many times to rotate, otherwise will default to one. I'd imagine uses are somewhat similar to Array#cycle, but can't think of many where we'd want rotate instead of shuffle. Thoughts?
21/ 21st day: I looked at the IRB docs today and learned that IRB stores more eval history than just the most recent result (`_`) if we set IRB.conf[:EVAL_HISTORY]!! We can then access that eval history with two underscores `__`
22/ Day 22: Range#exclude_end? tells us whether a range is exclusive. I can't think of a time I'd use this, any ideas?
23/ Day 23: Probably making it obvious I rarely do frontend work by not knowing this one, but String#center lets us nicely center strings. It takes an arg which represents the length of the final centered string, and an optional second arg for what to pad with (default is space).
24/ 24th day: MatchData#pre_match and MatchData#post_match give us the portions of a string which are before or after a match. Some sample uses below!
25/ Day 25, last week of March! Numeric#fdiv does float division which gives us another way to divide two integers and get a float result.
26/ Day 26: Enumerable#grep_v does the inverse of Enumerable#grep. It returns an array of every element in the enumerable which does _not_ === the pattern passed as an arg. Useful when it's more readable to do the inverse rather than the grep!
27/ Day 27: Hash#compare_by_identity sets a hash to only consider identity when comparing keys so that two keys are only equivalent if they are the exact same object. ActiveRecord uses this in a few places when loading and associating records https://github.com/rails/rails/search?q=compare_by_identity
28/ Day 28: the $LOADED_FEATURES global variable gives us an array containing module names loaded by require. Possibly useful when debugging, in some test suites, or if you're writing a gem and need for some reason to check if other gems have been loaded.
29/ 3 days left! Hash#invert flips keys and values in a hash. There are quite a few times I would've used this had I known about it, where I need to look something up by value instead of by key in a hash.
30/ Day 30: String#parition takes one argument splits the String at the arg into an array of size 3 representing the part of the String before the arg, the arg, and the part afterwards. Useful for parsing input!
31/ (1/2) Today is the last day of March. I was trying to think of a fun topic for a final tip and realized what’d be most interesting is to learn from anyone who has been following along on this thread!
31/ (2/2) So, if you’ve been following along, or even if you haven’t, I’d love love love if you comment with something I’ve missed that you think others might learn from! Or if you have time, even look into the Ruby docs and thread anything new to you and neat that you find!
You can follow @JemmaIssroff.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: