mercredi 8 août 2018

Ruby's variable equals if variable equals method?

I am reading through createyourlang book and saw a code snippet that looks like this:

  tokenizer = if identifier = chunk[IDENTIFIER, 1]
                IdentifierTokenizer.new(identifier, tokenizer).tokenize
              elsif constant = chunk[CONSTANT, 1]
                ConstantTokenizer.new(constant, tokenizer).tokenize
              elsif number = chunk[NUMBER, 1]
                ...

I find it confusing having two equal signs on the same line. What does it mean to have A = if B = C?

If you're wondering what chunk is, assume chunk is string "hello" and chunk[IDENTIFIER,1] equals to "hello" and the other chunk[OTHER_CONST,1] equals to nil. The function works. You can find the source code repo here. I am mainly curious how to read this function/ if there is a better way to rewrite this code to make it more readable?

Aucun commentaire:

Enregistrer un commentaire