DEV Community

Eben Eleazer
Eben Eleazer

Posted on • Updated on

Completing My First Assignments

epiphany = "When assigning, Always put the thing you're assigning into on the left"

I'm actually learning something!

So it's been a couple of weeks, of working in Ruby and honestly it's been pretty cool. I've been working with some variables, some methods, even started writing out simple classes.

But every now and then I was getting these crazy bugs. I would get errors like "Undefined Method" or I would have arrays with some of the objects missing. It would drive me up the wall!

Then I realized the mistake I was making.

Do Your Assignment Operations in the Correct Order

You see, I was thinking of the assignment operator the wrong way. It looks like an equals sign (Well, it technically is but that doesn't mean it functions the same way) so in my head I was treating it as "equals". Of course I know about "==", but in my head everything on both sides of "=" was the same. I was thinking of it as setting one thing "equal" to the other.

"=" doesn't work like it does in math class.

But, that's where I was getting hung up. You're not setting them "equal" to each other. You're "assigning" the thing on the left to be the thing on the right.

That operator is directional

By flipping the order (or ignoring it) I was assigning things like array indexes or string objects (you know, the very values that I was trying to keep) to be empty variables. No wonder my code kept breaking!

So that's my epiphany. Use the assignment operator in the correct order, and you can avoid ruining your code!

Top comments (0)