5 ms·
One handy trick that I've found a lot of people don't know: There's often the problem when working in IRB that you want to run a command that will produce a hu
by necubi 15y ago
One handy trick that I've found a lot of people don't know:
There's often the problem when working in IRB that you want to run a command that will produce a huge amount of output. For example, you might be reading a large file and assigning its value to a variable:
s = File.read("largefile.txt")
This can take a long time, as you wait for Ruby to print the entire contents to the terminal. However, this can be avoided by using a semicolon and another statement:
s = File.read("largefile.txt"); 0
Now the only thing printed out to the terminal will be 0, but s will still get the contents of largefile.txt.
- tkahn6 15y agoI wonder if this saves time because Ruby does lazy file i/o.
- michaelmior 15y agoTerminal output is just really slow.
- 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.
- philwelch 15y agoAnother neat IRB trick is to put a useful expression after the semicolon, like in your case s.length. This is especially useful on, say, the Rails console. Also keep in mind that Ruby can evaluate lots of different expressions, so if say you calculated a number "n" and expect that there are "n" records that meet a given query, you can go: stuff = SomeModel.somescope; stuff.size == n and Ruby will just say "true" or "false".
- ConstantineXVI 15y agoPry[1] will less (and pretty print, for hashes and such) returns, not to mention it's other tricks. [1] http://pry.github.com http://pry.github.com
- petercooper 15y agoPry is in exactly the same basket as awesome_print in terms of awesomeness, IMHO. It just did make my genuinely random selection for this video but will definitely be in the finished item :-)
- kbd 15y agoNeat, works in the CoffeeScript REPL too, thanks!
- gcr 15y agoNice trick, thanks! This also works in node. Doesn't work in python though -- python just displays both values.