3 ms·
> However, this can be avoided by using a semicolon and another statement: Just a semicolon is sufficient, actually. Also, importantly, it's not a statement,
by rue 15y ago
> However, this can be avoided by using a semicolon and another statement:
Just a semicolon is sufficient, actually.
Also, importantly, it's not a statement, it's an expression (which is why you see the return value of your read to begin with).
- jamesbritt 15y agoAlso, importantly, it's not a statement, it's an expression (which is why you see the return value of your read to begin with) This needs to be drilled into the heads of everyone learning Ruby. Saves much head-scratching later on. Ruby: Objects, messages, and expressions. (Mostly)
- Cushman 15y agoA semicolon is not exactly sufficient-- it leaves the expression open, and thus unexecuted, until you close it. Try `puts 1;` vs. `puts 1;0`.
- Schwolop 15y agoThis confused the hell out of me when I came from MatLab to Ruby. I thought the semicolon was working like in MatLab to just suppress output. It wasn't... Many bugs ensued! Having now watched the video, if someone had told me that '_' in irb was equivalent to 'ans' in MatLab, I would have been very happy.
- petercooper 15y agoOhh, you're giving me ideas now. How great would it be to have a whole panoply of "[language a] for [language b] developers" videos or articles ;-) I know of a few but still..
- rue 15y agoYou're quite right! irb does indeed do that… I've been using pry, which handles it ‘correctly’.
- Cushman 15y agoIn that case, I'd say we're both right-- though it's an open question what's the "right" behavior.