Vocabulary: It’s Important

Question: What is the English word for “mental depression or apathy caused by comparison of the actual state of the world with an ideal state?”

Answer: There is none. But if you were learning German, you’d simply say Weltschmerz. Yep. One word. There happen to be a lot of these glorious one-word wonders in the German language.

As I learn to program, I realize how important it is to know your vocabulary. Each language has its own unique shortcuts that help save a ton of time & frustration, if you know them. Ruby is full of them!

I was working on a problem where I had to test every possible combination of a set of values for a certain condition, and I ended up manually writing my own method that created every permutation of the values. It took a lot of time & thinking; some messing up & frustration. Little did I know that there’s an actual method for Arrays already written in the standard Ruby library — literally called permutation — that does this. Whaaaat? If I had only looked a little harder…

permutation { |p| block } → ary
permutation → an_enumerator
permutation(n) { |p| block } → ary
permutation(n) → an_enumerator
When invoked with a block, yield all permutations of length n of the elements of ary, then return the array itself. If n is not specified, yield all permutations of all elements. The implementation makes no guarantees about the order in which the permutations are yielded.
If no block is given, an enumerator is returned instead.
Examples:
a = [1, 2, 3]
a.permutation.to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
a.permutation(1).to_a #=> [[1],[2],[3]]
a.permutation(2).to_a #=> [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
a.permutation(3).to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
a.permutation(0).to_a #=> [[]] # one permutation of length 0
a.permutation(4).to_a #=> [] # no permutations of length 4

Did you also know that the Enumerable class in Ruby has an all? method that will test a block of code on all elements of the argument & return true if they meet them or false if at least one does not? That can certainly come in handy.

all? [{ |obj| block } ] → true or false
Passes each element of the collection to the given block. The method returns true if the block never returns false or nil. If the block is not given, Ruby adds an implicit block of{ |obj| obj } which will cause all? to return true when none of the collection members are false or nil.
%w[ant bear cat].all? { |word| word.length >= 3 } #=> true
%w[ant bear cat].all? { |word| word.length >= 4 } #=> false
[nil, true, 99].all? #=> false

So it’s obviously important to know your vocabulary. It’s key to writing easy-to-read, expressive code. Is there a best way to learn the vocabulary of a language?

Experienced developers have told me that most of the time it’s a matter of blunt force memorization over time, getting experience with a language or framework. I think there’s a better way, though. There always is. In grade school, we learned 20 new vocabulary words each week. We had to read the vocabulary word, learn how to pronounce it correctly, spell it correctly, know the definition, and be able to use it in a sentence. Reading documentation can be boring & it often doesn’t explain things well enough. Why don’t we have a [insert software language here] vocabulary of the week for beginners to help us get up to speed faster?

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.