יום שישי, 12 ביולי 2013

unless in Ruby

The unless is ruby is equivalents to if not
Sample code

  1:   fileName = 'C:\\learn\\Ruby\\TestFiles\\myTest.txt'
  2:   f = File.open(fileName, "r") 
  3:   f.each_line do |line|
  4:   next unless line =~ /\d{2,6}/ # Skip lines with no numbers with len < 2 
  5:     puts line
  6:   end

Input data
13333333333333333333333333333
2
3
4
Zvika
5
6
7
Shaaf


And the result
C:\learn\Ruby>test5.rb
2
3
4
Zvika
5
6
7
Shaaf

אין תגובות:

הוסף רשומת תגובה