I'm new to ruby and don't understand why the following code does what is does. Can anyone enlighten me?
ruby -e 'a = [1,2,3,4,5,6,7,8,9,10]; a.each { |x| if (x === 2..5 ) then print x, " " end}; puts '
2 3 4 5 6 7 8 9 10
I would have thought this would be the same as
ruby -e 'a = [1,2,3,4,5,6,7,8,9,10]; a.each { |x| case x; when 2..5 then print x, " "; end}; puts '
2 3 4 5
I am also confused that
ruby -e 'while gets; print if /<div id=.*$/ .. /<\/body>/; end' < fred
<div id="eutpuhe">
UETHUEPUK
THEUTEUH
teuhepxn
thueonhoe
ononueuouh
</div>
</body>
and a three . range test give the same output.
ruby -e 'while gets; print if /<div id=.*$/ ... /<\/body>/; end' < fred
<div id="eutpuhe">
UETHUEPUK
THEUTEUH
teuhepxn
thueonhoe
ononueuouh
</div>
</body>
The original file 'fred' is
<html>
<title>hello</title>
<body>
<div id="eutpuhe">
UETHUEPUK
THEUTEUH
teuhepxn
thueonhoe
ononueuouh
</div>
</body>
</html>
ruby -v
ruby 1.8.4 (2005-12-24) [powerpc-darwin8.6.0]
Compiled from the distribution, not the Mac OS shipped version.
Rob
Rob Burrowes Ph: +64 (9) 3737 599 Extn 87972
Computing Services Manager Fax: +64 (9) 3737 453
Computer Science Department Mob:+64 (27) 4731 856
The University of Auckland
Rm 391, Level 3
38 Princes Street http://www.cs.auckland.ac.nz/~rob
Private bag 92019 http://www.wikarekare.org
Auckland 1001 http://www.burrowes.org