teaching machines

CS 330 Lecture 6 – Regex in Ruby

February 6, 2015 by . Filed under cs330, lectures, spring 2015.

Agenda

TODO

Code

isa.rb

#!/usr/bin/env ruby

path = ARGV[0].downcase

if path =~ /\.(jpeg|jpg)$/
  puts 'You jpeg.'
end

nofoo.rb

#!/usr/bin/env ruby

text = IO.read(ARGV[0])

if text !~ /foo/
  exit 0
else
  exit 1
end

nocomments.rb

#!/usr/bin/env ruby
#
   # this is a comment

File.readlines(ARGV[0]).each do |line| # asdfasd
  if line !~ /^\s*#/
    puts line
  end
end

ocracy.rb

#!/usr/bin/env ruby

lines = File.readlines('/usr/share/dict/words')

puts lines.grep(/(ocracy|archy)$/)

Haiku

on fuzzy matching
I lost my puppy
Sadly I had no photos
So I hung no signs