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.
This 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.
Ohh, 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..
Pry 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 :-)
Another 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:
Any chance of a command-line session transcript instead?
I suspect there may be some interesting content here, but this is the kind of thing where I just hate screencasts. I can't skim it to see if these are things I already know or care to know, and even if they are it'll take me half an hour to get through, when I could probably read through the command-line session in 5 minutes.
When I do a video like this, usually someone does something like that within a few hours(!) but if not, I can do one. That said, it will all be on the Web eventually - the video was just to promote the effort (and put me on the line to finish it ;-))
As a list, though, it digs into: random numbers from a range, awesome_print, curious concatenation, fiddle, simple substring presence, spaces as string literal delimiters, multiple module inclusion, shorthand for ivar interpolation, ruby syntax checking, ruby syntax analysis with ripper, chained ifs, "next" as Proc's return, zipping arrays, exploding ranges into arrays, json output, __method__, multi line chaining, _ in irb, checking set bits, params defaulting to other params, Proc#source_location, prepending to a string in place, accessing the current source file as data, and some regex tricks.
Nice collection. The weird concatenation, I suspect, is a part of the grammar. It's frequently used in C code as well, when the format string is too long to fit in one line.
It actually works for string literals only, so you can't do a real concatenation there, with a variable for example. In C it's done at compile time, so you never actually concat those strings, the literals are merged. From what I see it's the same in ruby:
irb(main):001:0> a = "avs"
=> "avs"
irb(main):002:0> "sada" "sadasd"
=> "sadasadasd"
irb(main):003:0> "sada" "sadasd" "sda"
=> "sadasadasdsda"
irb(main):004:0> "sada" "sadasd" a
SyntaxError: compile error
(irb):4: syntax error, unexpected tIDENTIFIER, expecting $end
from (irb):4
from :0
The idea of Technical books on Kindle is not new.
I have converted numerous Sitepoint PDF books to Kindle format and read them on the Kindle AND on my computer using the Kindle app for Mac.
I loved the video and PROMISE to buy the Kindle book when it is available.
Yeah, I'm discovering that now, lol. My "research" consisted entirely of searching for "ruby" and going through the first few pages. They were nearly entirely Kindle versions of established print books. Turns out.. Amazon prefers to feature books that actually sell ;-)
I've since discovered some cheap indie Kindle programming books and even a whole range of cheap O'Reilly "What is *?" books. So it seems there's a market but IMHO not a very mature one, so I look forward to giving it a go. Thanks for the support!
For the array zip one @14:20, I'm surprised he didn't briefly mention that you can use unpacking in the block parameters:
names = ['fred', 'bill']
ages = [33, 40]
locations = ['san francisco', 'dubai']
names.zip(ages, locations) do |n, a, l|
puts n
puts "lives in " + l
puts "is #{a} years old"
end
Ruby is such an amazing and zany roadtrip of a language ... these few little gems have got me hungry for more. Its awfully addictive when you realise you can do so much with so little.
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:
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: Now the only thing printed out to the terminal will be 0, but s will still get the contents of largefile.txt.