Tip 1: On Twitter, Dr Nic Williams shared a tip of how to make systems like ChatGPT return better code by being more specific in your system prompts. For example:
When giving Ruby code snippets, use modern Ruby 3.3 syntax, especially for hashes and method signatures with named parameters. I followed up with a half joke based on my own experiences where LLMs ignore other modern Ruby constructs and idioms, which leads us to... Tip 2: If you have an array, say, of items and you want to create a hash with the keys being the unique values and the values being the number of times those values appear in the array, you might do something like this: array = [1,2,2,3,4,4,4]
array.each_with_object(Hash.new(0)) { |i, h|
h[i] += 1
}
Or, perhaps: hash = Hash.new(0)
array.each { |i| hash[i] += 1 }
Since Ruby 2.7, however, you can use array.tally ! It's made my Advent of Code solutions far less verbose.. ;-) Tip 3: A simpler tip lies at the heart of tip two. Skim Ruby's docs from time to time! Whenever I check out the docs for Enumerable , I'm reminded of something I fail to remember in my day to day work. Have a browse and you might find your own useful shortcuts to share with your team. |