Ruby Trick Shots
rubyreloaded.com
rubyreloaded.com
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.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).
This needs to be drilled into the heads of everyone learning Ruby. Saves much head-scratching later on.
Ruby: Objects, messages, and expressions. (Mostly)
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.
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".This also works in node. Doesn't work in python though -- python just displays both values.
1. Get the bytecode for some code:
irb(main):001:0> x = RubyVM::InstructionSequence.new '1 + 1'
=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
irb(main):002:0> puts x.disasm
== disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
0000 trace 1 ( 1)
0002 putobject 1
0004 putobject 1
0006 opt_plus <ic:1>
0008 leave
=> nil
irb(main):003:0>
2. Output parser states and debugging information: $ ruby -y -e'1 + 1'
(The output is long so I haven't pasted it. :-) ) $ bin/rbx compile -A -S -B -e '1 + 1' # A(ST), S(exp), B(ytecode)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.
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.
printf("%d ... blah blah blah"
"%f ...", 1, 1.2); 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 :0Also seems mainly like a foot-shooting manual so far!
But it's certainly true that I didn't know a number of these behaviors!
Could be, but a key value for examples of stuff you'd probably be better off not using very often is in learning why things behave as they do.
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!
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"
endRuby 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.