Hi I’m currently looking at a ruby quiz solution (http://rubyquiz.com/quiz76.html) and it makes sense to me, but I am having trouble understanding why there isn’t a self or any type of receiver for the methods shift, pop, scramble in munge_word:
class Array
def munge_each
map { |word| word.split(//).munge_word }
end
def munge_word
first, last, middle = shift, pop, scramble
"#{first}#{middle}#{last}"
end
def scramble
sort_by{rand}
end
end
Is it because it opened up the Array class and added functions to it? Are those methods class methods? I thought that syntax of var1, var2 = var3, var4 was just for concise multiple assignment of variables. I’ve never seen it done with methods on one side. Can someone explain?
This is essentially equivalent to
The receiver (for
shift,popandscramblemethods) is implicit and it isself. Which is an instance ofArray, becausemunge_wordis an instance method.Variables, methods – it doesn’t matter. Right-hand values just need to be a list (or array) of expressions. Any expressions. Look: