From 20cc0418476d2f27276fbf4cfe42fd884092befd Mon Sep 17 00:00:00 2001 From: murphy Date: Sat, 29 Oct 2005 15:11:36 +0000 Subject: ALL-TESTS.rb: not needed. test/ruby/example.in.rb: PLEAC part was doubled - deleted and adjusted target. suite.rb: back to debug hints. --- test/ruby/example.in.rb | 6281 ---------------------------------------- test/ruby/example.out.raydebug | 6281 ---------------------------------------- 2 files changed, 12562 deletions(-) (limited to 'test/ruby') diff --git a/test/ruby/example.in.rb b/test/ruby/example.in.rb index c89d3ab..8e6d20e 100644 --- a/test/ruby/example.in.rb +++ b/test/ruby/example.in.rb @@ -3786,6285 +3786,4 @@ if $0 == __FILE__ break if c.typ == TokenConsts::EOP end end -# -*- ruby -*- - -# Local variables: -# indent-tabs-mode: nil -# ruby-indent-level: 4 -# End: - -# @@PLEAC@@_NAME -# @@SKIP@@ Ruby - -# @@PLEAC@@_WEB -# @@SKIP@@ http://www.ruby-lang.org - - -# @@PLEAC@@_1.0 -string = '\n' # two characters, \ and an n -string = 'Jon \'Maddog\' Orwant' # literal single quotes - -string = "\n" # a "newline" character -string = "Jon \"Maddog\" Orwant" # literal double quotes - -string = %q/Jon 'Maddog' Orwant/ # literal single quotes - -string = %q[Jon 'Maddog' Orwant] # literal single quotes -string = %q{Jon 'Maddog' Orwant} # literal single quotes -string = %q(Jon 'Maddog' Orwant) # literal single quotes -string = %q # literal single quotes - -a = <<"EOF" -This is a multiline here document -terminated by EOF on a line by itself -EOF - - -# @@PLEAC@@_1.1 -value = string[offset,count] -value = string[offset..-1] - -string[offset,count] = newstring -string[offset..-1] = newtail - -# in Ruby we can also specify intervals by their two offsets -value = string[offset..offs2] -string[offset..offs2] = newstring - -leading, s1, s2, trailing = data.unpack("A5 x3 A8 A8 A*") - -fivers = string.unpack("A5" * (string.length/5)) - -chars = string.unpack("A1" * string.length) - -string = "This is what you have" -# +012345678901234567890 Indexing forwards (left to right) -# 109876543210987654321- Indexing backwards (right to left) -# note that 0 means 10 or 20, etc. above - -first = string[0, 1] # "T" -start = string[5, 2] # "is" -rest = string[13..-1] # "you have" -last = string[-1, 1] # "e" -end_ = string[-4..-1] # "have" -piece = string[-8, 3] # "you" - -string[5, 2] = "wasn't" # change "is" to "wasn't" -string[-12..-1] = "ondrous" # "This wasn't wondrous" -string[0, 1] = "" # delete first character -string[-10..-1] = "" # delete last 10 characters - -if string[-10..-1] =~ /pattern/ - puts "Pattern matches in last 10 characters" -end - -string[0, 5].gsub!(/is/, 'at') - -a = "make a hat" -a[0, 1], a[-1, 1] = a[-1, 1], a[0, 1] - -a = "To be or not to be" -b = a.unpack("x6 A6") - -b, c = a.unpack("x6 A2 X5 A2") -puts "#{b}\n#{c}\n" - -def cut2fmt(*args) - template = '' - lastpos = 1 - for place in args - template += "A" + (place - lastpos).to_s + " " - lastpos = place - end - template += "A*" - return template -end - -fmt = cut2fmt(8, 14, 20, 26, 30) - - -# @@PLEAC@@_1.2 -# careful! "b is true" doesn't mean "b != 0" (0 is true in Ruby) -# thus no problem of "defined" later since only nil is false -# the following sets to `c' if `b' is nil or false -a = b || c - -# if you need Perl's behaviour (setting to `c' if `b' is 0) the most -# effective way is to use Numeric#nonzero? (thanks to Dave Thomas!) -a = b.nonzero? || c - -# you will still want to use defined? in order to test -# for scope existence of a given object -a = defined?(b) ? b : c - -dir = ARGV.shift || "/tmp" - - -# @@PLEAC@@_1.3 -v1, v2 = v2, v1 - -alpha, beta, production = %w(January March August) -alpha, beta, production = beta, production, alpha - - -# @@PLEAC@@_1.4 -num = char[0] -char = num.chr - -# Ruby also supports having a char from character constant -num = ?r - -char = sprintf("%c", num) -printf("Number %d is character %c\n", num, num) - -ascii = string.unpack("C*") -string = ascii.pack("C*") - -hal = "HAL" -ascii = hal.unpack("C*") -# We can't use Array#each since we can't mutate a Fixnum -ascii.collect! { |i| - i + 1 # add one to each ASCII value -} -ibm = ascii.pack("C*") -puts ibm - - -# @@PLEAC@@_1.5 -array = string.split('') - -array = string.unpack("C*") - -string.scan(/./) { |b| - # do something with b -} - -string = "an apple a day" -print "unique chars are: ", string.split('').uniq.sort, "\n" - -sum = 0 -for ascval in string.unpack("C*") # or use Array#each for a pure OO style :) - sum += ascval -end -puts "sum is #{sum & 0xffffffff}" # since Ruby will go Bignum if necessary - -# @@INCLUDE@@ include/ruby/slowcat.rb - - -# @@PLEAC@@_1.6 -revbytes = string.reverse - -revwords = string.split(" ").reverse.join(" ") - -revwords = string.split(/(\s+)/).reverse.join - -# using the fact that IO is Enumerable, you can directly "select" it -long_palindromes = File.open("/usr/share/dict/words"). - select { |w| w.chomp!; w.reverse == w && w.length > 5 } - - -# @@PLEAC@@_1.7 -while string.sub!("\t+") { ' ' * ($&.length * 8 - $`.length % 8) } -end - - -# @@PLEAC@@_1.8 -'You owe #{debt} to me'.gsub(/\#{(\w+)}/) { eval($1) } - -rows, cols = 24, 80 -text = %q(I am #{rows} high and #{cols} long) -text.gsub!(/\#{(\w+)}/) { eval("#{$1}") } -puts text - -'I am 17 years old'.gsub(/\d+/) { 2 * $&.to_i } - - -# @@PLEAC@@_1.9 -e = "bo peep".upcase -e.downcase! -e.capitalize! - -"thIS is a loNG liNE".gsub!(/\w+/) { $&.capitalize } - - -# @@PLEAC@@_1.10 -"I have #{n+1} guanacos." -print "I have ", n+1, " guanacos." - - -# @@PLEAC@@_1.11 -var = <<'EOF'.gsub(/^\s+/, '') - your text - goes here -EOF - - -# @@PLEAC@@_1.12 -string = "Folding and splicing is the work of an editor,\n"+ - "not a mere collection of silicon\n"+ - "and\n"+ - "mobile electrons!" - -def wrap(str, max_size) - all = [] - line = '' - for l in str.split - if (line+l).length >= max_size - all.push(line) - line = '' - end - line += line == '' ? l : ' ' + l - end - all.push(line).join("\n") -end - -print wrap(string, 20) -#=> Folding and -#=> splicing is the -#=> work of an editor, -#=> not a mere -#=> collection of -#=> silicon and mobile -#=> electrons! - - -# @@PLEAC@@_1.13 -string = %q(Mom said, "Don't do that.") -string.gsub(/['"]/) { '\\'+$& } -string.gsub(/['"]/, '\&\&') -string.gsub(/[^A-Z]/) { '\\'+$& } -"is a test!".gsub(/\W/) { '\\'+$& } # no function like quotemeta? - - -# @@PLEAC@@_1.14 -string.strip! - - -# @@PLEAC@@_1.15 -def parse_csv(text) - new = text.scan(/"([^\"\\]*(?:\\.[^\"\\]*)*)",?|([^,]+),?|,/) - new << nil if text[-1] == ?, - new.flatten.compact -end - -line = %q -fields = parse_csv(line) -fields.each_with_index { |v,i| - print "#{i} : #{v}\n"; -} - - -# @@PLEAC@@_1.16 -# Use the soundex.rb Library from Michael Neumann. -# http://www.s-direktnet.de/homepages/neumann/rb_prgs/Soundex.rb -require 'Soundex' - -code = Text::Soundex.soundex(string) -codes = Text::Soundex.soundex(array) - -# substitution function for getpwent(): -# returns an array of user entries, -# each entry contains the username and the full name -def login_names - result = [] - File.open("/etc/passwd") { |file| - file.each_line { |line| - next if line.match(/^#/) - cols = line.split(":") - result.push([cols[0], cols[4]]) - } - } - result -end - -puts "Lookup user: " -user = STDIN.gets -user.chomp! -exit unless user -name_code = Text::Soundex.soundex(user) - -splitter = Regexp.new('(\w+)[^,]*\b(\w+)') -for username, fullname in login_names do - firstname, lastname = splitter.match(fullname)[1,2] - if name_code == Text::Soundex.soundex(username) - || name_code == Text::Soundex.soundex(firstname) - || name_code == Text::Soundex.soundex(lastname) - then - puts "#{username}: #{firstname} #{lastname}" - end -end - - -# @@PLEAC@@_1.17 -# @@INCLUDE@@ include/ruby/fixstyle.rb - - -# @@PLEAC@@_1.18 -# @@INCLUDE@@ include/ruby/psgrep.rb - - -# @@PLEAC@@_2.1 -# Matz tells that you can use Integer() for strict checked conversion. -Integer("abc") -#=> `Integer': invalid value for Integer: "abc" (ArgumentError) -Integer("567") -#=> 567 - -# You may use Float() for floating point stuff -Integer("56.7") -#=> `Integer': invalid value for Integer: "56.7" (ArgumentError) -Float("56.7") -#=> 56.7 - -# You may also use a regexp for that -if string =~ /^[+-]?\d+$/ - p 'is an integer' -else - p 'is not' -end - -if string =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ - p 'is a decimal number' -else - p 'is not' -end - - -# @@PLEAC@@_2.2 -# equal(num1, num2, accuracy) : returns true if num1 and num2 are -# equal to accuracy number of decimal places -def equal(i, j, a) - sprintf("%.#{a}g", i) == sprintf("%.#{a}g", j) -end - -wage = 536 # $5.36/hour -week = 40 * wage # $214.40 -printf("One week's wage is: \$%.2f\n", week/100.0) - - -# @@PLEAC@@_2.3 -num.round # rounds to integer - -a = 0.255 -b = sprintf("%.2f", a) -print "Unrounded: #{a}\nRounded: #{b}\n" -printf "Unrounded: #{a}\nRounded: %.2f\n", a - -print "number\tint\tfloor\tceil\n" -a = [ 3.3 , 3.5 , 3.7, -3.3 ] -for n in a - printf("% .1f\t% .1f\t% .1f\t% .1f\n", # at least I don't fake my output :) - n, n.to_i, n.floor, n.ceil) -end - - -# @@PLEAC@@_2.4 -def dec2bin(n) - [n].pack("N").unpack("B32")[0].sub(/^0+(?=\d)/, '') -end - -def bin2dec(n) - [("0"*32+n.to_s)[-32..-1]].pack("B32").unpack("N")[0] -end - - -# @@PLEAC@@_2.5 -for i in x .. y - # i is set to every integer from x to y, inclusive -end - -x.step(y,7) { |i| - # i is set to every integer from x to y, stepsize = 7 -} - -print "Infancy is: " -(0..2).each { |i| - print i, " " -} -print "\n" - - -# @@PLEAC@@_2.6 -# We can add conversion methods to the Integer class, -# this makes a roman number just a representation for normal numbers. -class Integer - - @@romanlist = [["M", 1000], - ["CM", 900], - ["D", 500], - ["CD", 400], - ["C", 100], - ["XC", 90], - ["L", 50], - ["XL", 40], - ["X", 10], - ["IX", 9], - ["V", 5], - ["IV", 4], - ["I", 1]] - - def to_roman - remains = self - roman = "" - for sym, num in @@romanlist - while remains >= num - remains -= num - roman << sym - end - end - roman - end - - def Integer.from_roman(roman) - ustr = roman.upcase - sum = 0 - for entry in @@romanlist - sym, num = entry[0], entry[1] - while sym == ustr[0, sym.length] - sum += num - ustr.slice!(0, sym.length) - end - end - sum - end - -end - - -roman_fifteen = 15.to_roman -puts "Roman for fifteen is #{roman_fifteen}" -i = Integer.from_roman(roman_fifteen) -puts "Converted back, #{roman_fifteen} is #{i}" - -# check -for i in (1..3900) - r = i.to_roman - j = Integer.from_roman(r) - if i != j - puts "error: #{i} : #{r} - #{j}" - end -end - - -# @@PLEAC@@_2.7 -random = rand(y-x+1)+x - -chars = ["A".."Z","a".."z","0".."9"].collect { |r| r.to_a }.join + %q(!@$%^&*) -password = (1..8).collect { chars[rand(chars.size)] }.pack("C*") - - -# @@PLEAC@@_2.8 -srand # uses a combination of the time, the process id, and a sequence number -srand(val) # for repeatable behaviour - - -# @@PLEAC@@_2.9 -# from the randomr lib: -# http://raa.ruby-lang.org/project/randomr/ -----> http://raa.ruby-lang.org/project/randomr/ - -require 'random/mersenne_twister' -mers = Random::MersenneTwister.new 123456789 -puts mers.rand(0) # 0.550321932544541 -puts mers.rand(10) # 2 - -# using online sources of random data via the realrand package: -# http://raa.ruby-lang.org/project/realrand/ -# **Note** -# The following online services are used in this package: -# http://www.random.org - source: atmospheric noise -# http://www.fourmilab.ch/hotbits - source: radioactive decay timings -# http://random.hd.org - source: entropy from local and network noise -# Please visit the sites and respect the rules of each service. - -require 'random/online' - -generator1 = Random::RandomOrg.new -puts generator1.randbyte(5).join(",") -puts generator1.randnum(10, 1, 6).join(",") # Roll dice 10 times. - -generator2 = Random::FourmiLab.new -puts generator2.randbyte(5).join(",") -# randnum is not supported. - -generator3 = Random::EntropyPool.new -puts generator3.randbyte(5).join(",") -# randnum is not supported. - - -# @@PLEAC@@_2.10 -def gaussian_rand - begin - u1 = 2 * rand() - 1 - u2 = 2 * rand() - 1 - w = u1*u1 + u2*u2 - end while (w >= 1) - w = Math.sqrt((-2*Math.log(w))/w) - [ u2*w, u1*w ] -end - -mean = 25 -sdev = 2 -salary = gaussian_rand[0] * sdev + mean -printf("You have been hired at \$%.2f\n", salary) - - -# @@PLEAC@@_2.11 -def deg2rad(d) - (d/180.0)*Math::PI -end - -def rad2deg(r) - (r/Math::PI)*180 -end - - -# @@PLEAC@@_2.12 -sin_val = Math.sin(angle) -cos_val = Math.cos(angle) -tan_val = Math.tan(angle) - -# AFAIK Ruby's Math module doesn't provide acos/asin -# While we're at it, let's also define missing hyperbolic functions -module Math - def Math.asin(x) - atan2(x, sqrt(1 - x**2)) - end - def Math.acos(x) - atan2(sqrt(1 - x**2), x) - end - def Math.atan(x) - atan2(x, 1) - end - def Math.sinh(x) - (exp(x) - exp(-x)) / 2 - end - def Math.cosh(x) - (exp(x) + exp(-x)) / 2 - end - def Math.tanh(x) - sinh(x) / cosh(x) - end -end - -# The support for Complex numbers is not built-in -y = Math.acos(3.7) -#=> in `sqrt': square root for negative number (ArgumentError) - -# There is an implementation of Complex numbers in 'complex.rb' in current -# Ruby distro, but it doesn't support atan2 with complex args, so it doesn't -# solve this problem. - - -# @@PLEAC@@_2.13 -log_e = Math.log(val) -log_10 = Math.log10(val) - -def log_base(base, val) - Math.log(val)/Math.log(base) -end - -answer = log_base(10, 10_000) -puts "log10(10,000) = #{answer}" - - -# @@PLEAC@@_2.14 -require 'matrix.rb' - -a = Matrix[[3, 2, 3], [5, 9, 8]] -b = Matrix[[4, 7], [9, 3], [8, 1]] -c = a * b - -a.row_size -a.column_size - -c.det -a.transpose - - -# @@PLEAC@@_2.15 -require 'complex.rb' -require 'rational.rb' - -a = Complex(3, 5) # 3 + 5i -b = Complex(2, -2) # 2 - 2i -puts "c = #{a*b}" - -c = a * b -d = 3 + 4*Complex::I - -printf "sqrt(#{d}) = %s\n", Math.sqrt(d) - - -# @@PLEAC@@_2.16 -number = hexadecimal.hex -number = octal.oct - -print "Gimme a number in decimal, octal, or hex: " -num = gets.chomp -exit unless defined?(num) -num = num.oct if num =~ /^0/ # does both oct and hex -printf "%d %x %o\n", num, num, num - -print "Enter file permission in octal: " -permissions = gets.chomp -raise "Exiting ...\n" unless defined?(permissions) -puts "The decimal value is #{permissions.oct}" - - -# @@PLEAC@@_2.17 -def commify(n) - n.to_s =~ /([^\.]*)(\..*)?/ - int, dec = $1.reverse, $2 ? $2 : "" - while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3') - end - int.reverse + dec -end - - -# @@PLEAC@@_2.18 -printf "It took %d hour%s\n", time, time == 1 ? "" : "s" - -# dunno if an equivalent to Lingua::EN::Inflect exists... - - -# @@PLEAC@@_2.19 -#----------------------------- -#!/usr/bin/ruby -# bigfact - calculating prime factors -def factorize(orig) - factors = {} - factors.default = 0 # return 0 instead nil if key not found in hash - n = orig - i = 2 - sqi = 4 # square of i - while sqi <= n do - while n.modulo(i) == 0 do - n /= i - factors[i] += 1 - # puts "Found factor #{i}" - end - # we take advantage of the fact that (i +1)**2 = i**2 + 2*i +1 - sqi += 2 * i + 1 - i += 1 - end - - if (n != 1) && (n != orig) - factors[n] += 1 - end - factors -end - -def printfactorhash(orig, factorcount) - print format("%-10d ", orig) - if factorcount.length == 0 - print "PRIME" - else - # sorts after number, because the hash keys are numbers - factorcount.sort.each { |factor,exponent| - print factor - if exponent > 1 - print "**", exponent - end - print " " - } - end - puts -end - -for arg in ARGV - n = arg.to_i - mfactors = factorize(n) - printfactorhash(n, mfactors) -end -#----------------------------- - - -# @@PLEAC@@_3.0 -puts Time.now - -print "Today is day ", Time.now.yday, " of the current year.\n" -print "Today is day ", Time.now.day, " of the current month.\n" - - -# @@PLEAC@@_3.1 -day, month, year = Time.now.day, Time.now.month, Time.now.year -# or -day, month, year = Time.now.to_a[3..5] - -tl = Time.now.localtime -printf("The current date is %04d %02d %02d\n", tl.year, tl.month, tl.day) - -Time.now.localtime.strftime("%Y-%m-%d") - - -# @@PLEAC@@_3.2 -Time.local(year, month, day, hour, minute, second).tv_sec -Time.gm(year, month, day, hour, minute, second).tv_sec - - -# @@PLEAC@@_3.3 -sec, min, hour, day, month, year, wday, yday, isdst, zone = Time.at(epoch_secs).to_a - - -# @@PLEAC@@_3.4 -when_ = now + difference # now -> Time ; difference -> Numeric (delta in seconds) -then_ = now - difference - - -# @@PLEAC@@_3.5 -bree = 361535725 -nat = 96201950 - -difference = bree - nat -puts "There were #{difference} seconds between Nat and Bree" - -seconds = difference % 60 -difference = (difference - seconds) / 60 -minutes = difference % 60 -difference = (difference - minutes) / 60 -hours = difference % 24 -difference = (difference - hours) / 24 -days = difference % 7 -weeks = (difference - days) / 7 - -puts "(#{weeks} weeks, #{days} days, #{hours}:#{minutes}:#{seconds})" - - -# @@PLEAC@@_3.6 -monthday, weekday, yearday = date.mday, date.wday, date.yday - -# AFAIK the week number is not just a division since week boundaries are on sundays -weeknum = d.strftime("%U").to_i + 1 - -year = 1981 -month = "jun" # or `6' if you want to emulate a broken language -day = 16 -t = Time.mktime(year, month, day) -print "#{month}/#{day}/#{year} was a ", t.strftime("%A"), "\n" - - -# @@PLEAC@@_3.7 -yyyy, mm, dd = $1, $2, $3 if "1998-06-25" =~ /(\d+)-(\d+)-(\d+)/ - -epoch_seconds = Time.mktime(yyyy, mm, dd).tv_sec - -# dunno an equivalent to Date::Manip#ParseDate - - -# @@PLEAC@@_3.8 -string = Time.at(epoch_secs) -Time.at(1234567890).gmtime # gives: Fri Feb 13 23:31:30 UTC 2009 - -time = Time.mktime(1973, "jan", 18, 3, 45, 50) -print "In localtime it gives: ", time.localtime, "\n" - - -# @@PLEAC@@_3.9 -# Ruby provides micro-seconds in Time object -Time.now.usec - -# Ruby gives the seconds in floating format when substracting two Time objects -before = Time.now -line = gets -elapsed = Time.now - before -puts "You took #{elapsed} seconds." - -# On my Celeron-400 with Linux-2.2.19-14mdk, average for three execs are: -# This Ruby version: average 0.00321 sec -# Cookbook's Perl version: average 0.00981 sec -size = 500 -number_of_times = 100 -total_time = 0 -number_of_times.times { - # populate array - array = [] - size.times { array << rand } - # sort it - begin_ = Time.now - array.sort! - time = Time.now - begin_ - total_time += time -} -printf "On average, sorting %d random numbers takes %.5f seconds\n", - size, (total_time/Float(number_of_times)) - - -# @@PLEAC@@_3.10 -sleep(0.005) # Ruby is definitely not as broken as Perl :) -# (may be interrupted by sending the process a SIGALRM) - - -# @@PLEAC@@_3.11 -#!/usr/bin/ruby -w -# hopdelta - feed mail header, produce lines -# showing delay at each hop. -require 'time' -class MailHopDelta - - def initialize(mail) - @head = mail.gsub(/\n\s+/,' ') - @topline = %w-Sender Recipient Time Delta- - @start_from = mail.match(/^From.*\@([^\s>]*)/)[1] - @date = Time.parse(mail.match(/^Date:\s+(.*)/)[1]) - end - - def out(line) - "%-20.20s %-20.20s %-20.20s %s" % line - end - - def hop_date(day) - day.strftime("%I:%M:%S %Y/%m/%d") - end - - def puts_hops - puts out(@topline) - puts out(['Start', @start_from, hop_date(@date),'']) - @head.split(/\n/).reverse.grep(/^Received:/).each do |hop| - hop.gsub!(/\bon (.*?) (id.*)/,'; \1') - whence = hop.match(/;\s+(.*)$/)[1] - unless whence - warn "Bad received line: #{hop}" - next - end - from = $+ if hop =~ /from\s+(\S+)|\((.*?)\)/ - by = $1 if hop =~ /by\s+(\S+\.\S+)/ - next unless now = Time.parse(whence).localtime - delta = now - @date - puts out([from, by, hop_date(now), hop_time(delta)]) - @date = now - end - end - - def hop_time(secs) - sign = secs < 0 ? -1 : 1 - days, secs = secs.abs.divmod(60 * 60 * 24) - hours,secs = secs.abs.divmod(60 * 60) - mins, secs = secs.abs.divmod(60) - rtn = "%3ds" % [secs * sign] - rtn << "%3dm" % [mins * sign] if mins != 0 - rtn << "%3dh" % [hours * sign] if hours != 0 - rtn << "%3dd" % [days * sign] if days != 0 - rtn - end -end - -$/ = "" -mail = MailHopDelta.new(ARGF.gets).puts_hops - - -# @@PLEAC@@_4.0 -single_level = [ "this", "that", "the", "other" ] - -# Ruby directly supports nested arrays -double_level = [ "this", "that", [ "the", "other" ] ] -still_single_level = [ "this", "that", [ "the", "other" ] ].flatten - - -# @@PLEAC@@_4.1 -a = [ "quick", "brown", "fox" ] -a = %w(Why are you teasing me?) - -lines = <<"END_OF_HERE_DOC".gsub(/^\s*(.+)/, '\1') - The boy stood on the burning deck, - It was as hot as glass. -END_OF_HERE_DOC - -bigarray = IO.readlines("mydatafile").collect { |l| l.chomp } - -name = "Gandalf" -banner = %Q(Speak, #{name}, and welcome!) - -host_info = `host #{his_host}` - -%x(ps #{$$}) - -banner = 'Costs only $4.95'.split(' ') - -rax = %w! ( ) < > { } [ ] ! - - -# @@PLEAC@@_4.2 -def commify_series(a) - a.size == 0 ? '' : - a.size == 1 ? a[0] : - a.size == 2 ? a.join(' and ') : - a[0..-2].join(', ') + ', and ' + a[-1] -end - -array = [ "red", "yellow", "green" ] - -print "I have ", array, " marbles\n" -# -> I have redyellowgreen marbles - -# But unlike Perl: -print "I have #{array} marbles\n" -# -> I have redyellowgreen marbles -# So, needs: -print "I have #{array.join(' ')} marbles\n" -# -> I have red yellow green marbles - -def commify_series(a) - sepchar = a.select { |p| p =~ /,/ } != [] ? '; ' : ', ' - a.size == 0 ? '' : - a.size == 1 ? a[0] : - a.size == 2 ? a.join(' and ') : - a[0..-2].join(sepchar) + sepchar + 'and ' + a[-1] -end - - -# @@PLEAC@@_4.3 -# (note: AFAIK Ruby doesn't allow gory change of Array length) -# grow the array by assigning nil to past the end of array -ary[new_size-1] = nil -# shrink the array by slicing it down -ary.slice!(new_size..-1) -# init the array with given size -Array.new(number_of_elems) -# assign to an element past the original end enlarges the array -ary[index_new_last_elem] = value - -def what_about_that_array(a) - print "The array now has ", a.size, " elements.\n" - # Index of last element is not really interesting in Ruby - print "Element #3 is `#{a[3]}'.\n" -end -people = %w(Crosby Stills Nash Young) -what_about_that_array(people) - - -# @@PLEAC@@_4.4 -# OO style -bad_users.each { |user| - complain(user) -} -# or, functional style -for user in bad_users - complain(user) -end - -for var in ENV.keys.sort - puts "#{var}=#{ENV[var]}" -end - -for user in all_users - disk_space = get_usage(user) - if (disk_space > MAX_QUOTA) - complain(user) - end -end - -for l in IO.popen("who").readlines - print l if l =~ /^gc/ -end - -# we can mimic the obfuscated Perl way -while fh.gets # $_ is set to the line just read - chomp # $_ has a trailing \n removed, if it had one - split.each { |w| # $_ is split on whitespace - # but $_ is not set to each chunk as in Perl - print w.reverse - } -end -# ...or use a cleaner way -for l in fh.readlines - l.chomp.split.each { |w| print w.reverse } -end - -# same drawback as in problem 1.4, we can't mutate a Numeric... -array.collect! { |v| v - 1 } - -a = [ .5, 3 ]; b = [ 0, 1 ] -for ary in [ a, b ] - ary.collect! { |v| v * 7 } -end -puts "#{a.join(' ')} #{b.join(' ')}" - -# we can mutate Strings, cool; we need a trick for the scalar -for ary in [ [ scalar ], array, hash.values ] - ary.each { |v| v.strip! } # String#strip rules :) -end - - -# @@PLEAC@@_4.5 -# not relevant in Ruby since we have always references -for item in array - # do somethingh with item -end - - -# @@PLEAC@@_4.6 -unique = list.uniq - -# generate a list of users logged in, removing duplicates -users = `who`.collect { |l| l =~ /(\w+)/; $1 }.sort.uniq -puts("users logged in: #{commify_series(users)}") # see 4.2 for commify_series - - -# @@PLEAC@@_4.7 -a - b -# [ 1, 1, 2, 2, 3, 3, 3, 4, 5 ] - [ 1, 2, 4 ] -> [3, 5] - - -# @@PLEAC@@_4.8 -union = a | b -intersection = a & b -difference = a - b - - -# @@PLEAC@@_4.9 -array1.concat(array2) -# if you will assign to another object, better use: -new_ary = array1 + array2 - -members = [ "Time", "Flies" ] -initiates = [ "An", "Arrow" ] -members += initiates - -members = [ "Time", "Flies" ] -initiates = [ "An", "Arrow" ] -members[2,0] = [ "Like", initiates ].flatten - -members[0] = "Fruit" -members[3,2] = "A", "Banana" - - -# @@PLEAC@@_4.10 -reversed = ary.reverse - -ary.reverse_each { |e| - # do something with e -} - -descending = ary.sort.reverse -descending = ary.sort { |a,b| b <=> a } - - -# @@PLEAC@@_4.11 -# remove n elements from front of ary (shift n) -front = ary.slice!(0, n) - -# remove n elements from the end of ary (pop n) -end_ = ary.slice!(-n .. -1) - -# let's extend the Array class, to make that useful -class Array - def shift2() - slice!(0 .. 1) # more symetric with pop2... - end - def pop2() - slice!(-2 .. -1) - end -end - -friends = %w(Peter Paul Mary Jim Tim) -this, that = friends.shift2 - -beverages = %w(Dew Jolt Cola Sprite Fresca) -pair = beverages.pop2 - - -# @@PLEAC@@_4.12 -# use Enumerable#detect (or the synonym Enumerable#find) -highest_eng = employees.detect { |emp| emp.category == 'engineer' } - - -# @@PLEAC@@_4.13 -# use Enumerable#select (or the synonym Enumerable#find_all) -bigs = nums.select { |i| i > 1_000_000 } -pigs = users.keys.select { |k| users[k] > 1e7 } - -matching = `who`.select { |u| u =~ /^gnat / } - -engineers = employees.select { |e| e.position == 'Engineer' } - -secondary_assistance = applicants.select { |a| - a.income >= 26_000 && a.income < 30_000 -} - - -# @@PLEAC@@_4.14 -# normally you would have an array of Numeric (Float or -# Fixnum or Bignum), so you would use: -sorted = unsorted.sort -# if you have strings representing Integers or Floats -# you may specify another sort method: -sorted = unsorted.sort { |a,b| a.to_f <=> b.to_f } - -# let's use the list of my own PID's -`ps ux`.split("\n")[1..-1]. - select { |i| i =~ /^#{ENV['USER']}/ }. - collect { |i| i.split[1] }. - sort { |a,b| a.to_i <=> b.to_i }.each { |i| puts i } -puts "Select a process ID to kill:" -pid = gets.chomp -raise "Exiting ... \n" unless pid && pid =~ /^\d+$/ -Process.kill('TERM', pid.to_i) -sleep 2 -Process.kill('KILL', pid.to_i) - -descending = unsorted.sort { |a,b| b.to_f <=> a.to_f } - - -# @@PLEAC@@_4.15 -ordered = unordered.sort { |a,b| compare(a,b) } - -precomputed = unordered.collect { |e| [compute, e] } -ordered_precomputed = precomputed.sort { |a,b| a[0] <=> b[0] } -ordered = ordered_precomputed.collect { |e| e[1] } - -ordered = unordered.collect { |e| [compute, e] }. - sort { |a,b| a[0] <=> b[0] }. - collect { |e| e[1] } - -for employee in employees.sort { |a,b| a.name <=> b.name } - print employee.name, " earns \$ ", employee.salary, "\n" -end - -# Beware! `0' is true in Ruby. -# For chaining comparisons, you may use Numeric#nonzero?, which -# returns num if num is not zero, nil otherwise -sorted = employees.sort { |a,b| (a.name <=> b.name).nonzero? || b.age <=> a.age } - -users = [] -# getpwent is not wrapped in Ruby... let's fallback -IO.readlines('/etc/passwd').each { |u| users << u.split(':') } -users.sort! { |a,b| a[0] <=> b[0] } -for user in users - puts user[0] -end - -sorted = names.sort { |a,b| a[1, 1] <=> b[1, 1] } -sorted = strings.sort { |a,b| a.length <=> b.length } - -# let's show only the compact version -ordered = strings.collect { |e| [e.length, e] }. - sort { |a,b| a[0] <=> b[0] }. - collect { |e| e[1] } - -ordered = strings.collect { |e| [/\d+/.match(e)[0].to_i, e] }. - sort { |a,b| a[0] <=> b[0] }. - collect { |e| e[1] } - -print `cat /etc/passwd`.collect { |e| [e, e.split(':').indexes(3,2,0)].flatten }. - sort { |a,b| (a[1] <=> b[1]).nonzero? || (a[2] <=> b[2]).nonzero? || a[3] <=> b[3] }. - collect { |e| e[0] } - - -# @@PLEAC@@_4.16 -circular.unshift(circular.pop) # the last shall be first -circular.push(circular.shift) # and vice versa - -def grab_and_rotate(l) - l.push(ret = l.shift) - ret -end - -processes = [1, 2, 3, 4, 5] -while (1) - process = grab_and_rotate(processes) - puts "Handling process #{process}" - sleep 1 -end - - -# @@PLEAC@@_4.17 -def fisher_yates_shuffle(a) - (a.size-1).downto(1) { |i| - j = rand(i+1) - a[i], a[j] = a[j], a[i] if i != j - } -end - -def naive_shuffle(a) - for i in 0...a.size - j = rand(a.size) - a[i], a[j] = a[j], a[i] - end -end - - -# @@PLEAC@@_4.18 -#!/usr/bin/env ruby -# example 4-2 words -# words - gather lines, present in colums - -# class to encapsulate the word formatting from the input -class WordFormatter - def initialize(cols) - @cols = cols - end - - # helper to return the length of the longest word in the wordlist - def maxlen(wordlist) - max = 1 - for word in wordlist - if word.length > max - max = word.length - end - end - max - end - - # process the wordlist and print it formmated into columns - def output(wordlist) - collen = maxlen(wordlist) + 1 - columns = @cols / collen - columns = 1 if columns == 0 - rows = (wordlist.length + columns - 1) / columns - # now process each item, picking out proper piece for this position - 0.upto(rows * columns - 1) { |item| - target = (item % columns) * rows + (item / columns) - eol = ((item+1) % columns == 0) - piece = wordlist[target] || "" - piece = piece.ljust(collen) unless eol - print piece - puts if eol - } - # no need to finish it up, because eol is always true for the last element - end -end - -# get nr of chars that fit in window or console, see PLEAC 15.4 -# not portable -- linux only (?) -def getWinCharWidth() - buf = "\0" * 8 - $stdout.ioctl(0x5413, buf) - ws_row, ws_col, ws_xpixel, ws_ypixel = buf.unpack("$4") - ws_col || 80 -rescue - 80 -end - -# main program -cols = getWinCharWidth() -formatter = WordFormatter.new(cols) -words = readlines() -words.collect! { |line| - line.chomp -} -formatter.output(words) - - -# @@PLEAC@@_4.19 -# In ruby, Fixnum's are automatically converted to Bignum's when -# needed, so there is no need for an extra module -def factorial(n) - s = 1 - while n > 0 - s *= n - n -= 1 - end - s -end - -puts factorial(500) - -#--------------------------------------------------------- -# Example 4-3. tsc-permute -# tsc_permute: permute each word of input -def permute(items, perms) - unless items.length > 0 - puts perms.join(" ") - else - for i in items - newitems = items.dup - newperms = perms.dup - newperms.unshift(newitems.delete(i)) - permute(newitems, newperms) - end - end -end -# In ruby the main program must be after all definitions it is using -permute(ARGV, []) - -#--------------------------------------------------------- -# mjd_permute: permute each word of input - -def factorial(n) - s = 1 - while n > 0 - s *= n - n -= 1 - end - s -end - -# we use a class with a class variable store the private cache -# for the results of the factorial function. -class Factorial - @@fact = [ 1 ] - def Factorial.compute(n) - if @@fact[n] - @@fact[n] - else - @@fact[n] = n * Factorial.compute(n - 1) - end - end -end - -#--------------------------------------------------------- -# Example 4-4- mjd-permute -# n2pat(n, len): produce the N-th pattern of length len - -# We must use a lower case letter as parameter N, otherwise it is -# handled as constant Length is the length of the resulting -# array, not the index of the last element (length -1) like in -# the perl example. -def n2pat(n, length) - pat = [] - i = 1 - while i <= length - pat.push(n % i) - n /= i - i += 1 - end - pat -end - -# pat2perm(pat): turn pattern returned by n2pat() into -# permutation of integers. -def pat2perm(pat) - source = (0 .. pat.length - 1).to_a - perm = [] - perm.push(source.slice!(pat.pop)) while pat.length > 0 - perm -end - -def n2perm(n, len) - pat2perm(n2pat(n,len)) -end - -# In ruby the main program must be after all definitions -while gets - data = split - # the perl solution has used $#data, which is length-1 - num_permutations = Factorial.compute(data.length()) - 0.upto(num_permutations - 1) do |i| - # in ruby we can not use an array as selector for an array - # but by exchanging the two arrays, we can use the collect method - # which returns an array with the result of all block invocations - permutation = n2perm(i, data.length).collect { - |j| data[j] - } - puts permutation.join(" ") - end -end - - -# @@PLEAC@@_5.0 -age = { "Nat", 24, - "Jules", 25, - "Josh", 17 } - -age["Nat"] = 24 -age["Jules"] = 25 -age["Josh"] = 17 - -food_color = { - "Apple" => "red", - "Banana" => "yellow", - "Lemon" => "yellow", - "Carrot" => "orange" - } - -# In Ruby, you cannot avoid the double or simple quoting -# while manipulatin hashes - - -# @@PLEAC@@_5.1 -hash[key] = value - -food_color["Raspberry"] = "pink" -puts "Known foods:", food_color.keys - - -# @@PLEAC@@_5.2 -# does hash have a value for key ? -if (hash.has_key?(key)) - # it exists -else - # it doesn't -end - -[ "Banana", "Martini" ].each { |name| - print name, " is a ", food_color.has_key?(name) ? "food" : "drink", "\n" -} - -age = {} -age['Toddler'] = 3 -age['Unborn'] = 0 -age['Phantasm'] = nil - -for thing in ['Toddler', 'Unborn', 'Phantasm', 'Relic'] - print "#{thing}: " - print "Has-key " if age.has_key?(thing) - print "True " if age[thing] - print "Nonzero " if age[thing] && age[thing].nonzero? - print "\n" -end - -#=> -# Toddler: Has-key True Nonzero -# Unborn: Has-key True -# Phantasm: Has-key -# Relic: - -# You use Hash#has_key? when you use Perl's exists -> it checks -# for existence of a key in a hash. -# All Numeric are "True" in ruby, so the test doesn't have the -# same semantics as in Perl; you would use Numeric#nonzero? to -# achieve the same semantics (false if 0, true otherwise). - - -# @@PLEAC@@_5.3 -food_color.delete("Banana") - - -# @@PLEAC@@_5.4 -hash.each { |key, value| - # do something with key and value -} - -hash.each_key { |key| - # do something with key -} - -food_color.each { |food, color| - puts "#{food} is #{color}" -} - -food_color.each_key { |food| - puts "#{food} is #{food_color[food]}" -} - -# IMO this demonstrates that OO style is by far more readable -food_color.keys.sort.each { |food| - puts "#{food} is #{food_color[food]}." -} - -#----------------------------- -#!/usr/bin/ruby -# countfrom - count number of messages from each sender - -# Default value is 0 -from = Hash.new(0) -while gets - /^From: (.*)/ and from[$1] += 1 -end - -# More useful to sort by number of received mail by person -from.sort {|a,b| b[1]<=>a[1]}.each { |v| - puts "#{v[1]}: #{v[0]}" -} -#----------------------------- - - -# @@PLEAC@@_5.5 -# You may use the built-in 'inspect' method this way: -p hash - -# Or do it the Cookbook way: -hash.each { |k,v| puts "#{k} => #{v}" } - -# Sorted by keys -hash.sort.each { |e| puts "#{e[0]} => #{e[1]}" } -# Sorted by values -hash.sort{|a,b| a[1]<=>b[1]}.each { |e| puts "#{e[0]} => #{e[1]}" } - - -# @@PLEAC@@_5.7 -ttys = Hash.new -for i in `who` - user, tty = i.split - (ttys[user] ||= []) << tty # see problems_ruby for more infos -end -ttys.keys.sort.each { |k| - puts "#{k}: #{commify_series(ttys[k])}" # from 4.2 -} - - -# @@PLEAC@@_5.8 -surname = { "Mickey" => "Mantle", "Babe" => "Ruth" } -puts surname.index("Mantle") - -# If you really needed to 'invert' the whole hash, use Hash#invert - -#----------------------------- -#!/usr/bin/ruby -w -# foodfind - find match for food or color - -given = ARGV.shift or raise "usage: foodfind food_or_color" - -color = { - "Apple" => "red", - "Banana" => "yellow", - "Lemon" => "yellow", - "Carrot" => "orange", -} - -if (color.has_key?(given)) - puts "#{given} is a food with color #{color[given]}." -end -if (color.has_value?(given)) - puts "#{color.index(given)} is a food with color #{given}." -end -#----------------------------- - - -# @@PLEAC@@_5.9 -# Sorted by keys (Hash#sort gives an Array of pairs made of each key,value) -food_color.sort.each { |f| - puts "#{f[0]} is #{f[1]}." -} - -# Sorted by values -food_color.sort { |a,b| a[1] <=> b[1] }.each { |f| - puts "#{f[0]} is #{f[1]}." -} - -# Sorted by length of values -food_color.sort { |a,b| a[1].length <=> b[1].length }.each { |f| - puts "#{f[0]} is #{f[1]}." -} - - -# @@PLEAC@@_5.10 -merged = a.clone.update(b) # because Hash#update changes object in place - -drink_color = { "Galliano" => "yellow", "Mai Tai" => "blue" } -ingested_color = drink_color.clone.update(food_color) - -substance_color = {} -for i in [ food_color, drink_color ] - i.each_key { |k| - if substance_color.has_key?(k) - puts "Warning: #{k} seen twice. Using the first definition." - next - end - substance_color[k] = 1 - } -end - - -# @@PLEAC@@_5.11 -common = hash1.keys & hash2.keys - -this_not_that = hash1.keys - hash2.keys - - -# @@PLEAC@@_5.12 -# no problem here, Ruby handles any kind of object for key-ing -# (it takes Object#hash, which defaults to Object#id) - - -# @@PLEAC@@_5.13 -# AFAIK, not possible in Ruby - - -# @@PLEAC@@_5.14 -# Be careful, the following is possible only because Fixnum objects are -# special (documentation says: there is effectively only one Fixnum object -# instance for any given integer value). -count = Hash.new(0) -array.each { |e| - count[e] += 1 -} - - -# @@PLEAC@@_5.15 -father = { - "Cain" , "Adam", - "Abel" , "Adam", - "Seth" , "Adam", - "Enoch" , "Cain", - "Irad" , "Enoch", - "Mehujael" , "Irad", - "Methusael" , "Mehujael", - "Lamech" , "Methusael", - "Jabal" , "Lamech", - "Jubal" , "Lamech", - "Tubalcain" , "Lamech", - "Enos" , "Seth", -} - -while gets - chomp - begin - print $_, " " - end while $_ = father[$_] - puts -end - -children = {} -father.each { |k,v| - (children[v] ||= []) << k -} -while gets - chomp - puts "#{$_} begat #{(children[$_] || ['Nobody']).join(', ')}.\n" -end - -includes = {} -files.each { |f| - begin - for l in IO.readlines(f) - next unless l =~ /^\s*#\s*include\s*<([^>]+)>/ - (includes[$1] ||= []) << f - end - rescue SystemCallError - $stderr.puts "#$! (skipping)" - end -} - -include_free = includes.values.flatten.uniq - includes.keys - - -# @@PLEAC@@_5.16 -# dutree - print sorted intented rendition of du output -#% dutree -#% dutree /usr -#% dutree -a -#% dutree -a /bin - -# The DuNode class collects all information about a directory, -# and provides some convenience methods -class DuNode - - attr_reader :name - attr_accessor :size - attr_accessor :kids - - def initialize(name) - @name = name - @kids = [] - @size = 0 - end - - # support for sorting nodes with side - def size_compare(node2) - @size <=> node2.size - end - - def basename - @name.sub(/.*\//, "") - end - - #returns substring before last "/", nil if not there - def parent - p = @name.sub(/\/[^\/]+$/,"") - if p == @name - nil - else - p - end - end - -end - -# The DuTree does the acdtual work of -# getting the input, parsing it, builging up a tree -# and format it for output -class Dutree - - attr_reader :topdir - - def initialize - @nodes = Hash.new - @dirsizes = Hash.new(0) - @kids = Hash.new([]) - end - - # get a node by name, create it if it does not exist yet - def get_create_node(name) - if @nodes.has_key?(name) - @nodes[name] - else - node = DuNode.new(name) - @nodes[name] = node - node - end - end - - # run du, read in input, save sizes and kids - # stores last directory read in instance variable topdir - def input(arguments) - name = "" - cmd = "du " + arguments.join(" ") - IO.popen(cmd) { |pipe| - pipe.each { |line| - size, name = line.chomp.split(/\s+/, 2) - node = get_create_node(name) - node.size = size.to_i - @nodes[name] = node - parent = node.parent - if parent - get_create_node(parent).kids.push(node) - end - } - } - @topdir = @nodes[name] - end - - # figure out how much is taken in each directory - # that isn't stored in the subdirectories. Add a new - # fake kid called "." containing that much. - def get_dots(node) - cursize = node.size - for kid in node.kids - cursize -= kid.size - get_dots(kid) - end - if node.size != cursize - newnode = get_create_node(node.name + "/.") - newnode.size = cursize - node.kids.push(newnode) - end - end - - # recursively output everything - # passing padding and number width as well - # on recursive calls - def output(node, prefix="", width=0) - line = sprintf("%#{width}d %s", node.size, node.basename) - puts(prefix + line) - prefix += line.sub(/\d /, "| ") - prefix.gsub!(/[^|]/, " ") - if node.kids.length > 0 # not a bachelor node - kids = node.kids - kids.sort! { |a,b| - b.size_compare(a) - } - width = kids[0].size.to_s.length - for kid in kids - output(kid, prefix, width) - end - end - end - -end - -tree = Dutree.new -tree.input(ARGV) -tree.get_dots(tree.topdir) -tree.output(tree.topdir) - - -# @@PLEAC@@_6.0 -# The verbose version are match, sub, gsub, sub! and gsub!; -# pattern needs to be a Regexp object; it yields a MatchData -# object. -pattern.match(string) -string.sub(pattern, replacement) -string.gsub(pattern, replacement) -# As usual in Ruby, sub! does the same as sub but also modifies -# the object, the same for gsub!/gsub. - -# Sugared syntax yields the position of the match (or nil if no -# match). Note that the object at the right of the operator needs -# not to be a Regexp object (it can be a String). The "dont -# match" operator yields true or false. -meadow =~ /sheep/ # position of the match, nil if no match -meadow !~ /sheep/ # true if doesn't match, false if it does -# There is no sugared version for the substitution - -meadow =~ /\bovines?\b/i and print "Here be sheep!" - -string = "good food" -string.sub!(/o*/, 'e') - -# % echo ababacaca | ruby -ne 'puts $& if /(a|ba|b)+(a|ac)+/' -# ababa - -# The "global" (or "multiple") match is handled by String#scan -scan (/(\d+)/) { - puts "Found number #{$1}" -} - -# String#scan yields an Array if not used with a block -numbers = scan(/\d+/) - -digits = "123456789" -nonlap = digits.scan(/(\d\d\d)/) -yeslap = digits.scan(/(?=(\d\d\d))/) -puts "Non-overlapping: #{nonlap.join(' ')}" -puts "Overlapping: #{yeslap.join(' ')}"; -# Non-overlapping: 123 456 789 -# Overlapping: 123 234 345 456 567 678 789 - -string = "And little lambs eat ivy" -string =~ /l[^s]*s/ -puts "(#$`) (#$&) (#$')" -# (And ) (little lambs) ( eat ivy) - - -# @@PLEAC@@_6.1 -# Ruby doesn't have the same problem: -dst = src.sub('this', 'that') - -progname = $0.sub('^.*/', '') - -bindirs = %w(/usr/bin /bin /usr/local/bin) -libdirs = bindirs.map { |l| l.sub('bin', 'lib') } - - -# @@PLEAC@@_6.3 -/\S+/ # as many non-whitespace bytes as possible -/[A-Za-z'-]+/ # as many letters, apostrophes, and hyphens - -/\b([A-Za-z]+)\b/ # usually best -/\s([A-Za-z]+)\s/ # fails at ends or w/ punctuation - - -# @@PLEAC@@_6.4 -require 'socket' -str = 'www.ruby-lang.org and www.rubygarden.org' -re = / - ( # capture the hostname in $1 - (?: # these parens for grouping only - (?! [-_] ) # lookahead for neither underscore nor dash - [\w-] + # hostname component - \. # and the domain dot - ) + # now repeat that whole thing a bunch of times - [A-Za-z] # next must be a letter - [\w-] + # now trailing domain part - ) # end of $1 capture - /x # /x for nice formatting - -str.gsub! re do # pass a block to execute replacement - host = TCPsocket.gethostbyname($1) - "#{$1} [#{host[3]}]" -end - -puts str -#----------------------------- -# to match whitespace or #-characters in an extended re you need to escape -# them. - -foo = 42 -str = 'blah #foo# blah' -str.gsub! %r/ # replace - \# # a pound sign - (\w+) # the variable name - \# # another pound sign - /x do - eval $1 # with the value of a local variable - end -puts str # => blah 42 blah - - -# @@PLEAC@@_6.5 -# The 'g' modifier doesn't exist in Ruby, a regexp can't be used -# directly in a while loop; instead, use String#scan { |match| .. } -fish = 'One fish two fish red fish blue fish' -WANT = 3 -count = 0 -fish.scan(/(\w+)\s+fish\b/i) { - if (count += 1) == WANT - puts "The third fish is a #{$1} one." - end -} - -if fish =~ /(?:\w+\s+fish\s+){2}(\w+)\s+fish/i - puts "The third fish is a #{$1} one." -end - -pond = 'One fish two fish red fish blue fish' -# String#scan without a block gives an array of matches, each match -# being an array of all the specified groups -colors = pond.scan(/(\w+)\s+fish\b/i).flatten # get all matches -color = colors[2] # then the one we want -# or without a temporary array -color = pond.scan(/(\w+)\s+fish\b/i).flatten[2] # just grab element 3 -puts "The third fish in the pond is #{color}." - -count = 0 -fishes = 'One fish two fish red fish blue fish' -evens = fishes.scan(/(\w+)\s+fish\b/i).select { (count+=1) % 2 == 0 } -print "Even numbered fish are #{evens.join(' ')}." - -count = 0 -fishes.gsub(/ - \b # makes next \w more efficient - ( \w+ ) # this is what we\'ll be changing - ( - \s+ fish \b - ) - /x) { - if (count += 1) == 4 - 'sushi' + $2 - else - $1 + $2 - end -} - -pond = 'One fish two fish red fish blue fish swim here.' -puts "Last fish is #{pond.scan(/\b(\w+)\s+fish\b/i).flatten[-1]}" - -/ - A # find some pattern A - (?! # mustn\'t be able to find - .* # something - A # and A - ) - $ # through the end of the string -/x - -# The "s" perl modifier is "m" in Ruby (not very nice since there is -# also an "m" in perl..) -pond = "One fish two fish red fish blue fish swim here." -if (pond =~ / - \b ( \w+) \s+ fish \b - (?! .* \b fish \b ) - /mix) - puts "Last fish is #{$1}." -else - puts "Failed!" -end - - -# @@PLEAC@@_6.6 -#----------------------------- -#!/usr/bin/ruby -w -# killtags - very bad html killer -$/ = nil; # each read is whole file -while file = gets() do - file.gsub!(/<.*?>/m,''); # strip tags (terribly) - puts file # print file to STDOUT -end -#----------------------------- -#!/usr/bin/ruby -w -#headerfy - change certain chapter headers to html -$/ = '' -while file = gets() do - pattern = / - \A # start of record - ( # capture in $1 - Chapter # text string - \s+ # mandatory whitespace - \d+ # decimal number - \s* # optional whitespace - : # a real colon - . * # anything not a newline till end of line - ) - /x - puts file.gsub(pattern,'

\1

') -end -#----------------------------- -#% ruby -00pe "gsub!(/\A(Chapter\s+\d+\s*:.*)/,'

\1

')" datafile - -#!/usr/bin/ruby -w -#----------------------------- -for file in ARGV - file = File.open(ARGV.shift) - while file.gets('') do # each read is a paragraph - print "chunk #{$.} in $ARGV has <<#{$1}>>\n" while /^START(.*?)^END/m - end # /m activates the multiline mode -end -#----------------------------- - -# @@PLEAC@@_6.7 -#----------------------------- -$/ = nil; -file = File.open("datafile") -chunks = file.gets.split(/pattern/) -#----------------------------- -# .Ch, .Se and .Ss divide chunks of STDIN -chunks = gets(nil).split(/^\.(Ch|Se|Ss)$/) -print "I read #{chunks.size} chunks.\n" -#----------------------------- - - -# @@PLEAC@@_6.8 -while gets - if ~/BEGIN/ .. ~/END/ - # line falls between BEGIN and END inclusive - end -end - -while gets - if ($. == firstnum) .. ($. == lastnum) - # operate between firstnum and lastnum line number - end -end - -# in ruby versions prior to 1.8, the above two conditional -# expressions could be shortened to: -# if /BEGIN/ .. /END/ -# and -# if firstnum .. lastnum -# but these now only work this way from the command line - -#----------------------------- - -while gets - if ~/BEGIN/ ... ~/END/ - # line falls between BEGIN and END on different lines - end -end - -while gets - if ($. == first) ... ($. == last) - # operate between first and last line number on different lines - end -end - -#----------------------------- -# command-line to print lines 15 through 17 inclusive (see below) -ruby -ne 'print if 15 .. 17' datafile - -# print out all .. displays from HTML doc -while gets - print if ~%r##i .. ~%r##i; -end - -# same, but as shell command -# ruby -ne 'print if %r##i .. %r##i' document.html -#----------------------------- -# ruby -ne 'BEGIN { $top=3; $bottom=5 }; \ -# print if $top .. $bottom' /etc/passwd # FAILS -# ruby -ne 'BEGIN { $top=3; $bottom=5 }; \ -# print if $. == $top .. $. == $bottom' /etc/passwd # works -# ruby -ne 'print if 3 .. 5' /etc/passwd # also works -#----------------------------- -print if ~/begin/ .. ~/end/; -print if ~/begin/ ... ~/end/; -#----------------------------- -while gets - $in_header = $. == 1 .. ~/^$/ ? true : false - $in_body = ~/^$/ .. ARGF.eof ? true : false -end -#----------------------------- -seen = {} -ARGF.each do |line| - next unless line =~ /^From:?\s/i .. line =~ /^$/; - line.scan(%r/([^<>(),;\s]+\@[^<>(),;\s]+)/).each do |addr| - puts addr unless seen[addr] - seen[addr] ||= 1 - end -end - - -# @@PLEAC@@_6.9 -def glob2pat(globstr) - patmap = { - '*' => '.*', - '?' => '.', - '[' => '[', - ']' => ']', - } - globstr.gsub!(/(.)/) { |c| patmap[c] || Regexp::escape(c) } - '^' + globstr + '$' -end - - -# @@PLEAC@@_6.10 -# avoid interpolating patterns like this if the pattern -# isn't going to change: -pattern = ARGV.shift -ARGF.each do |line| - print line if line =~ /#{pattern}/ -end - -# the above creates a new regex each iteration. Instead, -# use the /o modifier so the regex is compiled only once - -pattern = ARGV.shift -ARGF.each do |line| - print line if line =~ /#{pattern}/o -end - -#----------------------------- - -#!/usr/bin/ruby -# popgrep1 - grep for abbreviations of places that say "pop" -# version 1: slow but obvious way -popstates = %w(CO ON MI WI MN) -ARGF.each do |line| - popstates.each do |state| - if line =~ /\b#{state}\b/ - print line - last - end - end -end - -#----------------------------- -#!/usr/bin/ruby -# popgrep2 - grep for abbreviations of places that say "pop" -# version 2: eval strings; fast but hard to quote -popstates = %w(CO ON MI WI MN) -code = "ARGF.each do |line|\n" -popstates.each do |state| - code += "\tif line =~ /\\b#{state}\\b/; print(line); next; end\n" -end -code += "end\n" -print "CODE IS\n---\n#{code}\n---\n" if false # turn on for debugging -eval code - -# CODE IS -# --- -# ARGF.each do |line| -# if line =~ /\bCO\b/; print(line); next; end -# if line =~ /\bON\b/; print(line); next; end -# if line =~ /\bMI\b/; print(line); next; end -# if line =~ /\bWI\b/; print(line); next; end -# if line =~ /\bMN\b/; print(line); next; end -# end -# -# --- - -## alternatively, the same idea as above but compiling -## to a case statement: (not in perlcookbook) -#!/usr/bin/ruby -w -# popgrep2.5 - grep for abbreviations of places that say "pop" -# version 2.5: eval strings; fast but hard to quote -popstates = %w(CO ON MI WI MN) -code = "ARGF.each do |line|\n case line\n" -popstates.each do |state| - code += " when /\\b#{state}\\b/ : print line\n" -end -code += " end\nend\n" -print "CODE IS\n---\n#{code}\n---\n" if false # turn on for debugging -eval code - -# CODE IS -# --- -# ARGF.each do |line| -# case line -# when /\bCO\b/ : print line -# when /\bON\b/ : print line -# when /\bMI\b/ : print line -# when /\bWI\b/ : print line -# when /\bMN\b/ : print line -# end -# end -# -# --- - -# Note: (above) Ruby 1.8+ allows the 'when EXP : EXPR' on one line -# with the colon separator. - -#----------------------------- -#!/usr/bin/ruby -# popgrep3 - grep for abbreviations of places that say "pop" -# version3: build a match_any function -popstates = %w(CO ON MI WI MN) -expr = popstates.map{|e|"line =~ /\\b#{e}\\b/"}.join('||') -eval "def match_any(line); #{expr};end" -ARGF.each do |line| - print line if match_any(line) -end -#----------------------------- - -## building a match_all function is a trivial -## substitution of && for || -## here is a generalized example: -#!/usr/bin/ruby -w -## grepauth - print lines that mention both foo and bar -class MultiMatch - def initialize(*patterns) - _any = build_match('||',patterns) - _all = build_match('&&',patterns) - eval "def match_any(line);#{_any};end\n" - eval "def match_all(line);#{_all};end\n" - end - def build_match(sym,args) - args.map{|e|"line =~ /#{e}/"}.join(sym) - end -end - -mm = MultiMatch.new('foo','bar') -ARGF.each do |line| - print line if mm.match_all(line) -end -#----------------------------- - -#!/usr/bin/ruby -# popgrep4 - grep for abbreviations of places that say "pop" -# version4: pretty fast, but simple: compile all re's first: -popstates = %w(CO ON MI WI MN) -popstates = popstates.map{|re| %r/\b#{re}\b/} -ARGF.each do |line| - popstates.each do |state_re| - if line =~ state_re - print line - break - end - end -end - -## speeds trials on the jargon file(412): 26006 lines, 1.3MB -## popgrep1 => 7.040s -## popgrep2 => 0.656s -## popgrep2.5 => 0.633s -## popgrep3 => 0.675s -## popgrep4 => 1.027s - -# unless speed is criticial, the technique in popgrep4 is a -# reasonable balance between speed and logical simplicity. - - -# @@PLEAC@@_6.11 -begin - print "Pattern? " - pat = $stdin.gets.chomp - Regexp.new(pat) -rescue - warn "Invalid Pattern" - retry -end - - -# @@PLEAC@@_6.13 -# uses the 'amatch' extension found on: -# http://raa.ruby-lang.org/project/amatch/ -require 'amatch' -matcher = Amatch.new('balast') -#$relative, $distance = 0, 1 -File.open('/usr/share/dict/words').each_line do |line| - print line if matcher.search(line) <= 1 -end -__END__ -#CODE -ballast -ballasts -balustrade -balustrades -blast -blasted -blaster -blasters -blasting -blasts - - -# @@PLEAC@@_6.14 -str.scan(/\G(\d)/).each do |token| - puts "found #{token}" -end -#----------------------------- -n = " 49 here" -n.gsub!(/\G /,'0') -puts n -#----------------------------- -str = "3,4,5,9,120" -str.scan(/\G,?(\d+)/).each do |num| - puts "Found number: #{num}" -end -#----------------------------- -# Ruby doesn't have the String.pos or a /c re modifier like Perl -# But it does have StringScanner in the standard library (strscn) -# which allows similar functionality: - -require 'strscan' -text = 'the year 1752 lost 10 days on the 3rd of September' -sc = StringScanner.new(text) -while sc.scan(/.*?(\d+)/) - print "found: #{sc[1]}\n" -end -if sc.scan(/\S+/) - puts "Found #{sc[0]} after last number" -end -#----------------------------- -# assuming continuing from above: -puts "The position in 'text' is: #{sc.pos}" -sc.pos = 30 -puts "The position in 'text' is: #{sc.pos}" - - -# @@PLEAC@@_6.15 -#----------------------------- -# greedy pattern -str.gsub!(/<.*>/m,'') # not good - -# non-greedy (minimal) pattern -str.gsub!(/<.*?>/m,'') # not great - - -#----------------------------- -#this and that are important Oh, me too! -#----------------------------- -%r{ (.*?) }mx -#----------------------------- -%r/BEGIN((?:(?!BEGIN).)*)END/ -#----------------------------- -%r{ ( (?: (?!|). )* ) }mx -#----------------------------- -%r{ ( (?: (?!). )* ) }mx -#----------------------------- -%r{ - - [^<]* # stuff not possibly bad, and not possibly the end. - (?: - # at this point, we can have '<' if not part of something bad - (?! ) # what we can't have - < # okay, so match the '<' - [^<]* # and continue with more safe stuff - ) * - - }mx - - -# @@PLEAC@@_6.16 -#----------------------------- -$/ = "" -ARGF.each do |para| - para.scan %r/ - \b # start at word boundary - (\S+) # find chunk of non-whitespace - \b # until a word boundary - ( - \s+ # followed by whitespace - \1 # and that same chunk again - \b # and a word boundary - ) + # one or more times - /xi do - puts "dup word '#{$1}' at paragraph #{$.}" - end -end -#----------------------------- -astr = 'nobody' -bstr = 'bodysnatcher' -if "#{astr} #{bstr}" =~ /^(\w+)(\w+) \2(\w+)$/ - print "#{$2} overlaps in #{$1}-#{$2}-#{$3}" -end -#----------------------------- -#!/usr/bin/ruby -w -# prime_pattern -- find prime factors of argument using patterns -ARGV << 180 -cap = 'o' * ARGV.shift -while cap =~ /^(oo+?)\1+$/ - print $1.size, " " - cap.gsub!(/#{$1}/,'o') -end -puts cap.size -#----------------------------- -#diophantine -# solve for 12x + 15y + 16z = 281, maximizing x -if ('o' * 281).match(/^(o*)\1{11}(o*)\2{14}(o*)\3{15}$/) - x, y, z = $1.size, $2.size, $3.size - puts "One solution is: x=#{x}; y=#{y}; z=#{z}" -else - puts "No solution." -end -# => One solution is: x=17; y=3; z=2 - -#----------------------------- -# using different quantifiers: -('o' * 281).match(/^(o+)\1{11}(o+)\2{14}(o+)\3{15}$/) -# => One solution is: x=17; y=3; z=2 - -('o' * 281).match(/^(o*?)\1{11}(o*)\2{14}(o*)\3{15}$/) -# => One solution is: x=0; y=7; z=11 - -('o' * 281).match(/^(o+?)\1{11}(o*)\2{14}(o*)\3{15}$/) -# => One solution is: x=1; y=3; z=14 - - -# @@PLEAC@@_6.17 -# alpha OR beta -%r/alpha|beta/ - -# alpha AND beta -%r/(?=.*alpha)(?=.*beta)/m - -# alpha AND beta, no overlap -%r/alpha.*beta|beta.*alpha/m - -# NOT beta -%r/^(?:(?!beta).)*$/m - -# NOT bad BUT good -%r/(?=(?:(?!BAD).)*$)GOOD/m -#----------------------------- - -if !(string =~ /pattern/) # ugly - something() -end - -if string !~ /pattern/ # preferred - something() -end - - -#----------------------------- -if string =~ /pat1/ && string =~ /pat2/ - something() -end -#----------------------------- -if string =~ /pat1/ || string =~ /pat2/ - something() -end -#----------------------------- -#!/usr/bin/ruby -w -# minigrep - trivial grep -pat = ARGV.shift -ARGF.each do |line| - print line if line =~ /#{pat}/o -end -#----------------------------- - "labelled" =~ /^(?=.*bell)(?=.*lab)/m -#----------------------------- -$string =~ /bell/ && $string =~ /lab/ -#----------------------------- -$murray_hill = "blah bell blah " -if $murray_hill =~ %r{ - ^ # start of string - (?= # zero-width lookahead - .* # any amount of intervening stuff - bell # the desired bell string - ) # rewind, since we were only looking - (?= # and do the same thing - .* # any amount of intervening stuff - lab # and the lab part - ) - }mx # /m means . can match newline - - print "Looks like Bell Labs might be in Murray Hill!\n"; -end -#----------------------------- -"labelled" =~ /(?:^.*bell.*lab)|(?:^.*lab.*bell)/ -#----------------------------- -$brand = "labelled"; -if $brand =~ %r{ - (?: # non-capturing grouper - ^ .*? # any amount of stuff at the front - bell # look for a bell - .*? # followed by any amount of anything - lab # look for a lab - ) # end grouper - | # otherwise, try the other direction - (?: # non-capturing grouper - ^ .*? # any amount of stuff at the front - lab # look for a lab - .*? # followed by any amount of anything - bell # followed by a bell - ) # end grouper - }mx # /m means . can match newline - print "Our brand has bell and lab separate.\n"; -end -#----------------------------- -$map =~ /^(?:(?!waldo).)*$/s -#----------------------------- -$map = "the great baldo" -if $map =~ %r{ - ^ # start of string - (?: # non-capturing grouper - (?! # look ahead negation - waldo # is he ahead of us now? - ) # is so, the negation failed - . # any character (cuzza /s) - ) * # repeat that grouping 0 or more - $ # through the end of the string - }mx # /m means . can match newline - print "There's no waldo here!\n"; -end -=begin - 7:15am up 206 days, 13:30, 4 users, load average: 1.04, 1.07, 1.04 - -USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT - -tchrist tty1 5:16pm 36days 24:43 0.03s xinit - -tchrist tty2 5:19pm 6days 0.43s 0.43s -tcsh - -tchrist ttyp0 chthon 7:58am 3days 23.44s 0.44s -tcsh - -gnat ttyS4 coprolith 2:01pm 13:36m 0.30s 0.30s -tcsh -=end -#% w | minigrep '^(?!.*ttyp).*tchrist' -#----------------------------- -%r{ - ^ # anchored to the start - (?! # zero-width look-ahead assertion - .* # any amount of anything (faster than .*?) - ttyp # the string you don't want to find - ) # end look-ahead negation; rewind to start - .* # any amount of anything (faster than .*?) - tchrist # now try to find Tom -}x -#----------------------------- -#% w | grep tchrist | grep -v ttyp -#----------------------------- -#% grep -i 'pattern' files -#% minigrep '(?i)pattern' files -#----------------------------- - - -# @@PLEAC@@_6.20 -ans = $stdin.gets.chomp -re = %r/^#{Regexp.quote(ans)}/ -case - when "SEND" =~ re : puts "Action is send" - when "STOP" =~ re : puts "Action is stop" - when "ABORT" =~ re : puts "Action is abort" - when "EDIT" =~ re : puts "Action is edit" -end -#----------------------------- -require 'abbrev' -table = Abbrev.abbrev %w-send stop abort edit- -loop do - print "Action: " - ans = $stdin.gets.chomp - puts "Action for #{ans} is #{table[ans.downcase]}" -end - - -#----------------------------- -# dummy values are defined for 'file', 'PAGER', and -# the 'invoke_editor' and 'deliver_message' methods -# do not do anything interesting in this example. -#!/usr/bin/ruby -w -require 'abbrev' - -file = 'pleac_ruby.data' -PAGER = 'less' - -def invoke_editor - puts "invoking editor" -end - -def deliver_message - puts "delivering message" -end - -actions = { - 'edit' => self.method(:invoke_editor), - 'send' => self.method(:deliver_message), - 'list' => proc {system(PAGER, file)}, - 'abort' => proc {puts "See ya!"; exit}, - "" => proc {puts "Unknown Command"} -} - -dtable = Abbrev.abbrev(actions.keys) -loop do - print "Action: " - ans = $stdin.gets.chomp.delete(" \t") - actions[ dtable[ans.downcase] || "" ].call -end - - -# @@PLEAC@@_6.19 -#----------------------------- -# basically, the Perl Cookbook categorizes this as an -# unsolvable problem ... -#----------------------------- -1 while addr.gsub!(/\([^()]*\)/,'') -#----------------------------- -Dear someuser@host.com, - -Please confirm the mail address you gave us Wed May 6 09:38:41 -MDT 1998 by replying to this message. Include the string -"Rumpelstiltskin" in that reply, but spelled in reverse; that is, -start with "Nik...". Once this is done, your confirmed address will -be entered into our records. - - -# @@PLEAC@@_6.21 -#----------------------------- -#% gunzip -c ~/mail/archive.gz | urlify > archive.urlified -#----------------------------- -#% urlify ~/mail/*.inbox > ~/allmail.urlified -#----------------------------- -#!/usr/bin/ruby -w -# urlify - wrap HTML links around URL-like constructs - -urls = '(https?|telnet|gopher|file|wais|ftp)'; -ltrs = '\w'; -gunk = '/#~:.?+=&%@!\-'; -punc = '.:?\-'; -any = "#{ltrs}#{gunk}#{punc}"; - -ARGF.each do |line| - line.gsub! %r/ - \b # start at word boundary - ( # begin $1 { - #{urls} : # need resource and a colon - [#{any}] +? # followed by on or more - # of any valid character, but - # be conservative and take only - # what you need to.... - ) # end $1 } - (?= # look-ahead non-consumptive assertion - [#{punc}]* # either 0 or more punctuation - [^#{any}] # followed by a non-url char - | # or else - $ # then end of the string - ) - /iox do - %Q|#{$1}| - end - print line -end - - -# @@PLEAC@@_6.23 -%r/^m*(d?c{0,3}|c[dm])(l?x{0,3}|x[lc])(v?i{0,3}|i[vx])$/i -#----------------------------- -str.sub!(/(\S+)(\s+)(\S+)/, '\3\2\1') -#----------------------------- -%r/(\w+)\s*=\s*(.*)\s*$/ # keyword is $1, value is $2 -#----------------------------- -%r/.{80,}/ -#----------------------------- -%r|(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)| -#----------------------------- -str.gsub!(%r|/usr/bin|,'/usr/local/bin') -#----------------------------- -str.gsub!(/%([0-9A-Fa-f][0-9A-Fa-f])/){ $1.hex.chr } -#----------------------------- -str.gsub!(%r{ - /\* # Match the opening delimiter - .*? # Match a minimal number of characters - \*/ # Match the closing delimiter -}xm,'') -#----------------------------- -str.sub!(/^\s+/, '') -str.sub!(/\s+$/, '') - -# but really, in Ruby we'd just do: -str.strip! -#----------------------------- -str.gsub!(/\\n/,"\n") -#----------------------------- -str.sub!(/^.*::/, '') -#----------------------------- -%r/^([01]?\d\d|2[0-4]\d|25[0-5])\.([01]?\d\d|2[0-4]\d|25[0-5])\. - ([01]?\d\d|2[0-4]\d|25[0-5])\.([01]?\d\d|2[0-4]\d|25[0-5])$/x -#----------------------------- -str.sub!(%r|^.*/|, '') -#----------------------------- -cols = ( (ENV['TERMCAP'] || " ") =~ /:co#(\d+):/ ) ? $1 : 80; -#----------------------------- -name = " #{$0} #{ARGV}".gsub(%r| /\S+/|, ' ') -#----------------------------- -require 'rbconfig' -include Config -raise "This isn't Linux" unless CONFIG['target_os'] =~ /linux/i; -#----------------------------- -str.gsub!(%r/\n\s+/, ' ') -#----------------------------- -nums = str.scan(/(\d+\.?\d*|\.\d+)/) -#----------------------------- -capwords = str.scan(%r/(\b[^\Wa-z0-9_]+\b)/) -#----------------------------- -lowords = str.scan(%r/(\b[^\WA-Z0-9_]+\b)/) -#----------------------------- -icwords = str.scan(%r/(\b[^\Wa-z0-9_][^\WA-Z0-9_]*\b)/) -#----------------------------- -links = str.scan(%r/]+?HREF\s*=\s*["']?([^'" >]+?)[ '"]?>/mi) -#----------------------------- -initial = str =~ /^\S+\s+(\S)\S*\s+\S/ ? $1 : "" -#----------------------------- -str.gsub!(%r/"([^"]*)"/, %q-``\1''-) -#----------------------------- - -$/ = "" -sentences = [] -ARGF.each do |para| - para.gsub!(/\n/, ' ') - para.gsub!(/ {3,}/,' ') - sentences << para.scan(/(\S.*?[!?.])(?= |\Z)/) -end - -#----------------------------- -%r/(\d{4})-(\d\d)-(\d\d)/ # YYYY in $1, MM in $2, DD in $3 -#----------------------------- -%r/ ^ - (?: - 1 \s (?: \d\d\d \s)? # 1, or 1 and area code - | # ... or ... - \(\d\d\d\) \s # area code with parens - | # ... or ... - (?: \+\d\d?\d? \s)? # optional +country code - \d\d\d ([\s\-]) # and area code - ) - \d\d\d (\s|\1) # prefix (and area code separator) - \d\d\d\d # exchange - $ - /x -#----------------------------- -%r/\boh\s+my\s+gh?o(d(dess(es)?|s?)|odness|sh)\b/i -#----------------------------- -lines = [] -lines << $1 while input.sub!(/^([^\012\015]*)(\012\015?|\015\012?)/,'') - - -# @@PLEAC@@_7.0 -# An IO object being Enumerable, we can use 'each' directly on it -File.open("/usr/local/widgets/data").each { |line| - puts line if line =~ /blue/ -} - -logfile = File.new("/var/log/rubylog.txt", "w") -mysub($stdin, logfile) - -# The method IO#readline is similar to IO#gets -# but throws an exception when it reaches EOF -f = File.new("bla.txt") -begin - while (line = f.readline) - line.chomp - $stdout.print line if line =~ /blue/ - end -rescue EOFError - f.close -end - -while $stdin.gets # reads from STDIN - unless (/\d/) - $stderr.puts "No digit found." # writes to STDERR - end - puts "Read: #{$_}" # writes to STDOUT -end - -logfile = File.new("/tmp/log", "w") - -logfile.close - -# $defout (or its synonym '$>') is the destination of output -# for Kernel#print, Kernel#puts, and family functions -logfile = File.new("log.txt", "w") -old = $defout -$defout = logfile # switch to logfile for output -puts "Countdown initiated ..." -$defout = old # return to original output -puts "You have 30 seconds to reach minimum safety distance." - - -# @@PLEAC@@_7.1 -source = File.new(path, "r") # open file "path" for reading only -sink = File.new(path, "w") # open file "path" for writing only - -source = File.open(path, File::RDONLY) # open file "path" for reading only -sink = File.open(path, File::WRONLY) # open file "path" for writing only - -file = File.open(path, "r+") # open "path" for reading and writing -file = File.open(path, flags) # open "path" with the flags "flags" (see examples below for flags) - -# open file "path" read only -file = File.open(path, "r") -file = File.open(path, File::RDONLY) - -# open file "path" write only, create it if it does not exist -# truncate it to zero length if it exists -file = File.open(path, "w") -file = File.open(path, File::WRONLY|File::TRUNC|File::CREAT) -file = File.open(path, File::WRONLY|File::TRUNC|File::CREAT, 0666) # with permission 0666 - -# open file "path" write only, fails if file exists -file = File.open(path, File::WRONLY|File::EXCL|File::CREAT) -file = File.open(path, File::WRONLY|File::EXCL|File::CREAT, 0666) - -# open file "path" for appending -file = File.open(path, "a") -file = File.open(path, File::WRONLY|File::APPEND|File::CREAT) -file = File.open(path, File::WRONLY|File::APPEND|File::CREAT, 0666) - -# open file "path" for appending only when file exists -file = File.open(path, File::WRONLY|File::APPEND) - -# open file "path" for reading and writing -file = File.open(path, "r+") -file = File.open(path, File::RDWR) - -# open file for reading and writing, create a new file if it does not exist -file = File.open(path, File::RDWR|File::CREAT) -file = File.open(path, File::RDWR|File::CREAT, 0600) - -# open file "path" reading and writing, fails if file exists -file = File.open(path, File::RDWR|File::EXCL|File::CREAT) -file = File.open(path, File::RDWR|File::EXCL|File::CREAT, 0600) - - -# @@PLEAC@@_7.2 -# No problem with Ruby since the filename doesn't contain characters with -# special meaning; like Perl's sysopen -File.open(filename, 'r') - - -# @@PLEAC@@_7.3 -File.expand_path('~root/tmp') -#=> "/root/tmp" -File.expand_path('~rpcuser') -#=> "/var/lib/nfs" - -# To expand ~/.. it explicitely needs the environment variable HOME -File.expand_path('~/tmp') -#=> "/home/gc/tmp" - - -# @@PLEAC@@_7.4 -# The exception raised in Ruby reports the filename -File.open('afile') - - -# @@PLEAC@@_7.5 -# Standard Ruby distribution provides the following useful extension -require 'tempfile' -# With the Tempfile class, the file is automatically deleted on garbage -# collection, so you won't need to remove it, later on. -tf = Tempfile.new('tmp') # a name is required to create the filename - -# If you need to pass the filename to an external program you can use -# File#path, but don't forget to File#flush in order to flush anything -# living in some buffer somewhere. -tf.flush -system("/usr/bin/dowhatever #{tf.path}") - -fh = Tempfile.new('tmp') -fh.sync = true # autoflushes -10.times { |i| fh.puts i } -fh.rewind -puts 'Tmp file has: ', fh.readlines - - -# @@PLEAC@@_7.6 -while (DATA.gets) do - # process the line -end -__END__ -# your data goes here -# __DATA__ doesn't exist in Ruby - -#CODE -# get info about the script (size, date of last modification) -kilosize = DATA.stat.size / 1024 -last_modif = DATA.stat.mtime -puts "

Script size is #{kilosize}" -puts "

Last script update: #{last_modif}" -__END__ -# DO NOT REMOVE THE PRECEEDING LINE. -# Everything else in this file will be ignored. -#CODE - - -# @@PLEAC@@_7.7 -while line = gets do - # do something with line. -end - -# or -while gets do - # do something with $_ -end - -# or more rubyish -$stdun.each do |line| - # do stuff with line -end - - -# ARGF may makes this more easy -# this is skipped if ARGV.size==0 -ARGV.each do |filename| - # closing and exception handling are done by the block - open(filename) do |fd| - fd.each do |line| - # do stuff with line - end - end rescue abort("can't open %s" % filename) -end - -# globbing is done in the Dir module -ARGV = Dir["*.[Cch]"] if ARGV.empty? - -# note: optparse is the preferred way to handle this -if (ARGV[0] == '-c') - chop_first += 1 - ARGV.shift -end - - -# processing numerical options -if ARGV[0] =~ /^-(\d+)$/ - columns = $1 - ARGV.shift -end - -# again, better to use optparse: -require 'optparse' -nostdout = 0 -append = 0 -unbuffer = 0 -ignore_ints = 0 -ARGV.options do |opt| - opt.on('-n') { nostdout +=1 } - opt.on('-a') { append +=1 } - opt.on('-u') { unbuffer +=1 } - opt.on('-i') { ignore_ints +=1 } - opt.parse! -end or abort("usage: " + __FILE__ + " [-ainu] [filenames]") - -# no need to do undef $/, we have File.read -str = File.read(ARGV[0]) - -# again we have File.read -str = File.read(ARGV[0]) - -# not sure what this should do: -# I believe open the file, print filename, lineno and line: -ARGF.each_with_index do |line, idx| - print ARGF.filename, ":", idx, ";", line -end - -# print all the lines in every file passed via command line that contains login -ARGF.each do |line| - puts line if line =~ /login/ -end -# -# even this would fit -#%ruby -ne "print if /f/" 2.log -# - -ARGF.each { |l| puts l.downcase! } - -#------------------ -#!/usr/bin/ruby -p -# just like perl's -p -$_.downcase! -# - -# I don't know who should I trust. -# perl's version splits on \w+ while python's on \w. - -chunks = 0 - -File.read(ARGV[0]).split.each do |word| - next if word =~ /^#/ - break if ["__DATA__", "__END__"].member? word - chunks += 1 -end - -print "Found ", chunks, " chunks\n" - - -# @@PLEAC@@_7.8 -old = File.open(old_file) -new = File.open(new_file, "w") -while old.gets do - # change $_, then... - new.print $_ -end -old.close -new.close -File.rename(old_file, "old.orig") -File.rename(new_file, old_file) - -while old.gets do - if $. == 20 then # we are at the 20th line - new.puts "Extra line 1" - new.puts "Extra line 2" - end - new.print $_ -end - -while old.gets do - next if 20..30 # skip the 20th line to the 30th - # Ruby (and Perl) permit to write if 20..30 - # instead of if (20 <= $.) and ($. <= 30) - new.print $_ -end - - -# @@PLEAC@@_7.9 -#% ruby -i.orig -pe 'FILTER COMMAND' file1 file2 file3 ... -# -#----------------------------- -##!/usr/bin/ruby -i.orig -p -# filter commands go here -#----------------------------- - -#% ruby -pi.orig -e 'gsub!(/DATE/){Time.now)' - -# effectively becomes: -ARGV << 'I' -oldfile = "" -while gets - if ARGF.filename != oldfile - newfile = ARGF.filename - File.rename(newfile, newfile + ".orig") - $stdout = File.open(newfile,'w') - oldfile = newfile - end - gsub!(/DATE/){Time.now} - print -end -$stdout = STDOUT -#----------------------------- -#% ruby -i.old -pe 'gsub!(%r{\bhisvar\b}, 'hervar')' *.[Cchy] - -#----------------------------- -# set up to iterate over the *.c files in the current directory, -# editing in place and saving the old file with a .orig extension -$-i = '.orig' # set up -i mode -ARGV.replace(Dir['*.[Cchy]']) -while gets - if $. == 1 - print "This line should appear at the top of each file\n" - end - gsub!(/\b(p)earl\b/i, '\1erl') # Correct typos, preserving case - print - ARGF.close if ARGF.eof -end - - -# @@PLEAC@@_7.10 -File.open('itest', 'r+') do |f| # open file for update - lines = f.readlines # read into array of lines - lines.each do |it| # modify lines - it.gsub!(/foo/, 'QQQ') - end - f.pos = 0 # back to start - f.print lines # write out modified lines - f.truncate(f.pos) # truncate to new length -end # file is automatically closed -#----------------------------- -File.open('itest', 'r+') do |f| - out = "" - f.each do |line| - out << line.gsub(/DATE/) {Time.now} - end - f.pos = 0 - f.print out - f.truncate(f.pos) -end - -# @@PLEAC@@_7.11 -File.open('infile', 'r+') do |f| - f.flock File::LOCK_EX - # update file -end -#----------------------------- -File::LOCK_SH # shared lock (for reading) -File::LOCK_EX # exclusive lock (for writing) -File::LOCK_NB # non-blocking request -File::LOCK_UN # free lock -#----------------------------- -unless f.flock File::LOCK_EX | File::LOCK_NB - warn "can't get immediate lock: blocking ..." - f.flock File::LOCK_EX -end -#----------------------------- -File.open('numfile', File::RDWR|File::CREAT) do |f| - f.flock(File::LOCK_EX) - num = f.gets.to_i || 0 - f.pos = 0 - f.truncate 0 - f.puts num + 1q -end - - -# @@PLEAC@@_7.12 -output_handle.sync = true -# Please note that like in Perl, $stderr is already unbuffered -#----------------------------- -#!/usr/bin/ruby -w -# seeme - demo stdio output buffering -$stdout.sync = ARGV.size > 0 -print "Now you don't see it..." -sleep 2 -puts "now you do" -#----------------------------- -$stderr.sync = true -afile.sync = false -#----------------------------- -# assume 'remote_con' is an interactive socket handle, -# but 'disk_file' is a handle to a regular file. -remote_con.sync = true # unbuffer for clarity -disk_file.sync = false # buffered for speed -#----------------------------- -require 'socket' -sock = TCPSocket.new('www.ruby-lang.org', 80) -sock.sync = true -sock.puts "GET /en/ HTTP/1.0 \n\n" -resp = sock.read -print "DOC IS: #{resp}\n" - - -# @@PLEAC@@_7.13 -#----------------------------- -# assumes fh1, fh2, fh2 are oen IO objects -nfound = select([$stdin, fh1, fh2, fh3], nil, nil, 0) -nfound[0].each do |file| - case file - when fh1 - # do something with fh1 - when fh2 - # do something with fh2 - when fh3 - # do something with fh3 - end -end -#----------------------------- -input_files = [] -# repeat next line for all in-files to poll -input_files << fh1 -if nfound = select(input_files, nil, nil, 0) - # input ready on files in nfound[0] -end - - -# @@PLEAC@@_8.0 -#----------------------------- -# datafile is a file or IO object -datafile.readlines.each { |line| - line.chomp! - size = line.length - puts size -} -#----------------------------- -datafile.readlines.each { |line| - puts line.chomp!.length -} -#----------------------------- -lines = datafile.readlines -#----------------------------- -whole_file = file.read -#----------------------------- -# ruby -040 -e 'word = gets; puts "First word is #{word}"' -#----------------------------- -# ruby -ne 'BEGIN { $/="%%\n" }; $_.chomp; puts $_ if( $_=~/Unix/i)' fortune.dat -#----------------------------- -handle.print "one", "two", "three" # "onetwothree" -puts "Baa baa black sheep." # sent to $stdout -#----------------------------- -buffer = handle.read(4096) -rv = buffer.length -#----------------------------- -handle.truncate(length) -open("/tmp#{$$}.pid", 'w') { |handle| handle.truncate(length) } -#----------------------------- -pos = datafile.pos # tell is an alias of pos -puts "I'm #{pos} bytes from the start of datafile" -#----------------------------- -logfile.seek(0, IO::SEEK_END) -datafile.seek(pos) # IO::SEEK_SET is the default -out.seek(-20, IO::SEEK_CUR) -#----------------------------- -written = datafile.syswrite(mystring) -raise RunTimeError unless written == mystring.length -block = infile.sysread(256) # no equivalent to perl offset parameter in sysread -puts "only read #{block.length} bytes" if 256 != block.length -#----------------------------- -pos = handle.sysseek(0, IO::SEEK_CUR) # don't change position - - -# @@PLEAC@@_8.1 -while (line = fh.gets) - line.chomp! - nextline = nil - line.gsub!(/\\$/) { |match| nextline = fh.gets; '' } - if (nextline != nil) - line += nextline - redo - end - # process full record in line here -end -#----------------------------- -# DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) \ -# $(TEXINFOS) $(INFOS) $(MANS) $(DATA) -# DEP_DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) \ -# $(TEXINFOS) $(INFO_DEPS) $(MANS) $(DATA) \ -# $(EXTRA_DIST) -#----------------------------- -line.gsub!(/\\\s*$/, '') { - # as before -} - - -# @@PLEAC@@_8.2 -#----------------------------- -count = `wc -l < #{filename}` -fail "wc failed: #{$?}" if $? != 0 -count.chomp! -#----------------------------- -count = 0 -File.open(file, 'r') { |fh| - count += 1 while fh.gets -} -# count now holds the number of lines read -#----------------------------- -count = 0 -while (chunk = file.sysread(2**16)) - count += chunk.count("\n") -end rescue EOFError -#----------------------------- -File.open(filename,'r') { |fh| - count += 1 while fh.gets -} -# count now holds the number of lines read -#----------------------------- -# As ruby doesn't quite have an equivalent to using a for -# statement as in perl, I threw this in -count = File.readlines(filename).size -#----------------------------- -1 while file.gets -count = $. -#----------------------------- -$/ = '' -open(filename, 'r') { |fh| - 1 while fh.gets - para_count = $. -} rescue fail("can't open #{filename}: $!") -#----------------------------- - - -# ^^PLEAC^^_8.3 -#----------------------------- -while (gets) - split.each { |chunk| - # do something with chunk - } -end -#----------------------------- -while (gets) - gsub(/(\w[\w'-]*)/) { |word| - # do something with word - } -end -#----------------------------- -# Make a word frequency count -# normally hashes can be created using {} or just Hash.new -# but we want the default value of an entry to be 0 instead -# of nil. (nil can't be incremented) -seen = Hash.new(0) -while (gets) - gsub(/(\w[\w'-]*)/) { |word| - seen[word.downcase] += 1 - } -end -# output hash in a descending numeric sort of its values -seen.sort { |a,b| b[1] <=> a[1] }.each do |k,v| - printf("%5d %s\n", v, k ) -end - -#----------------------------- -# Line frequency count -seen = Hash.new(0) -while (gets) - seen[$_.downcase] += 1 -end -seen.sort { |a,b| b[1] <=> a[1] }.each do |k,v| - printf("%5d %s\n", v, k ) -end -#----------------------------- - - -# @@PLEAC@@_8.4 -#----------------------------- -# instead of file handle FILE, we can just -# use a string containing the filename -File.readlines(file).each { |line| - # do something with line -} -#----------------------------- -File.readlines(file).reverse_each { |line| - # do something with line -} -#----------------------------- -# the variable lines might have been created -# this way -# lines = File.readlines(file) -# -# normally one would use the reverse_each, but -# if you insist on using a numerical index to -# iterate over the lines array... -(lines.size - 1).downto(0) { |i| - line = lines[i] -} -#----------------------------- -# the second readlines argument is a the -# record separator $/, just like perl, a blank -# separator splits the records into paragraphs -File.readlines(file, '').each { |paragraph| - # do something with paragraph - puts "->Paragraph #{paragraph}" -} -#----------------------------- - - -# @@PLEAC@@_8.6 - -$/ = "%\n"; -srand; - -File.open('/usr/share/fortune/humorists').each do |line| - adage = line if rand($.) < 1 -end - -puts adage; - - -# @@PLEAC@@_8.10 -begin - fh = File.open(file, "r+") - addr = fh.tell unless fh.eof while fh.gets - fh.truncate(addr) -rescue SystemCallError - $stderr.puts "#$!" -end - - -# @@PLEAC@@_9.0 -entry = File.stat("/usr/bin/vi") -entry = File.stat("/usr/bin") -entry = File.stat(INFILE) - -entry = File.stat("/usr/bin/vi") -ctime = entry.ctime -size = entry.size - -f = File.open(filename, "r") - -## There is no -T equivalent in Ruby, but we can still test emptiness -if test(?s, filename) - puts "#{filename} doesn't have text in it." - exit -end - -Dir.new("/usr/bin").each do |filename| - puts "Inside /usr/bin is something called #{filename}" -end - - -# @@PLEAC@@_9.1 -file = File.stat("filename") -readtime, writetime = file.atime, file.mtime -file.utime(readtime, writetime) - -SECONDS_PER_DAY = 60 * 60 * 24 -file = File.stat("filename") -atime, mtime = file.atime, file.mtime - -atime -= 7 * SECONDS_PER_DAY -mtime -= 7 * SECONDS_PER_DAY - -File.utime(atime, mtime, file) -mtime = File.stat(file).mtime -File.utime(Time.new, mtime, file) -File.utime(Time.new, File.stat("testfile").mtime, file) - -#----------------------------- -#!/usr/bin/ruby -w -## uvi - vi a file without changing it's access times - -if ARGV.length != 1 - puts "usage: uvi filename" - exit -end -file = ARGV[0] -atime, mtime = File.stat(file).atime, File.stat(file).mtime -system(ENV["EDITOR"] || "vi", file) -File.utime(atime, mtime, file) -#----------------------------- - - -# @@PLEAC@@_9.2 -File.unlink(FILENAME) - -err_flg = false -filenames.each do |file| - begin - File.unlink(file) - rescue - err_flg = $! - end -end -err_flg and raise "Couldn't unlink all of #{filenames.join(" ")}: #{err_flg}" - -File.unlink(file) - -count = filenames.length -filenames.each do |file| - begin - File.unlink(file) - rescue - count -= 1 - end -end -if count != filenames.length - STDERR.puts "could only delete #{count} of #{filenames.length} files" -end - - -# @@PLEAC@@_9.3 -require "ftools" -File.copy(oldfile, newfile) - -infile = File.open(oldfile, "r") -outfile = File.open(newfile, "w") - -blksize = infile.stat.blksize -# This doesn't handle partial writes or ^Z -# like the Perl version does. -while (line = infile.read(blksize)) - outfile.write(line) -end - -infile.close -outfile.close - -system("cp #{oldfile} #{newfile}") # unix -system("copy #{oldfile} #{newfile}") # dos, vms - -require "ftools" -File.copy("datafile.dat", "datafile.bak") -File.move("datafile.new", "datafile.dat") - - -# @@PLEAC@@_9.4 -$seen = {} # must use global var to be seen inside of method below - -def do_my_thing(filename) - dev, ino = File.stat(filename).dev, File.stat(filename).ino - unless $seen[[dev, ino]] - # do something with $filename because we haven't - # seen it before - end - $seen[[dev, ino]] = $seen[[dev, ino]].to_i + 1 -end - -files.each do |filename| - dev, ino = File.stat(filename).dev, File.stat(filename).ino - if !$seen.has_key?([dev, ino]) - $seen[[dev, ino]] = [] - end - $seen[[dev, ino]].push(filename) -end - -$seen.keys.sort.each do |devino| - ino, dev = devino - if $seen[devino].length > 1 - # $seen[devino] is a list of filenames for the same file - end -end - - -# @@PLEAC@@_9.5 -Dir.open(dirname) do |dir| - dir.each do |file| - # do something with dirname/file - puts file - end -end -# Dir.close is automatic - -# No -T equivalent in Ruby - -dir.each do |file| - next if file =~ /^\.\.?$/ - # ... -end - -def plainfiles(dir) - dh = Dir.open(dir) - dh.entries.grep(/^[^.]/). - map {|file| "#{dir}/#{file}"}. - find_all {|file| test(?f, file)}. - sort -end - - -# @@PLEAC@@_9.6 -list = Dir.glob("*.c") - -dir = Dir.open(path) -files = dir.entries.grep(/\.c$/) -dir.close - -files = Dir.glob("*.c") -files = Dir.open(path).entries.grep(/\.[ch]$/i) - -dir = Dir.new(path) -files = dir.entries.grep(/\.[ch]$/i) - -begin - d = Dir.open(dir) -rescue Errno::ENOENT - raise "Couldn't open #{dir} for reading: #{$!}" -end - -files = [] -d.each do |file| - puts file - next unless file =~ /\.[ch]$/i - - filename = "#{dir}/#{file}" - # There is no -T equivalent in Ruby, but we can still test emptiness - files.push(filename) if test(?s, filename) -end - -dirs.entries.grep(/^\d+$/). - map { |file| [file, "#{path}/#{file}"]} . - select { |file| test(?d, file[1]) }. - sort { |a,b| a[0] <=> b[0] }. - map { |file| file[1] } - - -# @@PLEAC@@_9.7 -require 'find' -Find.find(dirlist) do |file| - # do whatever -end - -require 'find' -argv = ARGV.empty? ? %w{.} : ARGV -Find.find(*argv) do |file| - print file, (test(?d, file) ? "/\n" : "\n") -end - -require 'find' -argv = ARGV.empty? ? %w{.} : ARGV -sum = 0 -Find.find(*argv) do |file| - size = test(?s, file) || 0 - sum += size -end -puts "#{argv.join(' ')} contains #{sum} bytes" - -require 'find' -argv = ARGV.empty? ? %w{.} : ARGV -saved_size, saved_name = -1, "" -Find.find(*argv) do |file| - size = test(?s, file) || 0 - next unless test(?f, file) && size > saved_size - saved_size = size - saved_name = file -end -puts "Biggest file #{saved_name} in #{argv.join(' ')} is #{saved_size}" - -require 'find' -argv = ARGV.empty? ? %w{.} : ARGV -age, name = nil -Find.find(*argv) do |file| - mtime = File.stat(file).mtime - next if age && age > mtime - age = mtime - name = file -end -puts "#{name} #{age}" - -#----------------------------- -#!/usr/bin/ruby -w -# fdirs - find all directories -require 'find' -argv = ARGV.empty? ? %w{.} : ARGV -File.find(*argv) { |file| puts file if test(?d, file) } -#----------------------------- - - -# @@PLEAC@@_9.8 -require 'fileutils' - -puts "Usage #{$0} dir ..." if ARGV.empty? -ARGV.each do |dir| - FileUtils.rmtree(dir) -end - - -# @@PLEAC@@_9.9 -require 'ftools' -names.each do |file| - newname = file - begin - File.move(file, newname) - rescue Errno::EPERM - $stderr.puts "Couldn't rename #{file} to #{newname}: #{$!}" - end -end - -require 'ftools' -op = ARGV.empty? ? (raise "Usage: rename expr [files]\n") : ARGV.shift -argv = ARGV.empty? ? $stdin.readlines.map { |f| f.chomp } : ARGV -argv.each do |file| - was = file - file = eval("file.#{op}") - File.move(was, file) unless was == file -end - - -# @@PLEAC@@_9.10 -base = File.basename(path) -dir = File.dirname(path) -# ruby has no fileparse equivalent -dir, base = File.split(path) -ext = base.scan(/\..*$/).to_s - -path = '/usr/lib/libc.a' -file = File.basename(path) -dir = File.dirname(path) - -puts "dir is #{dir}, file is #{file}" -# dir is /usr/lib, file is libc.a - -path = '/usr/lib/libc.a' -dir, filename = File.split(path) -name, ext = filename.split(/(?=\.)/) -puts "dir is #{dir}, name is #{name}, ext is #{ext}" -# NOTE: The Ruby code prints -# dir is /usr/lib, name is libc, extension is .a -# while the Perl code prints a '/' after the directory name -# dir is /usr/lib/, name is libc, extension is .a - -# No fileparse_set_fstype() equivalent in ruby - -def extension(path) - ext = path.scan(/\..*$/).to_s - ext.sub(/^\./, "") -end - - -# @@PLEAC@@_9.11 -#----------------------------- -#!/usr/bin/ruby -w -# symirror - build spectral forest of symlinks - -require 'find' -require 'fileutils' - -raise "usage: #{$0} realdir mirrordir" unless ARGV.size == 2 - -srcdir,dstdir = ARGV -srcmode = File::stat(srcdir).mode -Dir.mkdir(dstdir, srcmode & 07777) unless test(?d, dstdir) - -# fix relative paths -Dir.chdir(srcdir) {srcdir = Dir.pwd} -Dir.chdir(dstdir) {dstdir = Dir.pwd} - -Find.find(srcdir) do |srcfile| - if test(?d, srcfile) - dest = srcfile.sub(/^#{srcdir}/, dstdir) - dmode = File::stat(srcfile).mode & 07777 - Dir.mkdir(dest, dmode) unless test(?d, dest) - a = Dir["#{srcfile}/*"].reject{|f| test(?d, f)} - FileUtils.ln_s(a, dest) - end -end - - -# @@PLEAC@@_9.12 -# we use the Getopt/Declare library here for convenience: -# http://raa.ruby-lang.org/project/getoptdeclare/ -#----------------------------- -#!/usr/bin/ruby -w -# lst - list sorted directory contents (depth first) - -require 'find' -require 'etc' -require "Getopt/Declare" - -# Note: in the option-spec below there must by at least one hard -# tab in between each -option and its description. For example -# -i read from stdin - -opts = Getopt::Declare.new(<<'EOPARAM') - ============ - Input Format: - -i read from stdin - ============ - Output Format: - -l long listing - -r reverse listing - ============ - Sort on: (one of) - -m mtime (modify time - default) - {$sort_criteria = :mtime} - -u atime (access time) - {$sort_criteria = :atime} - -c ctime (inode change time) - {$sort_criteria = :ctime} - -s size - {$sort_criteria = :size} - [mutex: -m -u -c -s] - -EOPARAM - -$sort_criteria ||= :mtime -files = {} -DIRS = opts['-i'] ? $stdin.readlines.map{|f|f.chomp!} : ARGV -DIRS.each do |dir| - Find.find(dir) do |ent| - files[ent] = File::stat(ent) - end -end -entries = files.keys.sort_by{|f| files[f].send($sort_criteria)} -entries = entries.reverse unless opts['-r'] - -entries.each do |ent| - unless opts['-l'] - puts ent - next - end - stats = files[ent] - ftime = stats.send($sort_criteria == :size ? :mtime : $sort_criteria) - printf "%6d %04o %6d %8s %8s %8d %s %s\n", - stats.ino, - stats.mode & 07777, - stats.nlink, - ETC::PASSWD[stats.uid].name, - ETC::GROUP[stats.gid].name, - stats.size, - ftime.strftime("%a %b %d %H:%M:%S %Y"), - ent -end - - -# @@PLEAC@@_10.0 -def hello - $greeted += 1 # in Ruby, a variable beginning with $ is global (can be any type of course) - puts "hi there!" -end - -# We need to initialize $greeted before it can be used, because "+=" is waiting a Numeric object -$greeted = 0 -hello # note that appending () is optional to function calls with no parameters - - -# @@PLEAC@@_10.1 -# In Ruby, parameters are named anyway -def hypotenuse(side1, side2) - Math.sqrt(side1**2 + side2**2) # the sqrt function comes from the Math module -end -diag = hypotenuse(3, 4) - -puts hypotenuse(3, 4) - -a = [3, 4] -print hypotenuse(*a) # the star operator will magically convert an Array into a "tuple" - -both = men + women - -# In Ruby, all objects are references, so the same problem arises; we then return a new object -nums = [1.4, 3.5, 6.7] -def int_all(n) - n.collect { |v| v.to_i } -end -ints = int_all(nums) - -nums = [1.4, 3.5, 6.7] -def trunc_em(n) - n.collect! { |v| v.to_i } # the bang-version of collect modifies the object -end -trunc_em(nums) - -# Ruby has two chomp version: -# ``chomp'' chomps the record separator and returns what's expected -# ``chomp!'' does the same but also modifies the parameter object - - -# @@PLEAC@@_10.2 -def somefunc - variable = something # variable is local by default -end - -name, age = ARGV -start = fetch_time - -a, b = pair # will succeed if pair is an Array object (like ARGV is) -c = fetch_time - -# In ruby, run_check can't access a, b, or c until they are -# explicitely defined global (using leading $), even if they are -# both defined in the same scope - -def check_x(x) - y = "whatever" - run_check - if $condition - puts "got $x" - end -end - -# The following will keep a reference to the array, though the -# results will be slightly different from perl: the last element -# of $global_array will be itself an array -def save_array(ary) - $global_array << ary -end - -# The following gives the same results as in Perl for $global_array, -# though it doesn't illustrate anymore the way to keep a reference -# to an object: $global_array is extended with the elements of ary -def save_array(ary) - $global_array += ary -end - - -# @@PLEAC@@_10.3 -# In Ruby, AFAIK a method cannot access "local variables" defined -# upper scope; mostly because everything is an object, so you'll -# do the same by defining an attribute or a static attribute - -# In Ruby the BEGIN also exists: -BEGIN { puts "hello from BEGIN" } -puts "hello from main" -BEGIN { puts "hello from 2nd BEGIN" } -# gives: -# hello from BEGIN -# hello from 2nd BEGIN -# hello from main - -# In Ruby, it can be written as a static method and a static -# variable -class Counter - @@counter = 0 - def Counter.next_counter; @@counter += 1; end -end - -# There is no need of BEGIN since the variable will get -# initialized when parsing -class Counter - @@counter = 42 - def Counter.next_counter; @@counter += 1; end - def Counter.prev_counter; @@counter -= 1; end -end - - -# @@PLEAC@@_10.4 -# You can either get the whole trace as an array of strings, each -# string telling which file, line and method is calling: -caller - -# ...or only the last caller -caller[0] - -# We need to extract just the method name of the backtrace: -def whoami; caller()[0] =~ /in `([^']+)'/ ? $1 : '(anonymous)'; end -def whowasi; caller()[1] =~ /in `([^']+)'/ ? $1 : '(anonymous)'; end - - -# @@PLEAC@@_10.5 -# In Ruby, every value is a reference on an object, thus there is -# no such problem -array_diff(array1, array2) - -def add_vecpair(a1, a2) - results = [] - a1.each_index { |i| results << (a1[i] + a2[i]) } - results -end -a = [1, 2] -b = [5, 8] -c = add_vecpair(a, b) -p c - -# Add this to the beginning of the function to check if we were -# given two arrays -a1.type == Array && a2.type == Array or - raise "usage: add_vecpair array1 array2 (was used with: #{a1.type} #{a2.type})" - - -# @@PLEAC@@_10.6 -# There is no return context in Ruby - - -# @@PLEAC@@_10.7 -# Like in Perl, we need to fake with a hash, but it's dirty :-( -def thefunc(param_args) - args = { 'INCREMENT' => '10s', 'FINISH' => '0', 'START' => 0 } - args.update(param_args) - if (args['INCREMENT'] =~ /m$/ ) - # ..... - end -end - -thefunc({ 'INCREMENT' => '20s', 'START' => '+5m', 'FINISH' => '+30m' }) -thefunc({}) - - -# @@PLEAC@@_10.8 -# there is no "undef" direct equivalent but there is the slice equiv: -a, c = func.indexes(0, 2) - - -# @@PLEAC@@_10.9 -# Ruby has no such limitation: -def somefunc - ary = [] - hash = {} - # ... - return ary, hash -end -arr, dict = somefunc - -array_of_hashes = fn -h1, h2, h3 = fn - - -# @@PLEAC@@_10.10 -return -# or (equivalent) -return nil - - -# @@PLEAC@@_10.11 -# You can't prototype in Ruby regarding types :-( -# Though, you can force the number of arguments: -def func_with_no_arg; end -def func_with_no_arg(); end -def func_with_one_arg(a1); end -def func_with_two_args(a1, a2); end -def func_with_any_number_of_args(*args); end - - -# @@PLEAC@@_10.12 -raise "some message" # raise exception - -begin - val = func -rescue Exception => msg - $stderr.puts "func raised an exception: #{msg}" -end - -# In Ruby the rescue statement uses an exception class, every -# exception which is not matched is still continuing -begin - val = func -rescue FullMoonError - ... -end - - -# @@PLEAC@@_10.13 -# Saving Global Values -# Of course we can just save the value and restore it later: -def print_age - puts "Age is #{$age}" -end - -$age = 18 # global variable -print_age() -if condition - safeage = $age - $age = 23 - print_age() - $age = safeage -end - -# We can also use a method that saves the global variable and -# restores it automatically when the block is left: - -def local(var) - eval("save = #{var.id2name}") - begin - result = yield - ensure - # we want to call this even if we got an exception - eval("#{var.id2name} = save") - end - result -end - -condition = true -$age = 18 -print_age() -if condition - local(:$age) { - $age = 23 - print_age() - } -end -print_age() - -# There is no need to use local() for filehandles or directory -# handles in ruby because filehandles are normal objects. - - -# @@PLEAC@@_10.14 -# In Ruby you may redefine a method [but not overload it :-(] -# just by defining again with the same name. -def foo; puts 'foo'; end -def foo; puts 'bar'; end -foo -#=> bar - -# You can also take a reference to an existing method before -# redefining a new one, using the `alias' keyword -def foo; puts 'foo'; end -alias foo_orig foo -def foo; puts 'bar'; end -foo_orig -foo -#=> foo -#=> bar - -# AFAIK, there is no direct way to create a new method whose name -# comes from a variable, so use "eval" -colors = %w(red blue green yellow orange purple violet) -colors.each { |c| - eval <<-EOS - def #{c}(*a) - "" + a.to_s + "" - end - EOS -} - - -# @@PLEAC@@_10.15 -def method_missing(name, *args) - "" + args.join(' ') + "" -end -puts chartreuse("stuff") - - -# @@PLEAC@@_10.16 -def outer(arg) - x = arg + 35 - inner = proc { x * 19 } - x + inner.call() -end - - -# @@PLEAC@@_10.17 -#!/usr/bin/ruby -w -# mailsort - sort mbox by different criteria -require 'English' -require 'Date' - -# Objects of class Mail represent a single mail. -class Mail - attr_accessor :no - attr_accessor :subject - attr_accessor :fulltext - attr_accessor :date - - def initialize - @fulltext = "" - @subject = "" - end - - def append(para) - @fulltext << para - end - - # this is called if you call puts(mail) - def to_s - @fulltext - end -end - -# represents a list of mails. -class Mailbox < Array - - Subjectpattern = Regexp.new('Subject:\s*(?:Re:\s*)*(.*)\n') - Datepattern = Regexp.new('Date:\s*(.*)\n') - - # reads mails from open file and stores them - def read(file) - $INPUT_RECORD_SEPARATOR = '' # paragraph reads - msgno = -1 - file.each { |para| - if para =~ /^From/ - mail = Mail.new - mail.no = (msgno += 1) - md = Subjectpattern.match(para) - if md - mail.subject = md[1] - end - md = Datepattern.match(para) - if md - mail.date = DateTime.parse(md[1]) - else - mail.date = DateTime.now - end - self.push(mail) - end - mail.append(para) if mail - } - end - - def sort_by_subject_and_no - self.sort_by { |m| - [m.subject, m.no] - } - end - - # sorts by a list of attributs of mail, given as symbols - def sort_by_attributs(*attrs) - # you can sort an Enumerable by an array of - # values, they would be compared - # from ary[0] to ary[n]t, say: - # ['b',1] > ['a',10] > ['a',9] - self.sort_by { |elem| - attrs.map { |attr| - elem.send(attr) - } - } - end - -end - -mailbox = Mailbox.new -mailbox.read(ARGF) - -# print only subjects sorted by subject and number -for m in mailbox.sort_by_subject_and_no - puts(m.subject) -end - -# print complete mails sorted by date, then subject, then number -for m in mailbox.sort_by_attributs(:date, :subject) - puts(m) -end - - -# @@PLEAC@@_11.7 -def mkcounter(count) - start = count - bundle = { - "NEXT" => proc { count += 1 }, - "PREV" => proc { count -= 1 }, - "RESET" => proc { count = start } - } - bundle["LAST"] = bundle["PREV"] - return bundle -end - -c1 = mkcounter(20) -c2 = mkcounter(77) - -puts "next c1: #{c1["NEXT"].call}" # 21 -puts "next c2: #{c2["NEXT"].call}" # 78 -puts "next c1: #{c1["NEXT"].call}" # 22 -puts "last c1: #{c1["PREV"].call}" # 21 -puts "last c1: #{c1["LAST"].call}" # 20 -puts "old c2: #{c2["RESET"].call}" # 77 - - -# @@PLEAC@@_11.15 -class Binary_tree - def initialize(val) - @value = val - @left = nil - @right = nil - end - - # insert given value into proper point of - # provided tree. If no tree provided, - # use implicit pass by reference aspect of @_ - # to fill one in for our caller. - def insert(val) - if val < @value then - if @left then - @left.insert(val) - else - @left = Binary_tree.new(val) - end - elsif val > @value then - if @right then - @right.insert(val) - else - @right = Binary_tree.new(val) - end - else - puts "double" - # do nothing, no double values - end - end - - # recurse on left child, - # then show current value, - # then recurse on right child. - def in_order - @left.in_order if @left - print @value, " " - @right.in_order if @right - end - - # show current value, - # then recurse on left child, - # then recurse on right child. - def pre_order - print @value, " " - @left.pre_order if @left - @right.pre_order if @right - end - - # recurse on left child, - # then recurse on right child, - # then show current value. - def post_order - @left.post_order if @left - @right.post_order if @right - print @value, " " - end - - # find out whether provided value is in the tree. - # if so, return the node at which the value was found. - # cut down search time by only looking in the correct - # branch, based on current value. - def search(val) - if val == @value then - return self - elsif val < @value then - return @left.search(val) if @left - return nil - else - return @right.search(val) if @right - return nil - end - end -end - -# first generate 20 random inserts -test = Binary_tree.new(0) -for a in 0..20 - test.insert(rand(1000)) -end - -# now dump out the tree all three ways -print "Pre order: "; test.pre_order; puts "" -print "In order: "; test.in_order; puts "" -print "Post order: "; test.post_order; puts "" - -print "search?" -while gets - print test.search($_.to_i) - print "\nsearch?" -end - - -# @@PLEAC@@_12.0 -# class and module names need to have the first letter capitalized -module Alpha - NAME = 'first' -end -module Omega - NAME = 'last' -end -puts "Alpha is #{Alpha::NAME}, Omega is #{Omega::NAME}" - -# ruby doesn't differentiate beteen compile-time and run-time -require 'getoptlong.rb' -require 'getoptlong' # assumes the .rb -require 'cards/poker.rb' -require 'cards/poker' # assumes the .rb -load 'cards/poker' # require only loads the file once - -module Cards - module Poker - @card_deck = Array.new # or @card_deck = [] - def shuffle - end - end -end - - -# @@PLEAC@@_12.1 -# a module exports all of its functions -module Your_Module - def self.function - # this would be called as Your_Module.function - end - - def Your_Module.another - # this is the same as above, but more specific - end -end - -# @@PLEAC@@_12.2 -begin - require 'nonexistent' -rescue LoadError - puts "Couldn't load #{$!}" # $! contains the last error string -end - -# @@PLEAC@@_12.4 -# module variables are private unless access functions are defined -module Alpha - @aa = 10 - @bb = 11 - - def self.put_aa - puts @aa - end - - def self.bb=(val) - @bb = val - end -end - -Alpha.bb = 12 -# Alpha.aa = 10 # error, no aa=method - - -# @@PLEAC@@_12.5 -# caller provides a backtrace of the call stack -module MyModule - def find_caller - caller - end - - def find_caller2(i) - caller(i) # an argument limits the size of the stack returned - end -end - - -# @@PLEAC@@_12.6 -BEGIN { - $logfile = '/tmp/mylog' unless defined? $logfile - $LF = File.open($logfile, 'a') -} - -module Logger - def self.logmsg(msg) - $LF.puts msg - end - - logmsg('startup') -end - -END { - Logger::logmsg('shutdown') - $LF.close -} - - -# @@PLEAC@@_12.7 -#----------------------------- -# results may be different on your system -# % ruby -e "$LOAD_PATH.each_index { |i| printf("%d %s\n", i, $LOAD_PATH[i] } -#0 /usr/local/lib/site_ruby/1.6 -#1 /usr/local/lib/site_ruby/1.6/i386-linux -#2 /usr/local/lib/site_ruby/ -#3 /usr/lib/ruby/1.6 -#4 /usr/lib/ruby/1.6/i136-linux -#5 . -#----------------------------- -# syntax for sh, bash, ksh, or zsh -#$ export RUBYLIB=$HOME/rubylib - -# syntax for csh or tcsh -# % setenv RUBYLIB ~/rubylib -#----------------------------- -$LOAD_PATH.unshift "/projects/spectre/lib"; - - -# @@PLEAC@@_12.8 -# equivalents in ruby are mkmf, SWIG, or Ruby/DL depending on usage - - -# @@PLEAC@@_12.9 -# no equivalent in ruby - - -# @@PLEAC@@_12.10 -# no equivalent in ruby - - -# @@PLEAC@@_12.11 -module FineTime - def self.time - # to be defined later - end -end - - -module FineTime - def self.time - "its a fine time" - end -end - -puts FineTime.time #=> "its a fine time" - - -# @@PLEAC@@_12.12 -def even_only(n) - raise "#{n} is not even" if (n & 1) != 0 # one way to test - # ... -end -def even_only(n) - $stderr.puts "#{n} is not even" if (n & 1) != 0 - # ... -end - - -# @@PLEAC@@_12.17 -# The library archive for ruby is called Ruby Application archive, -# or shorter RAA, and can be found at http://raa.ruby-lang.org. -# A typical library is installed like this: -# % gunzip some-module-4.54.tar.gz -# % tar xf some-module-4.54.tar -# % cd some-module-4.54.tar -# % ruby install.rb config -# % ruby install.rb setup -# get superuser previleges here if needed for next step -# % ruby install.rb install - -# Some modules use a different process, -# you should find details in the documentation -# Here is an example of such a different process -# % ruby extconf.rb -# % make -# % make install - -# If you want the module installed in your own directory: -# For ruby version specific libraries -# % ruby install.rb config --site-ruby=~/lib -# For version independent libraries -# % ruby install.rb config --site-ruby-common=~/lib - -# Information about possible options for config -# % ruby install.rb --help - -# If you have your own complete distribution -# % ruby install.rb --prefix=path=~/ruby-private - - -# @@PLEAC@@_13.0 -# Classes and objects in Ruby are rather straigthforward -class Person - # Class variables (also called static attributes) are prefixed by @@ - @@person_counter=0 - - # object constructor - def initialize(age, name, alive = true) # Default arg like in C++ - @age, @name, @alive = age, name, alive # Object attributes are prefixed by '@' - @@person_counter += 1 - # There is no '++' operator in Ruby. The '++'/'--' operators are in fact - # hidden assignments which affect variables, not objects. You cannot accomplish - # assignment via method. Since everything in Ruby is object, '++' and '--' - # contradict Ruby OO ideology. Instead '-=' and '+=' are used. - end - - attr_accessor :name, :age # This creates setter and getter methods for @name - # and @age. See 13.3 for detailes. - - # methods modifying the receiver object usually have the '!' suffix - def die! - @alive = false - puts "#{@name} has died at the age of #{@age}." - @alive - end - - def kill(anotherPerson) - print @name, ' is killing ', anotherPerson.name, ".\n" - anotherPerson.die! - end - - # methods used as queries - # usually have the '?' suffix - def alive? - @alive && true - end - - def year_of_birth - Time.now.year - @age - end - - # Class method (also called static method) - def Person.number_of_people - @@person_counter - end -end - -# Using the class: -# Create objects of class Person -lecter = Person.new(47, 'Hannibal') -starling = Person.new(29, 'Clarice', true) -pazzi = Person.new(40, 'Rinaldo', true) - -# Calling a class method -print "There are ", Person.number_of_people, " Person objects\n" - -print pazzi.name, ' is ', (pazzi.alive?) ? 'alive' : 'dead', ".\n" -lecter.kill(pazzi) -print pazzi.name, ' is ', (pazzi.alive?) ? 'alive' : 'dead', ".\n" - -print starling.name , ' was born in ', starling.year_of_birth, "\n" - - -# @@PLEAC@@_13.1 -# If you don't need any initialisation in the constructor, -# you don't need to write a constructor. -class MyClass -end - -class MyClass - def initialize - @start = Time.new - @age = 0 - end -end - -class MyClass - def initialize(inithash) - @start = Time.new - @age = 0 - for key, value in inithash - instance_variable_set("@#{key}", value) - end - end -end - -# @@PLEAC@@_13.2 -# Objects are destroyed by the garbage collector. -# The time of destroying is not predictable. -# The ruby garbage collector can handle circular references, -# so there is no need to write destructor for that. - -# There is no direct support for destructor. -# You can call a custom function, or more specific a proc object, when the -# garbage collector is about to destruct the object, but it is unpredictable -# when this occurs. -# Also if such a finalizer object has a reference to the orignal object, -# this may prevent the original object to get garbage collected. -# Because of this problem the finalize method below is -# a class method and not a instance method. -# So if you need to free resources for an object, like -# closing a socket or kill a spawned subprocess, -# you should do it explicitly. - -class MyClass - def initialize - ObjectSpace.define_finalizer(self, - self.class.method(:finalize).to_proc) - end - def MyClass.finalize(id) - puts "Object #{id} dying at #{Time.new}" - end -end - -# test code -3.times { - MyClass.new -} -ObjectSpace.garbage_collect - - -# @@PLEAC@@_13.3 -# You can write getter and setter methods in a natural way: -class Person - def name - @name - end - def name=(name) - @name = name - end -end - -# But there is a better and shorter way -class Person - attr_reader :age - attr_writer :name - # attr_reader and attr_writer are actually methods in class Class - # which set getter and setter methods for you. -end - -# There is also attr_accessor to create both setters and getters -class Person - attr_accessor :age, :name -end - - -# @@PLEAC@@_13.4 -class Person - # Class variables (also called static attributes) are prefixed by @@ - @@person_counter = 0 - - def Person.population - @@person_counter - end - def initialize - @@person_counter += 1 - ObjectSpace.define_finalizer(self, - self.class.method(:finalize).to_proc) - end - def Person.finalize(id) - @@person_counter -= 1 - end -end -people = [] -10.times { - people.push(Person.new) -} -printf("There are %d people alive", Person.population) - - -FixedArray.class_max_bounds = 100 -alpha = FixedArray.new -puts "Bound on alpha is #{alpha.max_bounds}" - -beta = FixedArray.new -beta.max_bounds = 50 # calls the instance method -beta.class.class_max_bounds = 50 # alternative, calls the class method -puts "Bound on alpha is #{alpha.max_bounds}" - -class FixedArray - @@bounds = 7 - - def max_bounds - @@max_bounds - end - # instance method, which sets the class variable - def max_bounds=(value) - @@max_bounds = value - end - # class method. This can only be called on a class, - # but not on the instances - def FixedArray.class_max_bounds=(value) - @@max_bounds = value - end -end - - -# @@PLEAC@@_13.5 -PersonStruct = Struct.new("Person", :name, :age, :peers) -# creates a class "Person::Struct", which is accessiable with the -# constant "PersonStruct" -p = PersonStruct.new -p = Struct::Person.new # alternative using the classname -p.name = "Jason Smythe" -p.age = 13 -p.peers = ["Wilbur", "Ralph", "Fred"] -p[:peers] = ["Wilbur", "Ralph", "Fred"] # alternative access using symbol -p["peers"] = ["Wilbur", "Ralph", "Fred"] # alternative access using name of field -p[2] = ["Wilbur", "Ralph", "Fred"] # alternative access using index of field -puts "At age #{p.age}, #{p.name}'s first friend is #{p.peers[0]}" - -# The fields of a struct have no special type, like other ruby variables -# you can put any objects in. Therefore the discussions how to specify -# the types of the fields do not apply to ruby. - -FamilyStruct = Struct.new("Family", :head, :address, :members) -folks = FamilyStruct.new -folks.head = PersonStruct.new -dad = folks.head -dad.name = "John" -dad.age = 34 - -# supply of own accessor method for the struct for error checking -class PersonStruct - def age=(value) - if !value.kind_of?(Integer) - raise(ArgumentError, "Age #{value} isn't an Integer") - elsif value > 150 - raise(ArgumentError, "Age #{value} is unreasonable") - end - @age = value - end -end - - -# @@PLEAC@@_13.6 -# The ruby Object class defines a dup and a clone method. -# The dup method is recommended for prototype object creation. -# The default implementation makes a shallow copy, -# but each class can override it, for example to make a deep copy. - -# If you want to call 'new' directly on the instances, -# you can create a instance method "new", which returns a new duplicate. -# This method is distinct from the class method new. -# -class A - def new - dup - end -end - -ob1 = A.new -# later on -ob2 = ob1.new - - -# @@PLEAC@@_13.7 -methname = 'flicker' -obj.send(methname, 10) # calls obj.flicker(10) - -# call three methods on the object, by name -['start', 'run', 'stop'].each do |method_string| - obj.send(method_string) -end - -# Another way is to create a Method object -method_obj = obj.method('flicker') -# And then call it -method_obj.call(10) - - -# @@PLEAC@@_13.8 -# All classes in Ruby inherit from class Object -# and thus all objects share methods defined in this class - -# the class of the object -puts any_object.type - -# Ruby classes are actually objects of class Class and they -# respond to methods defined in Object class as well - -# the superclass of this class -puts any_object.class.superclass - -# ask an object whether it is an instance of particular class -n = 4.7 -puts n.instance_of?(Float) # true -puts n.instance_of?(Numeric) # false - -# ask an object whether it is an instance of class, one of the -# superclasses of the object, or modules included in it -puts n.kind_of?(Float) # true (the class) -puts n.kind_of?(Numeric) # true (an ancestor class) -puts n.kind_of?(Comparable) # true (a mixin module) -puts n.kind_of?(String) # false - -# ask an object whether it can respond to a particular method -puts n.respond_to?('+') # true -puts n.respond_to?('length') # false - -# all methods an object can respond to -'just a string'.methods.each { |m| puts m } - - -# @@PLEAC@@_13.9 -# Actually any class in Ruby is inheritable -class Person - attr_accessor :age, :name - def initialize - @name - @age - end -end -#----------------------------- -dude = Person.new -dude.name = 'Jason' -dude.age = 23 -printf "%s is age %d.\n", dude.name, dude.age -#----------------------------- -# Inheriting from Person -class Employee < Person - attr_accessor :salary -end -#----------------------------- -empl = Employee.new -empl.name = 'Jason' -empl.age = 23 -empl.salary = 200 -printf "%s is age %d, the salary is %d.\n", empl.name, empl.age, empl.salary -#----------------------------- -# Any built-in class can be inherited the same way -class WeirdString < String - def initialize(obj) - super obj - end - def +(anotherObj) # + method in this class is overridden - # to return the sum of string lengths - self.length + anotherObj.length # 'self' can be omitted - end -end -#----------------------------- -a = WeirdString.new('hello') -b = WeirdString.new('bye') - -puts a + b # the overridden + -#=> 8 -puts a.length # method from the superclass, String -#=> 5 - - -# @@PLEAC@@_13.11 -# In ruby you can override the method_missing method -# to have a solution similar to perls AUTOLOAD. -class Person - - def initialize - @ok_fields = %w(name age peers parent) - end - - def valid_attribute?(name) - @ok_fields.include?(name) - end - - def method_missing(namesymbol, *params) - name = namesymbol.to_s - return if name =~ /^A-Z/ - if name.to_s[-1] == ('='[0]) # we have a setter - isSetter = true - name.sub!(/=$/, '') - end - if valid_attribute?(name) - if isSetter - instance_variable_set("@#{name}", *params) - else - instance_variable_get("@#{name}", *params) - end - else - # if no annestor is responsible, - # the Object class will throw a NoMethodError exception - super(namesymbol, *params) - end - end - - def new - kid = Person.new - kid.parent = self - kid - end - -end - -dad = Person.new -dad.name = "Jason" -dad.age = 23 -kid = dad.new -kid.name = "Rachel" -kid.age = 2 -puts "Kid's parent is #{kid.parent.name}" -puts dad -puts kid - -class Employee < Person - def initialize - super - @ok_fields.push("salary", "boss") - end - def ok_fields - @ok_fields - end -end - - -# @@PLEAC@@_13.13 -# The ruby garbage collector pretends to cope with circular structures. -# You can test it with this code: -class RingNode - attr_accessor :next - attr_accessor :prev - attr_reader :name - - def initialize(aName) - @name = aName - ObjectSpace.define_finalizer(self, - self.class.method(:finalize).to_proc) - end - - def RingNode.finalize(id) - puts "Node #{id} dying" - end - - def RingNode.show_all_objects - ObjectSpace.each_object {|id| - puts id.name if id.class == RingNode - } - end -end - -def create_test - a = RingNode.new("Node A") - b = RingNode.new("Node B") - c = RingNode.new("Node C") - a.next = b - b.next = c - c.next = a - a.prev = c - c.prev = b - b.prev = a - - a = nil - b = nil - c = nil -end - -create_test -RingNode.show_all_objects -ObjectSpace.garbage_collect -puts "After garbage collection" -RingNode.show_all_objects - - -# @@PLEAC@@_13.14 -class String - def <=>(other) - self.casecmp other - end -end - -# There is no way to directly overload the '""' (stringify) -# operator in Ruby. However, by convention, classes which -# can reasonably be converted to a String will define a -# 'to_s' method as in the TimeNumber class defined below. -# The 'puts' method will automatcally call an object's -# 'to_s' method as is demonstrated below. -# Furthermore, if a class defines a to_str method, an object of that -# class can be used most any place where the interpreter is looking -# for a String value. - -#--------------------------------------- -# NOTE: Ruby has a builtin Time class which would usually be used -# to manipulate time objects, the following is supplied for -# educational purposes to demonstrate operator overloading. -# -class TimeNumber - attr_accessor :hours,:minutes,:seconds - def initialize( hours, minutes, seconds) - @hours = hours - @minutes = minutes - @seconds = seconds - end - - def to_s - return sprintf( "%d:%02d:%02d", @hours, @minutes, @seconds) - end - - def to_str - to_s - end - - def +( other) - seconds = @seconds + other.seconds - minutes = @minutes + other.minutes - hours = @hours + other.hours - if seconds >= 60 - seconds %= 60 - minutes += 1 - end - if minutes >= 60 - minutes %= 60 - hours += 1 - end - return TimeNumber.new(hours, minutes, seconds) - end - - def -(other) - raise NotImplementedError - end - - def *(other) - raise NotImplementedError - end - - def /( other) - raise NotImplementedError - end -end - -t1 = TimeNumber.new(0, 58, 59) -sec = TimeNumber.new(0, 0, 1) -min = TimeNumber.new(0, 1, 0) -puts t1 + sec + min + min - -#----------------------------- -# StrNum class example: Ruby's builtin String class already has the -# capabilities outlined in StrNum Perl example, however the '*' operator -# on Ruby's String class acts differently: It creates a string which -# is the original string repeated N times. -# -# Using Ruby's String class as is in this example: -x = "Red"; y = "Black" -z = x+y -r = z*3 # r is "RedBlackRedBlackRedBlack" -puts "values are #{x}, #{y}, #{z}, and #{r}" -print "#{x} is ", x < y ? "LT" : "GE", " #{y}\n" -# prints: -# values are Red, Black, RedBlack, and RedBlackRedBlackRedBlack -# Red is GE Black - -#----------------------------- -class FixNum - REGEX = /(\.\d*)/ - DEFAULT_PLACES = 0 - attr_accessor :value, :places - def initialize(value, places = nil) - @value = value - if places - @places = places - else - m = REGEX.match(value.to_s) - if m - @places = m[0].length - 1 - else - @places = DEFAULT_PLACES - end - end - end - - def +(other) - FixNum.new(@value + other.value, max(@places, other.places)) - end - - def *(other) - FixNum.new(@value * other.value, max(@places, other.places)) - end - - def /(other) - puts "Divide: #{@value.to_f/other.value.to_f}" - result = FixNum.new(@value.to_f/other.value.to_f) - result.places = max(result.places,other.places) - result - end - - def to_s - sprintf("STR%s: %.*f", self.class.to_s , @places, @value) #. - end - - def to_str - to_s - end - - def to_i #convert to int - @value.to_i - end - - def to_f #convert to float` - @value.to_f - end - - private - def max(a,b) - a > b ? a : b - end -end - -def demo() - x = FixNum.new(40) - y = FixNum.new(12, 0) - - puts "sum of #{x} and #{y} is #{x+y}" - puts "product of #{x} and #{y} is #{x*y}" - - z = x/y - puts "#{z} has #{z.places} places" - unless z.places - z.places = 2 - end - - puts "div of #{x} by #{y} is #{z}" - puts "square of that is #{z*z}" -end - -if __FILE__ == $0 - demo() -end - - -# @@PLEAC@@_14.1 -# There are dbm, sdbm, gdbm modules -# and the bdb module for accessing the berkeley db -# sdbm seem to be available on the most systems, -# so we use it here -# -require "sdbm" -SDBM.open("filename", 0666) { |dbobj| - # raises exception if open error - - # the returned sdbm-dbobj has most of the methods of a hash - v = dbobj["key"] - dbobj["key"] = "newvalue" - if dbobj.has_key?("key") - # ... - end - dbobj.delete("key2") -} -# database is open only inside the block. - -# It is also possible to use a open .. close pair: -dbobj = SDBM.open("filename", 0666) -#.. do something with dbobj -dbobj.close - -#!/usr/bin/ruby -w -# userstats - generate statistics on who is logged in -# call with usernames as argument to display the totals -# for the given usernames, call with "ALL" to display all users - -require "sdbm" -filename = '/tmp/userstats.db' -SDBM.open(filename, 0666) { |dbobj| - if ARGV.length > 0 - if ARGV[0] == "ALL" - # ARGV is constant, so we need the variable userlist - userlist = dbobj.keys().sort() - else - userlist = ARGV - end - userlist.each { |user| - print "#{user}\t#{dbobj[user]}\n" - } - else - who = `who` - who.split("\n").each { |line| - md = /^(\S+)/.match(line) - raise "Bad line from who: #{line}" unless md - # sdbm stores only strings, so "+=" doesn't work, - # we need to convert them expicitly back to integer. - if dbobj.has_key?(md[0]) - dbobj[md[0]] = dbobj[md[0]].to_i + 1 - else - dbobj[md[0]] = "1" - end - } - end -} - - -# @@PLEAC@@_14.2 -# using open and clear -dbobj = SDBM.open("filename", 0666) -dbobj.clear() -dbobj.close() -# deleting file and recreating it -# the filenames depend on the flavor of dbm you use, -# for example sdbm has two files named filename.pag and filename.dir, -# so you need to delete both files -begin - File.delete("filename") - # raises Exception if not exist - dbobj = SDBM.open("filename", 0666) -rescue - # add error handling here -end - - -# @@PLEAC@@_14.3 -# sdbm2gdbm: converts sdbm database to a gdbm database -require "sdbm" -require "gdbm" - -unless ARGV.length == 2 - fail "usage: sdbm2gdbm infile outfile" -end -infile = ARGV[0] -outfile = ARGV[1] - -sdb = SDBM.open(infile) -gdb = GDBM.open(outfile, 0666) -sdb.each { |key, val| - gdb[key] = val -} -gdb.close -sdb.close - - -# @@PLEAC@@_14.4 -#!/usr/bin/ruby -w -# dbmmerge: merges two dbm databases -require "sdbm" - -unless ARGV.length == 3 - fail "usage: dbmmerge indb1 indb2 outdb" -end -infile1 = ARGV[0] -infile2 = ARGV[0] -outfile = ARGV[2] - -in1 = SDBM.open(infile1, nil) -in2 = SDBM.open(infile2, nil) -outdb = SDBM.open(outfile, 0666) - -[in1, in2].each { |indb| - indb.each { |key, val| - if outdb.has_key?(key) - # decide which value to set. - # set outdb[key] if necessary - else - outdb[key] = val - end - } -} -in1.close -in2.close -outdb.close - - -# @@PLEAC@@_14.7 -# we write a tie method that extends the Array class. -# It reads the file into the memory, executes the code block -# in which you can manipulate the array as needed, and writes -# the array back to the file after the end of the block execution -class Array - def tie(filename, flags) - File.open(filename, flags) { |f| - f.each_line { |line| - self.push(line.chomp) - } - yield - f.rewind - each { |line| - if line - f.puts(line) - else - f.puts "" - end - } - } - end -end - -array = Array.new -array.tie("/tmp/textfile.txt", File::RDWR|File::CREAT) { - array[4] = "a new line 4" -} - -# The tied array can be manipulated like a normal array, -# so there is no need for a special API, and the recno_demo program -# to demonstrate is API is useless - - -# tied array demo: show how to use array with a tied file -filename = "db_file.txt" -lines = Array.new -File.unlink(filename) if File.exists?(filename) -lines.tie(filename, File::RDWR | File::CREAT) { - # first create a textfile to play with - lines[0] = "zero" - lines[1] = "one" - lines[2] = "two" - lines[3] = "three" - lines[4] = "four" - - # print the records in order. - # Opposed to perl, the tied array behaves exactly as a normal array - puts "\nOriginal" - for i in 0..(lines.length-1) - puts "#{i}: #{lines[i]}" - end - - #use push and pop - a = lines.pop - lines.push("last") - puts("The last line was [#{a}]") - - #use shift and unshift - a = lines.shift - lines.unshift("first") - puts("The first line was [#{a}]") - - # add record after record 2 - i = 2 - lines.insert(i + 1, "Newbie") - - # add record before record one - i = 1 - lines.insert(i, "New One") - - # delete record 3 - lines.delete_at(3) - - #now print the records in reverse order - puts "\nReverse" - (lines.length - 1).downto(0){ |i| - puts "#{i}: #{lines[i]}" - } - -} - - -# @@PLEAC@@_14.8 -# example to store complex data in a database -# uses marshall from the standard library -require "sdbm" -db = SDBM.open("pleac14-8-database", 0666) - -# convert the Objects into strings and back by using the Marshal module. -# Most normal objects can be converted out of the box, -# but not special things like procedure objects, -# IO instance variables, singleton objects - -db["Tom Christiansen"] = Marshal.dump(["book author", "tchrist@perl.com"]) -db["Tom Boutell"] = Marshal.dump(["shareware author", -"boutell@boutell.com"]) - -name1 = "Tom Christiansen" -name2 = "Tom Boutell" - -tom1 = Marshal.load(db[name1]) -tom2 = Marshal.load(db[name2]) - -puts "Two Toming: #{tom1} #{tom2}" - -if tom1[0] == tom2[0] && tom1[1] == tom2[1] - puts "You're having runtime fun with one Tom made two." -else - puts "No two Toms are ever alike" -end - -# To change parts of an entry, get the whole entry, change the parts, -# and save the whole entry back -entry = Marshal.load(db["Tom Boutell"]) -entry[0] = "Poet Programmer" -db["Tom Boutell"] = Marshal.dump(entry) -db.close - - -# @@PLEAC@@_14.9 -# example to make data persistent -# uses Marshal from the standard lib -# Stores the data in a simple file, -# see 14.8 on how to store it in a dbm file - -# The BEGIN block is executed before the rest of the script -# we use global variables here because local variables -# will go out of scope and are not accessible from the main script - -BEGIN { - $persistent_store = "persitence.dat" - begin - File.open($persistent_store) do |f| - $stringvariable1 = Marshal.load(f) - $arrayvariable2 = Marshal.load(f) - end - rescue - puts "Can not open #{$persistent_store}" - # Initialisation if this script runs the first time - $stringvariable1 = "" - $arrayvariable2 = [] - end -} - -END { - File.open($persistent_store, "w+") do |f| - Marshal.dump($stringvariable1, f) - Marshal.dump($arrayvariable2, f) - end -} - -# simple test program -puts $stringvariable1 -puts $arrayvariable2 -$stringvariable1 = "Hello World" -$arrayvariable2.push(5) -puts $stringvariable1 -puts $arrayvariable2 - - -# @@PLEAC@@_14.10 -#!/usr/bin/ruby -w -# Ruby has a dbi module with an architecture similar -# to the Perl dbi module: the dbi module provides an unified -# interface and uses specialized drivers for each dbms vendor -# -begin - DBI.connect("DBI:driver:driverspecific", "username", "auth") { - |dbh| - - dbh.do(SQL1) - - dbh.prepare(SQL2){ |sth| - sth.execute - sth.fetch {|row| - # ... - } - } # end of block finishes the statement handle - } # end of block closes the database connection -rescue DBI::DatabaseError => e - puts "dbi error occurred" - puts "Error code: #{e.err}" - puts "Error message: #{e.errstr}" -end - -#!/usr/bin/ruby -w -# dbusers - example for mysql which creates a table, -# fills it with values, retrieves the values back, -# and finally destroys the table. - -require "dbi" - -# replacement for the User::pwnt module -def getpwent - result = [] - File.open("/etc/passwd") {|file| - file.each_line {|line| - next if line.match(/^#/) - cols = line.split(":") - result.push([cols[2], cols[0]]) - } - } - result -end - -begin - DBI.connect("DBI:Mysql:pleacdatabase", "pleac", "pleacpassword") { - |conn| - - conn.do("CREATE TABLE users (uid INT, login CHAR(8))") - - users = getpwent - - conn.prepare("INSERT INTO users VALUES (?,?)") {|sth| - users.each {|entry| - sth.execute(entry[0], entry[1]) - } - } - - conn.execute("SELECT uid, login FROM users WHERE uid < 50") {|sth| - sth.fetch {|row| - puts row.collect {|col| - if col.nil? - "(null)" - else - col - end - }.join(", ") - } - } - - conn.do("DROP TABLE users") - } -rescue DBI::DatabaseError => e - puts "dbi error occurred" - puts "Error code: #{e.err}" - puts "Error message: #{e.errstr}" -end - - -# @@PLEAC@@_15.1 -# This test program demonstrates parsing program arguments. -# It uses the optparse library, which is included with ruby 1.8 -# It handles classic unix style and gnu style options -require 'optparse' - -@debugmode = false -@verbose = false - -ARGV.options do |opts| - opts.banner = "Usage: ruby #{$0} [OPTIONS] INPUTFILES" - - opts.on("-h", "--help", "show this message") { - puts opts - exit - } - # The OptionParser#on method is called with a specification of short - # options, of long options, a data type spezification and user help - # messages for this option. - # The method analyses the given parameter and decides what it is, - # so you can leave out the long option if you don't need it - opts.on("-v", "--[no-]verbose=[FLAG]", TrueClass, "run verbosly") { - |@verbose| # sets @verbose to true or false - } - opts.on("-D", "--DEBUG", TrueClass, "turns on debug mode" ){ - |@debugmode| # sets @debugmode to true - } - opts.on("-c", "--count=NUMBER", Integer, "how many times we do it" ){ - |@count| # sets @count to given integer - } - opts.on("-o", "--output=FILE", String, "file to write output to"){ - |@outputfile| # sets @outputfile to given string - } - opts.parse! -end - -# example to use the options in the main program -puts "Verbose is on" if @verbose -puts "Debugmode is on" if @debugmode -puts "Outfile is #{@outputfile}" if defined? @outputfile -puts "Count is #{@count}" if defined? @count -ARGV.each { |param| - puts "Got parameter #{param}" -} - - -# @@PLEAC@@_15.4 -buf = "\0" * 8 -$stdout.ioctl(0x5413, buf) -ws_row, ws_col, ws_xpixel, ws_ypixel = buf.unpack("S4") - -raise "You must have at least 20 characters" unless ws_col >= 20 -max = 0 -values = (1..5).collect { rand(20) } # generate an array[5] of rand values -for i in values - max = i if max < i -end -ratio = Float(ws_col-12)/max # chars per unit -for i in values - printf "%8.1f %s\n", i, "*" * (ratio*i) -end - -# gives, for example: -# 15.0 ******************************* -# 10.0 ********************* -# 5.0 ********** -# 14.0 ***************************** -# 18.0 ************************************** - - -# @@PLEAC@@_16.1 -output = `program args` # collect output into one multiline string -output = `program args`.split # collect output into array, one line per -element - -readme = IO.popen("ls") -output = "" -while readme.gets do - output += $_ -end -readme.close - -`fsck -y /dev/rsd1a` # BAD AND SCARY in Perl because it's managed by the shell - # I donna in Ruby ... - -# so the "clean and secure" version -readme, writeme = IO.pipe -pid = fork { - # child - $stdout = writeme - readme.close - exec('find', '..') -} -# parent -Process.waitpid(pid, 0) -writeme.close -while readme.gets do - # do something with $_ -end - - -# @@PLEAC@@_16.2 -status = system("xemacs #{myfile}") - -status = system("xemacs", myfile) - -system("cmd1 args | cmd2 | cmd3 >outfile") -system("cmd args outfile 2>errfile") - -# stop if the command fails -raise "$program exited funny: #{$?}" unless system("cmd", "args1", "args2") - -# get the value of the signal sent to the child -# even if it is a SIGINT or SIGQUIT -system(arglist) -raise "program killed by signal #{$?}" if ($? & 127) != 0 - -pid = fork { - trap("SIGINT", "IGNORE") - exec("sleep", "10") -} -trap ("SIGINT") { - puts "Tsk tsk, no process interruptus" -} -Process.waitpid(pid, 0) - -# Ruby doesn't permit to lie to the program called by a 'system'. -# (ie specify what return argv[0] in C, $0 in Perl/Ruby ...) -# A (dirty) way is to create a link (under Unix), run this link and -# erase it. Somebody has a best idea ? - - -# @@PLEAC@@_16.3 -exec("archive *.data") - -exec("archive", "accounting.data") - -exec("archive accounting.data") - - -# @@PLEAC@@_16.4 -# read the output of a program -IO.popen("ls") {|readme| - while readme.gets do - # ... - end -} -# or -readme = IO.popen("ls") -while readme.gets do - # ... -end -readme.close - -# "write" in a program -IO.popen("cmd args","w") {|pipe| - pipe.puts("data") - pipe.puts("foo") -} - -# close wait for the end of the process -read = IO.popen("sleep 10000") # child goes to sleep -read.close # and the parent goes to lala land - -writeme = IO.popen("cmd args", "w") -writeme.puts "hello" # program will get hello\n on STDIN -writeme.close # program will get EOF on STDIN - -# send in a pager (eg less) all output -$stdout = IO.popen("/usr/bin/less","w") -print "huge string\n" * 10000 - - -# @@PLEAC@@_16.5 -#----------------------------- -def head(lines = 20) - pid = open("|-","w") - if pid == nil - return - else - while gets() do - pid.print - lines -= 1 - break if lines == 0 - end - end - exit -end - -head(100) -while gets() do - print -end -#----------------------------- -1: > Welcome to Linux, version 2.0.33 on a i686 - -2: > - -3: > "The software required `Windows 95 or better', - -4: > so I installed Linux." -#----------------------------- -> 1: Welcome to Linux, Kernel version 2.0.33 on a i686 - -> 2: - -> 3: "The software required `Windows 95 or better', - -> 4: so I installed Linux." -#----------------------------- -#!/usr/bin/ruby -# qnumcat - demo additive output filters - -def number() - pid = open("|-","w") - if pid == nil - return - else - while gets() do pid.printf("%d: %s", $., $_); end - end - exit -end - -def quote() - pid = open("|-","w") - if pid == nil - return - else - while gets() do pid.print "> #{$_}" end - end - exit -end - -number() -quote() - -while gets() do - print -end -$stdout.close -exit - - -# @@PLEAC@@_16.6 -ARGV.map! { |arg| - arg =~ /\.(gz|Z)$/ ? "|gzip -dc #{arg}" : arg -} -for file in ARGV - fh = open(file) - while fh.gets() do - # ....... - end -end -#----------------------------- -ARGV.map! { |arg| - arg =~ %r#^\w+://# ? "|GET #{arg}" : arg # -} -for file in ARGV - fh = open(file) - while fh.gets() do - # ....... - end -end -#----------------------------- -pwdinfo = (`domainname` =~ /^(\(none\))?$/) ? '/etc/passwd' : '|ypcat passwd'; -pwd = open(pwdinfo); -#----------------------------- -puts "File, please? "; -file = gets().chomp(); -fh = open(file); - - -# @@PLEAC@@_16.7 -output = `cmd 2>&1` # with backticks -# or -ph = open("|cmd 2>&1") # with an open pipe -while ph.gets() { } # plus a read -#----------------------------- -output = `cmd 2>/dev/null` # with backticks -# or -ph = open("|cmd 2>/dev/null") # with an open pipe -while ph.gets() { } # plus a read -#----------------------------- -output = `cmd 2>&1 1>/dev/null` # with backticks -# or -ph = open("|cmd 2>&1 1>/dev/null") # with an open pipe -while ph.gets() { } # plus a read -#----------------------------- -output = `cmd 3>&1 1>&2 2>&3 3>&-` # with backticks -# or -ph = open("|cmd 3>&1 1>&2 2>&3 3>&-") # with an open pipe -while ph.gets() { } # plus a read -#----------------------------- -system("program args 1>/tmp/program.stdout 2>/tmp/program.stderr") -#----------------------------- -output = `cmd 3>&1 1>&2 2>&3 3>&-` -#----------------------------- -fd3 = fd1 -fd1 = fd2 -fd2 = fd3 -fd3 = undef -#----------------------------- -system("prog args 1>tmpfile 2>&1") -system("prog args 2>&1 1>tmpfile") -#----------------------------- -# system ("prog args 1>tmpfile 2>&1") -fd1 = "tmpfile" # change stdout destination first -fd2 = fd1 # now point stderr there, too -#----------------------------- -# system("prog args 2>&1 1>tmpfile") -fd2 = fd1 # stderr same destination as stdout -fd1 = "tmpfile" # but change stdout destination -#----------------------------- -# It is often better not to rely on the shell, -# because of portability, possible security problems -# and bigger resource usage. So, it is often better to use the open3 library. -# See below for an example. -# opening stdin, stdout, stderr -require "open3" -stdin, stdout, stderr = Open3.popen('cmd') - - -# @@PLEAC@@_16.8 -#----------------------------- -# Contrary to perl, we don't need to use a module in Ruby -fh = Kernel.open("|" + program, "w+") -fh.puts "here's your input\n" -output = fh.gets() -fh.close() -#----------------------------- -Kernel.open("|program"),"w+") # RIGHT ! -#----------------------------- -# Ruby has already object methods for I/O handles -#----------------------------- -begin - fh = Kernel.open("|" + program_and_options, "w+") -rescue - if ($@ ~= /^open/) - $stderr.puts "open failed : #{$!} \n #{$@} \n" - break - end - raise # reraise unforseen exception -end - - -# @@PLEAC@@_16.13 -#% kill -l -#HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE -#ALRM TERM CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM -#PROF WINCH POLL PWR -#----------------------------- -#% ruby -e 'puts Signal.list.keys.join(" ")' -#PWR USR1 BUS USR2 TERM SEGV KILL POLL STOP SYS TRAP IOT HUP INT # -#WINCH XCPU TTIN CLD TSTP FPE IO TTOU PROF CHLD CONT PIPE ABRT -#VTALRM QUIT ILL XFSZ URG ALRM -#----------------------------- -# After that, the perl script create an hash equivalent to Signal.list, -# and an array. The array can be obtained by : -signame = [] -Signal.list.each { |name, i| signame[i] = name } - - -# @@PLEAC@@_16.14 -Process.kill(9, pid) # send $pid a signal 9 -Process.kill(-1, Process.getpgrp()) # send whole job a signal 1 -Process.kill("USR1", $$) # send myself a SIGUSR1 -Process.kill("HUP", pid1, pid2, pid3) # send a SIGHUP to processes in @pids -#----------------------------- -begin - Process.kill(0, minion) - puts "#{minion} is alive!" -rescue Errno::EPERM # changed uid - puts "#{minion} has escaped my control!"; -rescue Errno::ESRCH - puts "#{minion} is deceased."; # or zombied -rescue - puts "Odd; I couldn't check the status of #{minion} : #{$!}" -end - - -# @@PLEAC@@_16.15 -Kernel.trap("QUIT", got_sig_quit) # got_sig_quit = Proc.new { puts "Quit\n" } -trap("PIPE", "got_sig_quit") # def got_sig_pipe ... -trap("INT") { ouch++ } # increment ouch for every SIGINT -#----------------------------- -trap("INT", "IGNORE") # ignore the signal INT -#----------------------------- -trap("STOP", "DEFAULT") # restore default STOP signal handling - - -# @@PLEAC@@_16.16 -# the signal handler -def ding - trap("INT", "ding") - puts "\aEnter your name!" -end - -# prompt for name, overriding SIGINT -def get_name - save = trap("INT", "ding") - - puts "Kindly Stranger, please enter your name: " - name = gets().chomp() - trap("INT", save) - name -end - - -# @@PLEAC@@_16.21 -# implemented thanks to http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/1760 -require 'timeout' - -# we'll do something vastly more useful than cookbook to demonstrate timeouts -begin - timeout(5) { - waitsec = rand(10) - puts "Let's see if a sleep of #{waitsec} seconds is longer than 5 seconds..." - system("sleep #{waitsec}") - } - puts "Timeout didn't occur" -rescue Timeout::Error - puts "Timed out!" -end - - -# @@PLEAC@@_17.1 -# A basic TCP client connection -require 'socket' -begin - t = TCPSocket.new('www.ruby-lang.org', 'www') -rescue - puts "error: #{$!}" -else - # ... do something with the socket - t.print "GET / HTTP/1.0\n\n" - answer = t.gets(nil) - # and terminate the connection when we're done - t.close -end - -# Using the evil low level socket API -require 'socket' -# create a socket -s = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) -# build the address of the remote machine -sockaddr_server = [Socket::AF_INET, 80, - Socket.gethostbyname('www.ruby-lang.org')[3], - 0, 0].pack("snA4NN") -# connect -begin - s.connect(sockaddr_server) -rescue - puts "error: #{$!}" -else - # ... do something with the socket - s.print "GET / HTTP/1.0\n\n" - # and terminate the connection when we're done - s.close -end - -# TCP connection with management of error (DNS) -require 'socket' -begin - client = TCPSocket.new('does not exists', 'www') -rescue - puts "error: #{$!}" -end - -# TCP connection with a time out -require 'socket' -require 'timeout' -begin - timeout(1) do #the server has one second to answer - client = TCPSocket.new('www.host.com', 'www') - end -rescue - puts "error: #{$!}" -end - - -# @@PLEAC@@_17.12 -require 'socket' - -class Preforker - attr_reader (:child_count) - - def initialize(prefork, max_clients_per_child, port, client_handler) - @prefork = prefork - @max_clients_per_child = max_clients_per_child - @port = port - @child_count = 0 - - @reaper = proc { - trap('CHLD', @reaper) - pid = Process.wait - @child_count -= 1 - } - - @huntsman = proc { - trap('CHLD', 'IGNORE') - trap('INT', 'IGNORE') - Process.kill('INT', 0) - exit - } - - @client_handler=client_handler - end - - def child_handler - trap('INT', 'EXIT') - @client_handler.setUp - # wish: sigprocmask UNblock SIGINT - @max_clients_per_child.times { - client = @server.accept or break - @client_handler.handle_request(client) - client.close - } - @client_handler.tearDown - end - - def make_new_child - # wish: sigprocmask block SIGINT - @child_count += 1 - pid = fork do - child_handler - end - # wish: sigprocmask UNblock SIGINT - end - - def run - @server = TCPserver.open(@port) - trap('CHLD', @reaper) - trap('INT', @huntsman) - loop { - (@prefork - @child_count).times { |i| - make_new_child - } - sleep .1 - } - end -end - -#----------------------------- -#!/usr/bin/ruby - -require 'Preforker' - -class ClientHandler - def setUp - end - - def tearDown - end - - def handle_request(client) - # do stuff - end -end - -server = Preforker.new(1, 100, 3102, ClientHandler.new) -server.run - - -# @@PLEAC@@_18.2 -require 'net/ftp' - -begin - ftp = Net::FTP::new("ftp.host.com") - ftp.login(username,password) - ftp.chdir(directory) - ftp.get(filename) - ftp.put(filename) -rescue Net::FTPError - $stderr.print "FTP failed: " + $! -ensure - ftp.close() if ftp -end - -# A better solution for a local use could be : -Net::FTP::new("ftp.host.com") do |ftp| - ftp.login(username,password) - ftp.chdir(directory) - ftp.get(filename) - ftp.put(filename) -end - -# If you have only one file to get, there is a simple solution : -require 'open-uri' -open("ftp://www.ruby-lang.org/path/filename") do |fh| - # read from filehandle fh -end -#-------------------------------------------- -# to wait a defined time for the connection, -# use the timeout module -require 'timeout' -begin - timeout(30){ - ftp = Net::FTP::new("ftp.host.com") - ftp.debug_mode = true - } -rescue Net::FTPError - $stderr.puts "Couldn't connect." -rescue Timeout::Error - $stderr.puts "Timeout while connecting to server." -end - -begin - ftp.login() -rescue Net::FTPError - $stderr.print "Couldn't authentificate.\n" -end - -begin - ftp.login(username) -rescue Net::FTPError - $stderr.print "Still couldn't authenticate.\n" -end - -begin - ftp.login(username, password) -rescue Net::FTPError - $stderr.print "Couldn't authenticate, even with explicit - username and password.\n" -end - -begin - ftp.login(username, password, account) -rescue Net::FTPError - $stderr.print "No dice. It hates me.\n" -end -#----------------------------- -ftp.put(localfile, remotefile) -#----------------------------- -# Sending data from STDIN is not directly supported -# by the ftp library module. A possible way to do it is to use the -# storlines method directly to send raw commands to the ftp server. -#----------------------------- -ftp.get(remotefile, localfile) -#----------------------------- -ftp.get(remotefile) { |data| puts data } -#----------------------------- -ftp.chdir("/pub/ruby") -print "I'm in the directory ", ftp.pwd(), "\n" -#----------------------------- -ftp.mkdir("/pub/ruby/new_dir") -#----------------------------- -lines = ftp.ls("/pub/ruby/") -# => ["drwxr-xr-x 2 matz users 4096 July 17 1998 1.0", ... ] - -latest = ftp.dir("/pub/ruby/*.tgz").sort.last - -ftp.nlst("/pub/ruby") -# => ["/pub/ruby/1.0", ... ] -#----------------------------- -ftp.quit() - - -# @@PLEAC@@_18.6 -require 'net/telnet' -t = Net::Telnet::new( "Timeout" => 10, - "Prompt" => /%/, - "Host" => host ) -t.login(username, password) -files = t.cmd("ls") -t.print("top") -process_string = t.waitfor(/\d+ processes/) -t.close -#----------------------------- -/[$%#>] \z/n -#----------------------------- -# In case of an error, the telnet module throws an exception. -# For control of the behavior in case of an error, -# you just need to catch the exceptions and do your custom -# error handling. -#----------------------------- -begin - telnet.login(username, password) -rescue TimeoutError - fail "Login failed !\n" -end -#----------------------------- -telnet.waitfor('/--more--/') -#----------------------------- -telnet.waitfor(String => 'greasy smoke', Timeout => 30) - - -# @@PLEAC@@_18.7 -require 'ping' - -puts "#{host} is alive.\n" if Ping.pingecho(host); -#----------------------------- -# the ping module only use TCP ping, not ICMP even if we are root -if Ping.pingecho("kingkong.com") - puts "The giant ape lives!\n"; -else - puts "All hail mighty Gamera, friend of children!\n"; -end - - -# @@PLEAC@@_19.1 -#!/usr/local/bin/ruby -w -# hiweb - load CGI class to decode information given by web server - -require 'cgi' - -cgi = CGI.new('html3') - -# get a parameter from a form -value = cgi.params['PARAM_NAME'][0] - -# output a document -cgi.out { - cgi.html { - cgi.head { cgi.title { "Howdy there!" } } + - cgi.body { cgi.p { "You typed: " + cgi.tt { - CGI.escapeHTML(value) } } } - } -} - -require 'cgi' -cgi = CGI.new -who = cgi.param["Name"][0] # first param in list -phone = cgi.param["Number"][0] -picks = cgi.param["Choices"] # complete list - -print cgi.header( 'type' => 'text/plain', - 'expires' => Time.now + (3 * 24 * 60 * 60) ) - - -# @@PLEAC@@_19.3 -#!/usr/local/bin/ruby -w -# webwhoami - show web user's id -require 'etc' -print "Content-Type: text/plain\n\n" -print "Running as " + Etc.getpwuid.name + "\n" - -# % ruby -wc cgi-script # just check syntax - -# % ruby -w cgi-script # params from stdin -# (offline mode: enter name=value pairs on standard input) -# name=joe -# number=10 -# ^D - -# % ruby -w cgi-script name=joe number=10 # run with mock form input -# % ruby -d cgi-script name=joe number=10 # ditto, under the debugger - -# POST method script in csh -# % (setenv HTTP_METHOD POST; ruby -w cgi-script name=joe number=10) -# POST method script in sh -# % HTTP_METHOD=POST perl -w cgi-script name=joe number=10 - - -# @@PLEAC@@_19.4 -# ruby has several security levels, the level "1" is similar to perls taint mode. -# It can be switched on by providing the -T command line parameter -# or by setting $SAFE to 1. Setting $SAFE to 2,3 or 4 restricts possible -# harmful operations further. - -#!/usr/bin/ruby -T -$SAFE = 1 -File.open(ARGV[0], "w") -# ruby warns with: -# taint1.rb:2:in `initialize': Insecure operation - initialize (SecurityError) - -$SAFE = 1 -file = ARGV[0] -unless /^([\w.-]+)$/.match(file) - raise "filename #{file} has invalid characters" -end -file = $1 -# In ruby, even the back reference from a regular expression stays tainted. -# you need to explicitly untaint the variable: -file.untaint -File.open(file, "w") - -# Race condition exists like in perl: -unless File.exists(filename) # Wrong because of race condition - File.open(filename, "w") -end - - - -# @@PLEAC@@_19.10 -preference_value = cgi.cookies["preference name"][0] - -packed_cookie = CGI::Cookie.new("name" => "preference name", - "value" => "whatever you'd like", - "expires" => Time.local(Time.now.year + 2, - Time.now.mon, Time.now.day, Time.now.hour, Time.now.min, Time.now.sec) ) - -cgi.header("cookie" => [packed_cookie]) - -#!/usr/local/bin/ruby -w -# ic_cookies - sample CGI script that uses a cookie -require 'cgi' - -cgi = CGI.new('html3') - -cookname = "favorite ice cream" -favorite = cgi.params["flavor"][0] -tasty = cgi.cookies[cookname][0] || 'mint' - -unless favorite - cgi.out { - cgi.html { - cgi.head { cgi.title { "Ice Cookies" } } + - cgi.body { - cgi.h1 { "Hello Ice Cream" } + - cgi.hr + - cgi.form { - cgi.p { "Please select a flavor: " + - cgi.text_field("flavor", tasty ) } - } + - cgi.hr - } - } - } -else - cookie = CGI::Cookie.new( "name" => cookname, - "value" => favorite, - "expires" => Time.local(Time.now.year + 2, -Time.now.mon, Time.now.day, Time.now.hour, Time.now.min, Time.now.sec) ) - cgi.out("cookie" => [cookie]) { - cgi.html { - cgi.head { cgi.title { "Ice Cookies" } } + - cgi.body { - cgi.h1 { "Hello Ice Cream" } + - cgi.p { "You chose as your favorite flavor `#{favorite}'." } - } - } - } -end - - -# @@PLEAC@@_20.9 -def templatefile(filename, fillings) - aFile = File.new(filename, "r") - text = aFile.read() - aFile.close() - pattern = Regexp.new('%%(.*?)%%') - text.gsub!(pattern) { - fillings[$1] || "" - } - text -end - -fields = { - 'username' => whats_his_name, - 'count' => login_count, - 'total' => minutes_used -} -puts templatefile('simple.template', fields) - -# @@INCOMPLETE@@ -# An example using databases is missing diff --git a/test/ruby/example.out.raydebug b/test/ruby/example.out.raydebug index 609c346..90e2f2b 100644 --- a/test/ruby/example.out.raydebug +++ b/test/ruby/example.out.raydebug @@ -3786,6285 +3786,4 @@ reserved(if) global_variable($0) operator(==) pre_constant(__FILE__) reserved(break) reserved(if) ident(c)operator(.)ident(typ) operator(==) constant(TokenConsts)operator(::)constant(EOP) reserved(end) reserved(end) -comment(# -*- ruby -*-) - -comment(# Local variables:) -comment(# indent-tabs-mode: nil) -comment(# ruby-indent-level: 4) -comment(# End:) - -comment(# @@PLEAC@@_NAME) -comment(# @@SKIP@@ Ruby) - -comment(# @@PLEAC@@_WEB) -comment(# @@SKIP@@ http://www.ruby-lang.org) - - -comment(# @@PLEAC@@_1.0) -ident(string) operator(=) string comment(# two characters, \\ and an n) -ident(string) operator(=) string comment(# literal single quotes) - -ident(string) operator(=) string comment(# a "newline" character) -ident(string) operator(=) string comment(# literal double quotes) - -ident(string) operator(=) string comment(# literal single quotes) - -ident(string) operator(=) string comment(# literal single quotes) -ident(string) operator(=) string comment(# literal single quotes) -ident(string) operator(=) string comment(# literal single quotes) -ident(string) operator(=) string)> comment(# literal single quotes) - -ident(a) operator(=) stringstring - - -comment(# @@PLEAC@@_1.1) -ident(value) operator(=) ident(string)operator([)ident(offset)operator(,)ident(count)operator(]) -ident(value) operator(=) ident(string)operator([)ident(offset)operator(..)operator(-)integer(1)operator(]) - -ident(string)operator([)ident(offset)operator(,)ident(count)operator(]) operator(=) ident(newstring) -ident(string)operator([)ident(offset)operator(..)operator(-)integer(1)operator(]) operator(=) ident(newtail) - -comment(# in Ruby we can also specify intervals by their two offsets) -ident(value) operator(=) ident(string)operator([)ident(offset)operator(..)ident(offs2)operator(]) -ident(string)operator([)ident(offset)operator(..)ident(offs2)operator(]) operator(=) ident(newstring) - -ident(leading)operator(,) ident(s1)operator(,) ident(s2)operator(,) ident(trailing) operator(=) ident(data)operator(.)ident(unpack)operator(()stringoperator(\)) - -ident(fivers) operator(=) ident(string)operator(.)ident(unpack)operator(()string operator(*) operator(()ident(string)operator(.)ident(length)operator(/)integer(5)operator(\))operator(\)) - -ident(chars) operator(=) ident(string)operator(.)ident(unpack)operator(()string operator(*) ident(string)operator(.)ident(length)operator(\)) - -ident(string) operator(=) string -comment(# +012345678901234567890 Indexing forwards (left to right\)) -comment(# 109876543210987654321- Indexing backwards (right to left\)) -comment(# note that 0 means 10 or 20, etc. above) - -ident(first) operator(=) ident(string)operator([)integer(0)operator(,) integer(1)operator(]) comment(# "T") -ident(start) operator(=) ident(string)operator([)integer(5)operator(,) integer(2)operator(]) comment(# "is") -ident(rest) operator(=) ident(string)operator([)integer(13)operator(..)operator(-)integer(1)operator(]) comment(# "you have") -ident(last) operator(=) ident(string)operator([)operator(-)integer(1)operator(,) integer(1)operator(]) comment(# "e") -ident(end_) operator(=) ident(string)operator([)operator(-)integer(4)operator(..)operator(-)integer(1)operator(]) comment(# "have") -ident(piece) operator(=) ident(string)operator([)operator(-)integer(8)operator(,) integer(3)operator(]) comment(# "you") - -ident(string)operator([)integer(5)operator(,) integer(2)operator(]) operator(=) string comment(# change "is" to "wasn't") -ident(string)operator([)operator(-)integer(12)operator(..)operator(-)integer(1)operator(]) operator(=) string comment(# "This wasn't wondrous") -ident(string)operator([)integer(0)operator(,) integer(1)operator(]) operator(=) string comment(# delete first character) -ident(string)operator([)operator(-)integer(10)operator(..)operator(-)integer(1)operator(]) operator(=) string comment(# delete last 10 characters) - -reserved(if) ident(string)operator([)operator(-)integer(10)operator(..)operator(-)integer(1)operator(]) operator(=)operator(~) regexp - ident(puts) string -reserved(end) - -ident(string)operator([)integer(0)operator(,) integer(5)operator(])operator(.)ident(gsub!)operator(()regexpoperator(,) stringoperator(\)) - -ident(a) operator(=) string -ident(a)operator([)integer(0)operator(,) integer(1)operator(])operator(,) ident(a)operator([)operator(-)integer(1)operator(,) integer(1)operator(]) operator(=) ident(a)operator([)operator(-)integer(1)operator(,) integer(1)operator(])operator(,) ident(a)operator([)integer(0)operator(,) integer(1)operator(]) - -ident(a) operator(=) string -ident(b) operator(=) ident(a)operator(.)ident(unpack)operator(()stringoperator(\)) - -ident(b)operator(,) ident(c) operator(=) ident(a)operator(.)ident(unpack)operator(()stringoperator(\)) -ident(puts) stringchar(\\n)inlinechar(\\n)delimiter(")> - -reserved(def) method(cut2fmt)operator(()operator(*)ident(args)operator(\)) - ident(template) operator(=) string - ident(lastpos) operator(=) integer(1) - reserved(for) ident(place) reserved(in) ident(args) - ident(template) operator(+=) string operator(+) operator(()ident(place) operator(-) ident(lastpos)operator(\))operator(.)ident(to_s) operator(+) string - ident(lastpos) operator(=) ident(place) - reserved(end) - ident(template) operator(+=) string - reserved(return) ident(template) -reserved(end) - -ident(fmt) operator(=) ident(cut2fmt)operator(()integer(8)operator(,) integer(14)operator(,) integer(20)operator(,) integer(26)operator(,) integer(30)operator(\)) - - -comment(# @@PLEAC@@_1.2) -comment(# careful! "b is true" doesn't mean "b != 0" (0 is true in Ruby\)) -comment(# thus no problem of "defined" later since only nil is false) -comment(# the following sets to `c' if `b' is nil or false) -ident(a) operator(=) ident(b) operator(||) ident(c) - -comment(# if you need Perl's behaviour (setting to `c' if `b' is 0\) the most) -comment(# effective way is to use Numeric#nonzero? (thanks to Dave Thomas!\)) -ident(a) operator(=) ident(b)operator(.)ident(nonzero?) operator(||) ident(c) - -comment(# you will still want to use defined? in order to test) -comment(# for scope existence of a given object) -ident(a) operator(=) reserved(defined?)operator(()ident(b)operator(\)) operator(?) ident(b) operator(:) ident(c) - -ident(dir) operator(=) pre_constant(ARGV)operator(.)ident(shift) operator(||) string - - -comment(# @@PLEAC@@_1.3) -ident(v1)operator(,) ident(v2) operator(=) ident(v2)operator(,) ident(v1) - -ident(alpha)operator(,) ident(beta)operator(,) ident(production) operator(=) string -ident(alpha)operator(,) ident(beta)operator(,) ident(production) operator(=) ident(beta)operator(,) ident(production)operator(,) ident(alpha) - - -comment(# @@PLEAC@@_1.4) -ident(num) operator(=) ident(char)operator([)integer(0)operator(]) -ident(char) operator(=) ident(num)operator(.)ident(chr) - -comment(# Ruby also supports having a char from character constant) -ident(num) operator(=) integer(?r) - -ident(char) operator(=) ident(sprintf)operator(()stringoperator(,) ident(num)operator(\)) -ident(printf)operator(()stringoperator(,) ident(num)operator(,) ident(num)operator(\)) - -ident(ascii) operator(=) ident(string)operator(.)ident(unpack)operator(()stringoperator(\)) -ident(string) operator(=) ident(ascii)operator(.)ident(pack)operator(()stringoperator(\)) - -ident(hal) operator(=) string -ident(ascii) operator(=) ident(hal)operator(.)ident(unpack)operator(()stringoperator(\)) -comment(# We can't use Array#each since we can't mutate a Fixnum) -ident(ascii)operator(.)ident(collect!) operator({) operator(|)ident(i)operator(|) - ident(i) operator(+) integer(1) comment(# add one to each ASCII value) -operator(}) -ident(ibm) operator(=) ident(ascii)operator(.)ident(pack)operator(()stringoperator(\)) -ident(puts) ident(ibm) - - -comment(# @@PLEAC@@_1.5) -ident(array) operator(=) ident(string)operator(.)ident(split)operator(()stringoperator(\)) - -ident(array) operator(=) ident(string)operator(.)ident(unpack)operator(()stringoperator(\)) - -ident(string)operator(.)ident(scan)operator(()regexpoperator(\)) operator({) operator(|)ident(b)operator(|) - comment(# do something with b) -operator(}) - -ident(string) operator(=) string -ident(print) stringoperator(,) ident(string)operator(.)ident(split)operator(()stringoperator(\))operator(.)ident(uniq)operator(.)ident(sort)operator(,) string - -ident(sum) operator(=) integer(0) -reserved(for) ident(ascval) reserved(in) ident(string)operator(.)ident(unpack)operator(()stringoperator(\)) comment(# or use Array#each for a pure OO style :\)) - ident(sum) operator(+=) ident(ascval) -reserved(end) -ident(puts) stringdelimiter(")> comment(# since Ruby will go Bignum if necessary) - -comment(# @@INCLUDE@@ include/ruby/slowcat.rb) - - -comment(# @@PLEAC@@_1.6) -ident(revbytes) operator(=) ident(string)operator(.)ident(reverse) - -ident(revwords) operator(=) ident(string)operator(.)ident(split)operator(()stringoperator(\))operator(.)ident(reverse)operator(.)ident(join)operator(()stringoperator(\)) - -ident(revwords) operator(=) ident(string)operator(.)ident(split)operator(()regexpoperator(\))operator(.)ident(reverse)operator(.)ident(join) - -comment(# using the fact that IO is Enumerable, you can directly "select" it) -ident(long_palindromes) operator(=) constant(File)operator(.)ident(open)operator(()stringoperator(\))operator(.) - ident(select) operator({) operator(|)ident(w)operator(|) ident(w)operator(.)ident(chomp!)operator(;) ident(w)operator(.)ident(reverse) operator(==) ident(w) operator(&&) ident(w)operator(.)ident(length) operator(>) integer(5) operator(}) - - -comment(# @@PLEAC@@_1.7) -reserved(while) ident(string)operator(.)ident(sub!)operator(()stringoperator(\)) operator({) string operator(*) operator(()global_variable($&)operator(.)ident(length) operator(*) integer(8) operator(-) global_variable($`)operator(.)ident(length) operator(%) integer(8)operator(\)) operator(}) -reserved(end) - - -comment(# @@PLEAC@@_1.8) -stringoperator(.)ident(gsub)operator(()regexpoperator(\)) operator({) ident(eval)operator(()global_variable($1)operator(\)) operator(}) - -ident(rows)operator(,) ident(cols) operator(=) integer(24)operator(,) integer(80) -ident(text) operator(=) string -ident(text)operator(.)ident(gsub!)operator(()regexpoperator(\)) operator({) ident(eval)operator(()stringdelimiter(")>operator(\)) operator(}) -ident(puts) ident(text) - -stringoperator(.)ident(gsub)operator(()regexpoperator(\)) operator({) integer(2) operator(*) global_variable($&)operator(.)ident(to_i) operator(}) - - -comment(# @@PLEAC@@_1.9) -ident(e) operator(=) stringoperator(.)ident(upcase) -ident(e)operator(.)ident(downcase!) -ident(e)operator(.)ident(capitalize!) - -stringoperator(.)ident(gsub!)operator(()regexpoperator(\)) operator({) global_variable($&)operator(.)ident(capitalize) operator(}) - - -comment(# @@PLEAC@@_1.10) -stringcontent( guanacos.)delimiter(")> -ident(print) stringoperator(,) ident(n)operator(+)integer(1)operator(,) string - - -comment(# @@PLEAC@@_1.11) -ident(var) operator(=) stringoperator(.)ident(gsub)operator(()regexpoperator(,) stringoperator(\))string - - -comment(# @@PLEAC@@_1.12) -ident(string) operator(=) stringoperator(+) - stringoperator(+) - stringoperator(+) - string - -reserved(def) method(wrap)operator(()ident(str)operator(,) ident(max_size)operator(\)) - ident(all) operator(=) operator([)operator(]) - ident(line) operator(=) string - reserved(for) ident(l) reserved(in) ident(str)operator(.)ident(split) - reserved(if) operator(()ident(line)operator(+)ident(l)operator(\))operator(.)ident(length) operator(>)operator(=) ident(max_size) - ident(all)operator(.)ident(push)operator(()ident(line)operator(\)) - ident(line) operator(=) string - reserved(end) - ident(line) operator(+=) ident(line) operator(==) string operator(?) ident(l) operator(:) string operator(+) ident(l) - reserved(end) - ident(all)operator(.)ident(push)operator(()ident(line)operator(\))operator(.)ident(join)operator(()stringoperator(\)) -reserved(end) - -ident(print) ident(wrap)operator(()ident(string)operator(,) integer(20)operator(\)) -comment(#=> Folding and) -comment(#=> splicing is the) -comment(#=> work of an editor,) -comment(#=> not a mere) -comment(#=> collection of) -comment(#=> silicon and mobile) -comment(#=> electrons!) - - -comment(# @@PLEAC@@_1.13) -ident(string) operator(=) string -ident(string)operator(.)ident(gsub)operator(()regexpoperator(\)) operator({) stringoperator(+)global_variable($&) operator(}) -ident(string)operator(.)ident(gsub)operator(()regexpoperator(,) stringoperator(\)) -ident(string)operator(.)ident(gsub)operator(()regexpoperator(\)) operator({) stringoperator(+)global_variable($&) operator(}) -stringoperator(.)ident(gsub)operator(()regexpoperator(\)) operator({) stringoperator(+)global_variable($&) operator(}) comment(# no function like quotemeta?) - - -comment(# @@PLEAC@@_1.14) -ident(string)operator(.)ident(strip!) - - -comment(# @@PLEAC@@_1.15) -reserved(def) method(parse_csv)operator(()ident(text)operator(\)) - ident(new) operator(=) ident(text)operator(.)ident(scan)operator(()regexpoperator(\)) - ident(new) operator(<<) pre_constant(nil) reserved(if) ident(text)operator([)operator(-)integer(1)operator(]) operator(==) integer(?,) - ident(new)operator(.)ident(flatten)operator(.)ident(compact) -reserved(end) - -ident(line) operator(=) string)> -ident(fields) operator(=) ident(parse_csv)operator(()ident(line)operator(\)) -ident(fields)operator(.)ident(each_with_index) operator({) operator(|)ident(v)operator(,)ident(i)operator(|) - ident(print) stringcontent( : )inlinechar(\\n)delimiter(")>operator(;) -operator(}) - - -comment(# @@PLEAC@@_1.16) -comment(# Use the soundex.rb Library from Michael Neumann.) -comment(# http://www.s-direktnet.de/homepages/neumann/rb_prgs/Soundex.rb) -ident(require) string - -ident(code) operator(=) constant(Text)operator(::)constant(Soundex)operator(.)ident(soundex)operator(()ident(string)operator(\)) -ident(codes) operator(=) constant(Text)operator(::)constant(Soundex)operator(.)ident(soundex)operator(()ident(array)operator(\)) - -comment(# substitution function for getpwent(\):) -comment(# returns an array of user entries,) -comment(# each entry contains the username and the full name) -reserved(def) method(login_names) - ident(result) operator(=) operator([)operator(]) - constant(File)operator(.)ident(open)operator(()stringoperator(\)) operator({) operator(|)ident(file)operator(|) - ident(file)operator(.)ident(each_line) operator({) operator(|)ident(line)operator(|) - reserved(next) reserved(if) ident(line)operator(.)ident(match)operator(()regexpoperator(\)) - ident(cols) operator(=) ident(line)operator(.)ident(split)operator(()stringoperator(\)) - ident(result)operator(.)ident(push)operator(()operator([)ident(cols)operator([)integer(0)operator(])operator(,) ident(cols)operator([)integer(4)operator(])operator(])operator(\)) - operator(}) - operator(}) - ident(result) -reserved(end) - -ident(puts) string -ident(user) operator(=) constant(STDIN)operator(.)ident(gets) -ident(user)operator(.)ident(chomp!) -ident(exit) reserved(unless) ident(user) -ident(name_code) operator(=) constant(Text)operator(::)constant(Soundex)operator(.)ident(soundex)operator(()ident(user)operator(\)) - -ident(splitter) operator(=) constant(Regexp)operator(.)ident(new)operator(()stringoperator(\)) -reserved(for) ident(username)operator(,) ident(fullname) reserved(in) ident(login_names) reserved(do) - ident(firstname)operator(,) ident(lastname) operator(=) ident(splitter)operator(.)ident(match)operator(()ident(fullname)operator(\))operator([)integer(1)operator(,)integer(2)operator(]) - reserved(if) ident(name_code) operator(==) constant(Text)operator(::)constant(Soundex)operator(.)ident(soundex)operator(()ident(username)operator(\)) - operator(||) ident(name_code) operator(==) constant(Text)operator(::)constant(Soundex)operator(.)ident(soundex)operator(()ident(firstname)operator(\)) - operator(||) ident(name_code) operator(==) constant(Text)operator(::)constant(Soundex)operator(.)ident(soundex)operator(()ident(lastname)operator(\)) - reserved(then) - ident(puts) stringcontent(: )inlinecontent( )inlinedelimiter(")> - reserved(end) -reserved(end) - - -comment(# @@PLEAC@@_1.17) -comment(# @@INCLUDE@@ include/ruby/fixstyle.rb) - - -comment(# @@PLEAC@@_1.18) -comment(# @@INCLUDE@@ include/ruby/psgrep.rb) - - -comment(# @@PLEAC@@_2.1) -comment(# Matz tells that you can use Integer(\) for strict checked conversion.) -constant(Integer)operator(()stringoperator(\)) -comment(#=> `Integer': invalid value for Integer: "abc" (ArgumentError\)) -constant(Integer)operator(()stringoperator(\)) -comment(#=> 567) - -comment(# You may use Float(\) for floating point stuff) -constant(Integer)operator(()stringoperator(\)) -comment(#=> `Integer': invalid value for Integer: "56.7" (ArgumentError\)) -constant(Float)operator(()stringoperator(\)) -comment(#=> 56.7) - -comment(# You may also use a regexp for that) -reserved(if) ident(string) operator(=)operator(~) regexp - ident(p) string -reserved(else) - ident(p) string -reserved(end) - -reserved(if) ident(string) operator(=)operator(~) regexp - ident(p) string -reserved(else) - ident(p) string -reserved(end) - - -comment(# @@PLEAC@@_2.2) -comment(# equal(num1, num2, accuracy\) : returns true if num1 and num2 are) -comment(# equal to accuracy number of decimal places) -reserved(def) method(equal)operator(()ident(i)operator(,) ident(j)operator(,) ident(a)operator(\)) - ident(sprintf)operator(()stringcontent(g)delimiter(")>operator(,) ident(i)operator(\)) operator(==) ident(sprintf)operator(()stringcontent(g)delimiter(")>operator(,) ident(j)operator(\)) -reserved(end) - -ident(wage) operator(=) integer(536) comment(# $5.36/hour) -ident(week) operator(=) integer(40) operator(*) ident(wage) comment(# $214.40) -ident(printf)operator(()stringoperator(,) ident(week)operator(/)float(100.0)operator(\)) - - -comment(# @@PLEAC@@_2.3) -ident(num)operator(.)ident(round) comment(# rounds to integer) - -ident(a) operator(=) float(0.255) -ident(b) operator(=) ident(sprintf)operator(()stringoperator(,) ident(a)operator(\)) -ident(print) stringchar(\\n)content(Rounded: )inlinechar(\\n)delimiter(")> -ident(printf) stringchar(\\n)content(Rounded: %.2f)char(\\n)delimiter(")>operator(,) ident(a) - -ident(print) string -ident(a) operator(=) operator([) float(3.3) operator(,) float(3.5) operator(,) float(3.7)operator(,) operator(-)float(3.3) operator(]) -reserved(for) ident(n) reserved(in) ident(a) - ident(printf)operator(()stringoperator(,) comment(# at least I don't fake my output :\)) - ident(n)operator(,) ident(n)operator(.)ident(to_i)operator(,) ident(n)operator(.)ident(floor)operator(,) ident(n)operator(.)ident(ceil)operator(\)) -reserved(end) - - -comment(# @@PLEAC@@_2.4) -reserved(def) method(dec2bin)operator(()ident(n)operator(\)) - operator([)ident(n)operator(])operator(.)ident(pack)operator(()stringoperator(\))operator(.)ident(unpack)operator(()stringoperator(\))operator([)integer(0)operator(])operator(.)ident(sub)operator(()regexpoperator(,) stringoperator(\)) -reserved(end) - -reserved(def) method(bin2dec)operator(()ident(n)operator(\)) - operator([)operator(()stringoperator(*)integer(32)operator(+)ident(n)operator(.)ident(to_s)operator(\))operator([)operator(-)integer(32)operator(..)operator(-)integer(1)operator(])operator(])operator(.)ident(pack)operator(()stringoperator(\))operator(.)ident(unpack)operator(()stringoperator(\))operator([)integer(0)operator(]) -reserved(end) - - -comment(# @@PLEAC@@_2.5) -reserved(for) ident(i) reserved(in) ident(x) operator(..) ident(y) - comment(# i is set to every integer from x to y, inclusive) -reserved(end) - -ident(x)operator(.)ident(step)operator(()ident(y)operator(,)integer(7)operator(\)) operator({) operator(|)ident(i)operator(|) - comment(# i is set to every integer from x to y, stepsize = 7) -operator(}) - -ident(print) string -operator(()integer(0)operator(..)integer(2)operator(\))operator(.)ident(each) operator({) operator(|)ident(i)operator(|) - ident(print) ident(i)operator(,) string -operator(}) -ident(print) string - - -comment(# @@PLEAC@@_2.6) -comment(# We can add conversion methods to the Integer class,) -comment(# this makes a roman number just a representation for normal numbers.) -reserved(class) class(Integer) - - class_variable(@@romanlist) operator(=) operator([)operator([)stringoperator(,) integer(1000)operator(])operator(,) - operator([)stringoperator(,) integer(900)operator(])operator(,) - operator([)stringoperator(,) integer(500)operator(])operator(,) - operator([)stringoperator(,) integer(400)operator(])operator(,) - operator([)stringoperator(,) integer(100)operator(])operator(,) - operator([)stringoperator(,) integer(90)operator(])operator(,) - operator([)stringoperator(,) integer(50)operator(])operator(,) - operator([)stringoperator(,) integer(40)operator(])operator(,) - operator([)stringoperator(,) integer(10)operator(])operator(,) - operator([)stringoperator(,) integer(9)operator(])operator(,) - operator([)stringoperator(,) integer(5)operator(])operator(,) - operator([)stringoperator(,) integer(4)operator(])operator(,) - operator([)stringoperator(,) integer(1)operator(])operator(]) - - reserved(def) method(to_roman) - ident(remains) operator(=) pre_constant(self) - ident(roman) operator(=) string - reserved(for) ident(sym)operator(,) ident(num) reserved(in) class_variable(@@romanlist) - reserved(while) ident(remains) operator(>)operator(=) ident(num) - ident(remains) operator(-=) ident(num) - ident(roman) operator(<<) ident(sym) - reserved(end) - reserved(end) - ident(roman) - reserved(end) - - reserved(def) constant(Integer)operator(.)ident(from_roman)operator(()ident(roman)operator(\)) - ident(ustr) operator(=) ident(roman)operator(.)ident(upcase) - ident(sum) operator(=) integer(0) - reserved(for) ident(entry) reserved(in) class_variable(@@romanlist) - ident(sym)operator(,) ident(num) operator(=) ident(entry)operator([)integer(0)operator(])operator(,) ident(entry)operator([)integer(1)operator(]) - reserved(while) ident(sym) operator(==) ident(ustr)operator([)integer(0)operator(,) ident(sym)operator(.)ident(length)operator(]) - ident(sum) operator(+=) ident(num) - ident(ustr)operator(.)ident(slice!)operator(()integer(0)operator(,) ident(sym)operator(.)ident(length)operator(\)) - reserved(end) - reserved(end) - ident(sum) - reserved(end) - -reserved(end) - - -ident(roman_fifteen) operator(=) integer(15)operator(.)ident(to_roman) -ident(puts) stringdelimiter(")> -ident(i) operator(=) constant(Integer)operator(.)ident(from_roman)operator(()ident(roman_fifteen)operator(\)) -ident(puts) stringcontent( is )inlinedelimiter(")> - -comment(# check) -reserved(for) ident(i) reserved(in) operator(()integer(1)operator(..)integer(3900)operator(\)) - ident(r) operator(=) ident(i)operator(.)ident(to_roman) - ident(j) operator(=) constant(Integer)operator(.)ident(from_roman)operator(()ident(r)operator(\)) - reserved(if) ident(i) operator(!=) ident(j) - ident(puts) stringcontent( : )inlinecontent( - )inlinedelimiter(")> - reserved(end) -reserved(end) - - -comment(# @@PLEAC@@_2.7) -ident(random) operator(=) ident(rand)operator(()ident(y)operator(-)ident(x)operator(+)integer(1)operator(\))operator(+)ident(x) - -ident(chars) operator(=) operator([)stringoperator(..)stringoperator(,)stringoperator(..)stringoperator(,)stringoperator(..)stringoperator(])operator(.)ident(collect) operator({) operator(|)ident(r)operator(|) ident(r)operator(.)ident(to_a) operator(})operator(.)ident(join) operator(+) string -ident(password) operator(=) operator(()integer(1)operator(..)integer(8)operator(\))operator(.)ident(collect) operator({) ident(chars)operator([)ident(rand)operator(()ident(chars)operator(.)ident(size)operator(\))operator(]) operator(})operator(.)ident(pack)operator(()stringoperator(\)) - - -comment(# @@PLEAC@@_2.8) -ident(srand) comment(# uses a combination of the time, the process id, and a sequence number) -ident(srand)operator(()ident(val)operator(\)) comment(# for repeatable behaviour) - - -comment(# @@PLEAC@@_2.9) -comment(# from the randomr lib:) -comment(# http://raa.ruby-lang.org/project/randomr/) -operator(-)operator(-)operator(-)operator(-)operator(>) ident(http)symbol(:/)operator(/)ident(raa)operator(.)ident(ruby)operator(-)ident(lang)operator(.)ident(org)operator(/)ident(project)operator(/)ident(randomr)operator(/) - -ident(require) string -ident(mers) operator(=) constant(Random)operator(::)constant(MersenneTwister)operator(.)ident(new) integer(123456789) -ident(puts) ident(mers)operator(.)ident(rand)operator(()integer(0)operator(\)) comment(# 0.550321932544541) -ident(puts) ident(mers)operator(.)ident(rand)operator(()integer(10)operator(\)) comment(# 2) - -comment(# using online sources of random data via the realrand package:) -comment(# http://raa.ruby-lang.org/project/realrand/) -comment(# **Note**) -comment(# The following online services are used in this package:) -comment(# http://www.random.org - source: atmospheric noise) -comment(# http://www.fourmilab.ch/hotbits - source: radioactive decay timings) -comment(# http://random.hd.org - source: entropy from local and network noise) -comment(# Please visit the sites and respect the rules of each service.) - -ident(require) string - -ident(generator1) operator(=) constant(Random)operator(::)constant(RandomOrg)operator(.)ident(new) -ident(puts) ident(generator1)operator(.)ident(randbyte)operator(()integer(5)operator(\))operator(.)ident(join)operator(()stringoperator(\)) -ident(puts) ident(generator1)operator(.)ident(randnum)operator(()integer(10)operator(,) integer(1)operator(,) integer(6)operator(\))operator(.)ident(join)operator(()stringoperator(\)) comment(# Roll dice 10 times.) - -ident(generator2) operator(=) constant(Random)operator(::)constant(FourmiLab)operator(.)ident(new) -ident(puts) ident(generator2)operator(.)ident(randbyte)operator(()integer(5)operator(\))operator(.)ident(join)operator(()stringoperator(\)) -comment(# randnum is not supported.) - -ident(generator3) operator(=) constant(Random)operator(::)constant(EntropyPool)operator(.)ident(new) -ident(puts) ident(generator3)operator(.)ident(randbyte)operator(()integer(5)operator(\))operator(.)ident(join)operator(()stringoperator(\)) -comment(# randnum is not supported.) - - -comment(# @@PLEAC@@_2.10) -reserved(def) method(gaussian_rand) - reserved(begin) - ident(u1) operator(=) integer(2) operator(*) ident(rand)operator(()operator(\)) operator(-) integer(1) - ident(u2) operator(=) integer(2) operator(*) ident(rand)operator(()operator(\)) operator(-) integer(1) - ident(w) operator(=) ident(u1)operator(*)ident(u1) operator(+) ident(u2)operator(*)ident(u2) - reserved(end) reserved(while) operator(()ident(w) operator(>)operator(=) integer(1)operator(\)) - ident(w) operator(=) constant(Math)operator(.)ident(sqrt)operator(()operator(()operator(-)integer(2)operator(*)constant(Math)operator(.)ident(log)operator(()ident(w)operator(\))operator(\))operator(/)ident(w)operator(\)) - operator([) ident(u2)operator(*)ident(w)operator(,) ident(u1)operator(*)ident(w) operator(]) -reserved(end) - -ident(mean) operator(=) integer(25) -ident(sdev) operator(=) integer(2) -ident(salary) operator(=) ident(gaussian_rand)operator([)integer(0)operator(]) operator(*) ident(sdev) operator(+) ident(mean) -ident(printf)operator(()stringoperator(,) ident(salary)operator(\)) - - -comment(# @@PLEAC@@_2.11) -reserved(def) method(deg2rad)operator(()ident(d)operator(\)) - operator(()ident(d)operator(/)float(180.0)operator(\))operator(*)constant(Math)operator(::)constant(PI) -reserved(end) - -reserved(def) method(rad2deg)operator(()ident(r)operator(\)) - operator(()ident(r)operator(/)constant(Math)operator(::)constant(PI)operator(\))operator(*)integer(180) -reserved(end) - - -comment(# @@PLEAC@@_2.12) -ident(sin_val) operator(=) constant(Math)operator(.)ident(sin)operator(()ident(angle)operator(\)) -ident(cos_val) operator(=) constant(Math)operator(.)ident(cos)operator(()ident(angle)operator(\)) -ident(tan_val) operator(=) constant(Math)operator(.)ident(tan)operator(()ident(angle)operator(\)) - -comment(# AFAIK Ruby's Math module doesn't provide acos/asin) -comment(# While we're at it, let's also define missing hyperbolic functions) -reserved(module) class(Math) - reserved(def) constant(Math)operator(.)ident(asin)operator(()ident(x)operator(\)) - ident(atan2)operator(()ident(x)operator(,) ident(sqrt)operator(()integer(1) operator(-) ident(x)operator(**)integer(2)operator(\))operator(\)) - reserved(end) - reserved(def) constant(Math)operator(.)ident(acos)operator(()ident(x)operator(\)) - ident(atan2)operator(()ident(sqrt)operator(()integer(1) operator(-) ident(x)operator(**)integer(2)operator(\))operator(,) ident(x)operator(\)) - reserved(end) - reserved(def) constant(Math)operator(.)ident(atan)operator(()ident(x)operator(\)) - ident(atan2)operator(()ident(x)operator(,) integer(1)operator(\)) - reserved(end) - reserved(def) constant(Math)operator(.)ident(sinh)operator(()ident(x)operator(\)) - operator(()ident(exp)operator(()ident(x)operator(\)) operator(-) ident(exp)operator(()operator(-)ident(x)operator(\))operator(\)) operator(/) integer(2) - reserved(end) - reserved(def) constant(Math)operator(.)ident(cosh)operator(()ident(x)operator(\)) - operator(()ident(exp)operator(()ident(x)operator(\)) operator(+) ident(exp)operator(()operator(-)ident(x)operator(\))operator(\)) operator(/) integer(2) - reserved(end) - reserved(def) constant(Math)operator(.)ident(tanh)operator(()ident(x)operator(\)) - ident(sinh)operator(()ident(x)operator(\)) operator(/) ident(cosh)operator(()ident(x)operator(\)) - reserved(end) -reserved(end) - -comment(# The support for Complex numbers is not built-in) -ident(y) operator(=) constant(Math)operator(.)ident(acos)operator(()float(3.7)operator(\)) -comment(#=> in `sqrt': square root for negative number (ArgumentError\)) - -comment(# There is an implementation of Complex numbers in 'complex.rb' in current) -comment(# Ruby distro, but it doesn't support atan2 with complex args, so it doesn't) -comment(# solve this problem.) - - -comment(# @@PLEAC@@_2.13) -ident(log_e) operator(=) constant(Math)operator(.)ident(log)operator(()ident(val)operator(\)) -ident(log_10) operator(=) constant(Math)operator(.)ident(log10)operator(()ident(val)operator(\)) - -reserved(def) method(log_base)operator(()ident(base)operator(,) ident(val)operator(\)) - constant(Math)operator(.)ident(log)operator(()ident(val)operator(\))operator(/)constant(Math)operator(.)ident(log)operator(()ident(base)operator(\)) -reserved(end) - -ident(answer) operator(=) ident(log_base)operator(()integer(10)operator(,) integer(10_000)operator(\)) -ident(puts) stringdelimiter(")> - - -comment(# @@PLEAC@@_2.14) -ident(require) string - -ident(a) operator(=) constant(Matrix)operator([)operator([)integer(3)operator(,) integer(2)operator(,) integer(3)operator(])operator(,) operator([)integer(5)operator(,) integer(9)operator(,) integer(8)operator(])operator(]) -ident(b) operator(=) constant(Matrix)operator([)operator([)integer(4)operator(,) integer(7)operator(])operator(,) operator([)integer(9)operator(,) integer(3)operator(])operator(,) operator([)integer(8)operator(,) integer(1)operator(])operator(]) -ident(c) operator(=) ident(a) operator(*) ident(b) - -ident(a)operator(.)ident(row_size) -ident(a)operator(.)ident(column_size) - -ident(c)operator(.)ident(det) -ident(a)operator(.)ident(transpose) - - -comment(# @@PLEAC@@_2.15) -ident(require) string -ident(require) string - -ident(a) operator(=) constant(Complex)operator(()integer(3)operator(,) integer(5)operator(\)) comment(# 3 + 5i) -ident(b) operator(=) constant(Complex)operator(()integer(2)operator(,) operator(-)integer(2)operator(\)) comment(# 2 - 2i) -ident(puts) stringdelimiter(")> - -ident(c) operator(=) ident(a) operator(*) ident(b) -ident(d) operator(=) integer(3) operator(+) integer(4)operator(*)constant(Complex)operator(::)constant(I) - -ident(printf) stringcontent(\) = %s)char(\\n)delimiter(")>operator(,) constant(Math)operator(.)ident(sqrt)operator(()ident(d)operator(\)) - - -comment(# @@PLEAC@@_2.16) -ident(number) operator(=) ident(hexadecimal)operator(.)ident(hex) -ident(number) operator(=) ident(octal)operator(.)ident(oct) - -ident(print) string -ident(num) operator(=) ident(gets)operator(.)ident(chomp) -ident(exit) reserved(unless) reserved(defined?)operator(()ident(num)operator(\)) -ident(num) operator(=) ident(num)operator(.)ident(oct) reserved(if) ident(num) operator(=)operator(~) regexp comment(# does both oct and hex) -ident(printf) stringoperator(,) ident(num)operator(,) ident(num)operator(,) ident(num) - -ident(print) string -ident(permissions) operator(=) ident(gets)operator(.)ident(chomp) -ident(raise) string reserved(unless) reserved(defined?)operator(()ident(permissions)operator(\)) -ident(puts) stringdelimiter(")> - - -comment(# @@PLEAC@@_2.17) -reserved(def) method(commify)operator(()ident(n)operator(\)) - ident(n)operator(.)ident(to_s) operator(=)operator(~) regexp - ident(int)operator(,) ident(dec) operator(=) global_variable($1)operator(.)ident(reverse)operator(,) global_variable($2) operator(?) global_variable($2) operator(:) string - reserved(while) ident(int)operator(.)ident(gsub!)operator(()regexpoperator(,) stringoperator(\)) - reserved(end) - ident(int)operator(.)ident(reverse) operator(+) ident(dec) -reserved(end) - - -comment(# @@PLEAC@@_2.18) -ident(printf) stringoperator(,) ident(time)operator(,) ident(time) operator(==) integer(1) operator(?) string operator(:) string - -comment(# dunno if an equivalent to Lingua::EN::Inflect exists...) - - -comment(# @@PLEAC@@_2.19) -comment(#-----------------------------) -comment(#!/usr/bin/ruby) -comment(# bigfact - calculating prime factors) -reserved(def) method(factorize)operator(()ident(orig)operator(\)) - ident(factors) operator(=) operator({)operator(}) - ident(factors)operator(.)ident(default) operator(=) integer(0) comment(# return 0 instead nil if key not found in hash) - ident(n) operator(=) ident(orig) - ident(i) operator(=) integer(2) - ident(sqi) operator(=) integer(4) comment(# square of i) - reserved(while) ident(sqi) operator(<=) ident(n) reserved(do) - reserved(while) ident(n)operator(.)ident(modulo)operator(()ident(i)operator(\)) operator(==) integer(0) reserved(do) - ident(n) regexpcontent(" - end - # we take advantage of the fact that (i +1\)**2 = i**2 + 2*i +1 - sqi += 2 * i + 1 - i += 1 - end - - if (n != 1\) && (n != orig\) - factors[n] += 1 - end - factors -end - -def printfactorhash(orig, factorcount\) - print format("%-10d ", orig\) - if factorcount.length == 0 - print "PRIME" - else - # sorts after number, because the hash keys are numbers - factorcount.sort.each { |factor,exponent| - print factor - if exponent > 1 - print "**", exponent - end - print " " - } - end - puts -end - -for arg in ARGV - n = arg.to_i - mfactors = factorize(n\) - printfactorhash(n, mfactors\) -end -#----------------------------- - - -# @@PLEAC@@_3.0 -puts Time.now - -print "Today is day ", Time.now.yday, " of the current year.)char(\\n)content(" -print "Today is day ", Time.now.day, " of the current month.)char(\\n)content(" - - -# @@PLEAC@@_3.1 -day, month, year = Time.now.day, Time.now.month, Time.now.year -# or -day, month, year = Time.now.to_a[3..5] - -tl = Time.now.localtime -printf("The current date is %04d %02d %02d)char(\\n)content(", tl.year, tl.month, tl.day\) - -Time.now.localtime.strftime("%Y-%m-%d"\) - - -# @@PLEAC@@_3.2 -Time.local(year, month, day, hour, minute, second\).tv_sec -Time.gm(year, month, day, hour, minute, second\).tv_sec - - -# @@PLEAC@@_3.3 -sec, min, hour, day, month, year, wday, yday, isdst, zone = Time.at(epoch_secs\).to_a - - -# @@PLEAC@@_3.4 -when_ = now + difference # now -> Time ; difference -> Numeric (delta in seconds\) -then_ = now - difference - - -# @@PLEAC@@_3.5 -bree = 361535725 -nat = 96201950 - -difference = bree - nat -puts "There were )inlinecontent( seconds between Nat and Bree" - -seconds = difference % 60 -difference = (difference - seconds\) )delimiter(/)> integer(60) -ident(minutes) operator(=) ident(difference) operator(%) integer(60) -ident(difference) operator(=) operator(()ident(difference) operator(-) ident(minutes)operator(\)) operator(/) integer(60) -ident(hours) operator(=) ident(difference) operator(%) integer(24) -ident(difference) operator(=) operator(()ident(difference) operator(-) ident(hours)operator(\)) operator(/) integer(24) -ident(days) operator(=) ident(difference) operator(%) integer(7) -ident(weeks) operator(=) operator(()ident(difference) operator(-) ident(days)operator(\)) operator(/) integer(7) - -ident(puts) stringcontent( weeks, )inlinecontent( days, )inlinecontent(:)inlinecontent(:)inlinecontent(\))delimiter(")> - - -comment(# @@PLEAC@@_3.6) -ident(monthday)operator(,) ident(weekday)operator(,) ident(yearday) operator(=) ident(date)operator(.)ident(mday)operator(,) ident(date)operator(.)ident(wday)operator(,) ident(date)operator(.)ident(yday) - -comment(# AFAIK the week number is not just a division since week boundaries are on sundays) -ident(weeknum) operator(=) ident(d)operator(.)ident(strftime)operator(()stringoperator(\))operator(.)ident(to_i) operator(+) integer(1) - -ident(year) operator(=) integer(1981) -ident(month) operator(=) string comment(# or `6' if you want to emulate a broken language) -ident(day) operator(=) integer(16) -ident(t) operator(=) constant(Time)operator(.)ident(mktime)operator(()ident(year)operator(,) ident(month)operator(,) ident(day)operator(\)) -ident(print) stringcontent(/)inlinecontent(/)inlinecontent( was a )delimiter(")>operator(,) ident(t)operator(.)ident(strftime)operator(()stringoperator(\))operator(,) string - - -comment(# @@PLEAC@@_3.7) -ident(yyyy)operator(,) ident(mm)operator(,) ident(dd) operator(=) global_variable($1)operator(,) global_variable($2)operator(,) global_variable($3) reserved(if) string operator(=)operator(~) regexp - -ident(epoch_seconds) operator(=) constant(Time)operator(.)ident(mktime)operator(()ident(yyyy)operator(,) ident(mm)operator(,) ident(dd)operator(\))operator(.)ident(tv_sec) - -comment(# dunno an equivalent to Date::Manip#ParseDate) - - -comment(# @@PLEAC@@_3.8) -ident(string) operator(=) constant(Time)operator(.)ident(at)operator(()ident(epoch_secs)operator(\)) -constant(Time)operator(.)ident(at)operator(()integer(1234567890)operator(\))operator(.)ident(gmtime) comment(# gives: Fri Feb 13 23:31:30 UTC 2009) - -ident(time) operator(=) constant(Time)operator(.)ident(mktime)operator(()integer(1973)operator(,) stringoperator(,) integer(18)operator(,) integer(3)operator(,) integer(45)operator(,) integer(50)operator(\)) -ident(print) stringoperator(,) ident(time)operator(.)ident(localtime)operator(,) string - - -comment(# @@PLEAC@@_3.9) -comment(# Ruby provides micro-seconds in Time object) -constant(Time)operator(.)ident(now)operator(.)ident(usec) - -comment(# Ruby gives the seconds in floating format when substracting two Time objects) -ident(before) operator(=) constant(Time)operator(.)ident(now) -ident(line) operator(=) ident(gets) -ident(elapsed) operator(=) constant(Time)operator(.)ident(now) operator(-) ident(before) -ident(puts) stringcontent( seconds.)delimiter(")> - -comment(# On my Celeron-400 with Linux-2.2.19-14mdk, average for three execs are:) -comment(# This Ruby version: average 0.00321 sec) -comment(# Cookbook's Perl version: average 0.00981 sec) -ident(size) operator(=) integer(500) -ident(number_of_times) operator(=) integer(100) -ident(total_time) operator(=) integer(0) -ident(number_of_times)operator(.)ident(times) operator({) - comment(# populate array) - ident(array) operator(=) operator([)operator(]) - ident(size)operator(.)ident(times) operator({) ident(array) operator(<<) ident(rand) operator(}) - comment(# sort it) - ident(begin_) operator(=) constant(Time)operator(.)ident(now) - ident(array)operator(.)ident(sort!) - ident(time) operator(=) constant(Time)operator(.)ident(now) operator(-) ident(begin_) - ident(total_time) operator(+=) ident(time) -operator(}) -ident(printf) stringoperator(,) - ident(size)operator(,) operator(()ident(total_time)operator(/)constant(Float)operator(()ident(number_of_times)operator(\))operator(\)) - - -comment(# @@PLEAC@@_3.10) -ident(sleep)operator(()float(0.005)operator(\)) comment(# Ruby is definitely not as broken as Perl :\)) -comment(# (may be interrupted by sending the process a SIGALRM\)) - - -comment(# @@PLEAC@@_3.11) -comment(#!/usr/bin/ruby -w) -comment(# hopdelta - feed mail header, produce lines) -comment(# showing delay at each hop.) -ident(require) string -reserved(class) class(MailHopDelta) - - reserved(def) method(initialize)operator(()ident(mail)operator(\)) - instance_variable(@head) operator(=) ident(mail)operator(.)ident(gsub)operator(()regexpoperator(,)stringoperator(\)) - instance_variable(@topline) operator(=) string - instance_variable(@start_from) operator(=) ident(mail)operator(.)ident(match)operator(()regexp]*\))delimiter(/)>operator(\))operator([)integer(1)operator(]) - instance_variable(@date) operator(=) constant(Time)operator(.)ident(parse)operator(()ident(mail)operator(.)ident(match)operator(()regexpoperator(\))operator([)integer(1)operator(])operator(\)) - reserved(end) - - reserved(def) method(out)operator(()ident(line)operator(\)) - string operator(%) ident(line) - reserved(end) - - reserved(def) method(hop_date)operator(()ident(day)operator(\)) - ident(day)operator(.)ident(strftime)operator(()stringoperator(\)) - reserved(end) - - reserved(def) method(puts_hops) - ident(puts) ident(out)operator(()instance_variable(@topline)operator(\)) - ident(puts) ident(out)operator(()operator([)stringoperator(,) instance_variable(@start_from)operator(,) ident(hop_date)operator(()instance_variable(@date)operator(\))operator(,)stringoperator(])operator(\)) - instance_variable(@head)operator(.)ident(split)operator(()regexpoperator(\))operator(.)ident(reverse)operator(.)ident(grep)operator(()regexpoperator(\))operator(.)ident(each) reserved(do) operator(|)ident(hop)operator(|) - ident(hop)operator(.)ident(gsub!)operator(()regexpoperator(,)stringoperator(\)) - ident(whence) operator(=) ident(hop)operator(.)ident(match)operator(()regexpoperator(\))operator([)integer(1)operator(]) - reserved(unless) ident(whence) - ident(warn) stringdelimiter(")> - reserved(next) - reserved(end) - ident(from) operator(=) global_variable($+) reserved(if) ident(hop) operator(=)operator(~) regexp - ident(by) operator(=) global_variable($1) reserved(if) ident(hop) operator(=)operator(~) regexp - reserved(next) reserved(unless) ident(now) operator(=) constant(Time)operator(.)ident(parse)operator(()ident(whence)operator(\))operator(.)ident(localtime) - ident(delta) operator(=) ident(now) operator(-) instance_variable(@date) - ident(puts) ident(out)operator(()operator([)ident(from)operator(,) ident(by)operator(,) ident(hop_date)operator(()ident(now)operator(\))operator(,) ident(hop_time)operator(()ident(delta)operator(\))operator(])operator(\)) - instance_variable(@date) operator(=) ident(now) - reserved(end) - reserved(end) - - reserved(def) method(hop_time)operator(()ident(secs)operator(\)) - ident(sign) operator(=) ident(secs) operator(<) integer(0) operator(?) operator(-)integer(1) operator(:) integer(1) - ident(days)operator(,) ident(secs) operator(=) ident(secs)operator(.)ident(abs)operator(.)ident(divmod)operator(()integer(60) operator(*) integer(60) operator(*) integer(24)operator(\)) - ident(hours)operator(,)ident(secs) operator(=) ident(secs)operator(.)ident(abs)operator(.)ident(divmod)operator(()integer(60) operator(*) integer(60)operator(\)) - ident(mins)operator(,) ident(secs) operator(=) ident(secs)operator(.)ident(abs)operator(.)ident(divmod)operator(()integer(60)operator(\)) - ident(rtn) operator(=) string operator(%) operator([)ident(secs) operator(*) ident(sign)operator(]) - ident(rtn) operator(<<) string operator(%) operator([)ident(mins) operator(*) ident(sign)operator(]) reserved(if) ident(mins) operator(!=) integer(0) - ident(rtn) operator(<<) string operator(%) operator([)ident(hours) operator(*) ident(sign)operator(]) reserved(if) ident(hours) operator(!=) integer(0) - ident(rtn) operator(<<) string operator(%) operator([)ident(days) operator(*) ident(sign)operator(]) reserved(if) ident(days) operator(!=) integer(0) - ident(rtn) - reserved(end) -reserved(end) - -global_variable($/) operator(=) string -ident(mail) operator(=) constant(MailHopDelta)operator(.)ident(new)operator(()pre_constant(ARGF)operator(.)ident(gets)operator(\))operator(.)ident(puts_hops) - - -comment(# @@PLEAC@@_4.0) -ident(single_level) operator(=) operator([) stringoperator(,) stringoperator(,) stringoperator(,) string operator(]) - -comment(# Ruby directly supports nested arrays) -ident(double_level) operator(=) operator([) stringoperator(,) stringoperator(,) operator([) stringoperator(,) string operator(]) operator(]) -ident(still_single_level) operator(=) operator([) stringoperator(,) stringoperator(,) operator([) stringoperator(,) string operator(]) operator(])operator(.)ident(flatten) - - -comment(# @@PLEAC@@_4.1) -ident(a) operator(=) operator([) stringoperator(,) stringoperator(,) string operator(]) -ident(a) operator(=) string - -ident(lines) operator(=) stringoperator(.)ident(gsub)operator(()regexpoperator(,) stringoperator(\))string - -ident(bigarray) operator(=) constant(IO)operator(.)ident(readlines)operator(()stringoperator(\))operator(.)ident(collect) operator({) operator(|)ident(l)operator(|) ident(l)operator(.)ident(chomp) operator(}) - -ident(name) operator(=) string -ident(banner) operator(=) stringcontent(, and welcome!)delimiter(\))> - -ident(host_info) operator(=) shelldelimiter(`)> - -shelldelimiter(\))> - -ident(banner) operator(=) stringoperator(.)ident(split)operator(()stringoperator(\)) - -ident(rax) operator(=) string { } [ ] )delimiter(!)> - - -comment(# @@PLEAC@@_4.2) -reserved(def) method(commify_series)operator(()ident(a)operator(\)) - ident(a)operator(.)ident(size) operator(==) integer(0) operator(?) string operator(:) - ident(a)operator(.)ident(size) operator(==) integer(1) operator(?) ident(a)operator([)integer(0)operator(]) operator(:) - ident(a)operator(.)ident(size) operator(==) integer(2) operator(?) ident(a)operator(.)ident(join)operator(()stringoperator(\)) operator(:) - ident(a)operator([)integer(0)operator(..)operator(-)integer(2)operator(])operator(.)ident(join)operator(()stringoperator(\)) operator(+) string operator(+) ident(a)operator([)operator(-)integer(1)operator(]) -reserved(end) - -ident(array) operator(=) operator([) stringoperator(,) stringoperator(,) string operator(]) - -ident(print) stringoperator(,) ident(array)operator(,) string -comment(# -> I have redyellowgreen marbles) - -comment(# But unlike Perl:) -ident(print) stringcontent( marbles)char(\\n)delimiter(")> -comment(# -> I have redyellowgreen marbles) -comment(# So, needs:) -ident(print) stringoperator(\))delimiter(})>content( marbles)char(\\n)delimiter(")> -comment(# -> I have red yellow green marbles) - -reserved(def) method(commify_series)operator(()ident(a)operator(\)) - ident(sepchar) operator(=) ident(a)operator(.)ident(select) operator({) operator(|)ident(p)operator(|) ident(p) operator(=)operator(~) regexp operator(}) operator(!=) operator([)operator(]) operator(?) string operator(:) string - ident(a)operator(.)ident(size) operator(==) integer(0) operator(?) string operator(:) - ident(a)operator(.)ident(size) operator(==) integer(1) operator(?) ident(a)operator([)integer(0)operator(]) operator(:) - ident(a)operator(.)ident(size) operator(==) integer(2) operator(?) ident(a)operator(.)ident(join)operator(()stringoperator(\)) operator(:) - ident(a)operator([)integer(0)operator(..)operator(-)integer(2)operator(])operator(.)ident(join)operator(()ident(sepchar)operator(\)) operator(+) ident(sepchar) operator(+) string operator(+) ident(a)operator([)operator(-)integer(1)operator(]) -reserved(end) - - -comment(# @@PLEAC@@_4.3) -comment(# (note: AFAIK Ruby doesn't allow gory change of Array length\)) -comment(# grow the array by assigning nil to past the end of array) -ident(ary)operator([)ident(new_size)operator(-)integer(1)operator(]) operator(=) pre_constant(nil) -comment(# shrink the array by slicing it down) -ident(ary)operator(.)ident(slice!)operator(()ident(new_size)operator(..)operator(-)integer(1)operator(\)) -comment(# init the array with given size) -constant(Array)operator(.)ident(new)operator(()ident(number_of_elems)operator(\)) -comment(# assign to an element past the original end enlarges the array) -ident(ary)operator([)ident(index_new_last_elem)operator(]) operator(=) ident(value) - -reserved(def) method(what_about_that_array)operator(()ident(a)operator(\)) - ident(print) stringoperator(,) ident(a)operator(.)ident(size)operator(,) string - comment(# Index of last element is not really interesting in Ruby) - ident(print) stringcontent('.)char(\\n)delimiter(")> -reserved(end) -ident(people) operator(=) string -ident(what_about_that_array)operator(()ident(people)operator(\)) - - -comment(# @@PLEAC@@_4.4) -comment(# OO style) -ident(bad_users)operator(.)ident(each) operator({) operator(|)ident(user)operator(|) - ident(complain)operator(()ident(user)operator(\)) -operator(}) -comment(# or, functional style) -reserved(for) ident(user) reserved(in) ident(bad_users) - ident(complain)operator(()ident(user)operator(\)) -reserved(end) - -reserved(for) ident(var) reserved(in) constant(ENV)operator(.)ident(keys)operator(.)ident(sort) - ident(puts) stringcontent(=)inlinedelimiter(")> -reserved(end) - -reserved(for) ident(user) reserved(in) ident(all_users) - ident(disk_space) operator(=) ident(get_usage)operator(()ident(user)operator(\)) - reserved(if) operator(()ident(disk_space) operator(>) constant(MAX_QUOTA)operator(\)) - ident(complain)operator(()ident(user)operator(\)) - reserved(end) -reserved(end) - -reserved(for) ident(l) reserved(in) constant(IO)operator(.)ident(popen)operator(()stringoperator(\))operator(.)ident(readlines) - ident(print) ident(l) reserved(if) ident(l) operator(=)operator(~) regexp -reserved(end) - -comment(# we can mimic the obfuscated Perl way) -reserved(while) ident(fh)operator(.)ident(gets) comment(# $_ is set to the line just read) - ident(chomp) comment(# $_ has a trailing \\n removed, if it had one) - ident(split)operator(.)ident(each) operator({) operator(|)ident(w)operator(|) comment(# $_ is split on whitespace) - comment(# but $_ is not set to each chunk as in Perl) - ident(print) ident(w)operator(.)ident(reverse) - operator(}) -reserved(end) -comment(# ...or use a cleaner way) -reserved(for) ident(l) reserved(in) ident(fh)operator(.)ident(readlines) - ident(l)operator(.)ident(chomp)operator(.)ident(split)operator(.)ident(each) operator({) operator(|)ident(w)operator(|) ident(print) ident(w)operator(.)ident(reverse) operator(}) -reserved(end) - -comment(# same drawback as in problem 1.4, we can't mutate a Numeric...) -ident(array)operator(.)ident(collect!) operator({) operator(|)ident(v)operator(|) ident(v) operator(-) integer(1) operator(}) - -ident(a) operator(=) operator([) operator(.)integer(5)operator(,) integer(3) operator(])operator(;) ident(b) operator(=) operator([) integer(0)operator(,) integer(1) operator(]) -reserved(for) ident(ary) reserved(in) operator([) ident(a)operator(,) ident(b) operator(]) - ident(ary)operator(.)ident(collect!) operator({) operator(|)ident(v)operator(|) ident(v) operator(*) integer(7) operator(}) -reserved(end) -ident(puts) stringoperator(\))delimiter(})>content( )inlineoperator(\))delimiter(})>delimiter(")> - -comment(# we can mutate Strings, cool; we need a trick for the scalar) -reserved(for) ident(ary) reserved(in) operator([) operator([) ident(scalar) operator(])operator(,) ident(array)operator(,) ident(hash)operator(.)ident(values) operator(]) - ident(ary)operator(.)ident(each) operator({) operator(|)ident(v)operator(|) ident(v)operator(.)ident(strip!) operator(}) comment(# String#strip rules :\)) -reserved(end) - - -comment(# @@PLEAC@@_4.5) -comment(# not relevant in Ruby since we have always references) -reserved(for) ident(item) reserved(in) ident(array) - comment(# do somethingh with item) -reserved(end) - - -comment(# @@PLEAC@@_4.6) -ident(unique) operator(=) ident(list)operator(.)ident(uniq) - -comment(# generate a list of users logged in, removing duplicates) -ident(users) operator(=) shelloperator(.)ident(collect) operator({) operator(|)ident(l)operator(|) ident(l) operator(=)operator(~) regexpoperator(;) global_variable($1) operator(})operator(.)ident(sort)operator(.)ident(uniq) -ident(puts)operator(()stringdelimiter(")>operator(\)) comment(# see 4.2 for commify_series) - - -comment(# @@PLEAC@@_4.7) -ident(a) operator(-) ident(b) -comment(# [ 1, 1, 2, 2, 3, 3, 3, 4, 5 ] - [ 1, 2, 4 ] -> [3, 5]) - - -comment(# @@PLEAC@@_4.8) -ident(union) operator(=) ident(a) operator(|) ident(b) -ident(intersection) operator(=) ident(a) operator(&) ident(b) -ident(difference) operator(=) ident(a) operator(-) ident(b) - - -comment(# @@PLEAC@@_4.9) -ident(array1)operator(.)ident(concat)operator(()ident(array2)operator(\)) -comment(# if you will assign to another object, better use:) -ident(new_ary) operator(=) ident(array1) operator(+) ident(array2) - -ident(members) operator(=) operator([) stringoperator(,) string operator(]) -ident(initiates) operator(=) operator([) stringoperator(,) string operator(]) -ident(members) operator(+=) ident(initiates) - -ident(members) operator(=) operator([) stringoperator(,) string operator(]) -ident(initiates) operator(=) operator([) stringoperator(,) string operator(]) -ident(members)operator([)integer(2)operator(,)integer(0)operator(]) operator(=) operator([) stringoperator(,) ident(initiates) operator(])operator(.)ident(flatten) - -ident(members)operator([)integer(0)operator(]) operator(=) string -ident(members)operator([)integer(3)operator(,)integer(2)operator(]) operator(=) stringoperator(,) string - - -comment(# @@PLEAC@@_4.10) -ident(reversed) operator(=) ident(ary)operator(.)ident(reverse) - -ident(ary)operator(.)ident(reverse_each) operator({) operator(|)ident(e)operator(|) - comment(# do something with e) -operator(}) - -ident(descending) operator(=) ident(ary)operator(.)ident(sort)operator(.)ident(reverse) -ident(descending) operator(=) ident(ary)operator(.)ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(b) operator(<=>) ident(a) operator(}) - - -comment(# @@PLEAC@@_4.11) -comment(# remove n elements from front of ary (shift n\)) -ident(front) operator(=) ident(ary)operator(.)ident(slice!)operator(()integer(0)operator(,) ident(n)operator(\)) - -comment(# remove n elements from the end of ary (pop n\)) -ident(end_) operator(=) ident(ary)operator(.)ident(slice!)operator(()operator(-)ident(n) operator(..) operator(-)integer(1)operator(\)) - -comment(# let's extend the Array class, to make that useful) -reserved(class) class(Array) - reserved(def) method(shift2)operator(()operator(\)) - ident(slice!)operator(()integer(0) operator(..) integer(1)operator(\)) comment(# more symetric with pop2...) - reserved(end) - reserved(def) method(pop2)operator(()operator(\)) - ident(slice!)operator(()operator(-)integer(2) operator(..) operator(-)integer(1)operator(\)) - reserved(end) -reserved(end) - -ident(friends) operator(=) string -ident(this)operator(,) ident(that) operator(=) ident(friends)operator(.)ident(shift2) - -ident(beverages) operator(=) string -ident(pair) operator(=) ident(beverages)operator(.)ident(pop2) - - -comment(# @@PLEAC@@_4.12) -comment(# use Enumerable#detect (or the synonym Enumerable#find\)) -ident(highest_eng) operator(=) ident(employees)operator(.)ident(detect) operator({) operator(|)ident(emp)operator(|) ident(emp)operator(.)ident(category) operator(==) string operator(}) - - -comment(# @@PLEAC@@_4.13) -comment(# use Enumerable#select (or the synonym Enumerable#find_all\)) -ident(bigs) operator(=) ident(nums)operator(.)ident(select) operator({) operator(|)ident(i)operator(|) ident(i) operator(>) integer(1_000_000) operator(}) -ident(pigs) operator(=) ident(users)operator(.)ident(keys)operator(.)ident(select) operator({) operator(|)ident(k)operator(|) ident(users)operator([)ident(k)operator(]) operator(>) float(1e7) operator(}) - -ident(matching) operator(=) shelloperator(.)ident(select) operator({) operator(|)ident(u)operator(|) ident(u) operator(=)operator(~) regexp operator(}) - -ident(engineers) operator(=) ident(employees)operator(.)ident(select) operator({) operator(|)ident(e)operator(|) ident(e)operator(.)ident(position) operator(==) string operator(}) - -ident(secondary_assistance) operator(=) ident(applicants)operator(.)ident(select) operator({) operator(|)ident(a)operator(|) - ident(a)operator(.)ident(income) operator(>)operator(=) integer(26_000) operator(&&) ident(a)operator(.)ident(income) operator(<) integer(30_000) -operator(}) - - -comment(# @@PLEAC@@_4.14) -comment(# normally you would have an array of Numeric (Float or) -comment(# Fixnum or Bignum\), so you would use:) -ident(sorted) operator(=) ident(unsorted)operator(.)ident(sort) -comment(# if you have strings representing Integers or Floats) -comment(# you may specify another sort method:) -ident(sorted) operator(=) ident(unsorted)operator(.)ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(a)operator(.)ident(to_f) operator(<=>) ident(b)operator(.)ident(to_f) operator(}) - -comment(# let's use the list of my own PID's) -shelloperator(.)ident(split)operator(()stringoperator(\))operator([)integer(1)operator(..)operator(-)integer(1)operator(])operator(.) - ident(select) operator({) operator(|)ident(i)operator(|) ident(i) operator(=)operator(~) regexpoperator(])delimiter(})>delimiter(/)> operator(})operator(.) - ident(collect) operator({) operator(|)ident(i)operator(|) ident(i)operator(.)ident(split)operator([)integer(1)operator(]) operator(})operator(.) - ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(a)operator(.)ident(to_i) operator(<=>) ident(b)operator(.)ident(to_i) operator(})operator(.)ident(each) operator({) operator(|)ident(i)operator(|) ident(puts) ident(i) operator(}) -ident(puts) string -ident(pid) operator(=) ident(gets)operator(.)ident(chomp) -ident(raise) string reserved(unless) ident(pid) operator(&&) ident(pid) operator(=)operator(~) regexp -constant(Process)operator(.)ident(kill)operator(()stringoperator(,) ident(pid)operator(.)ident(to_i)operator(\)) -ident(sleep) integer(2) -constant(Process)operator(.)ident(kill)operator(()stringoperator(,) ident(pid)operator(.)ident(to_i)operator(\)) - -ident(descending) operator(=) ident(unsorted)operator(.)ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(b)operator(.)ident(to_f) operator(<=>) ident(a)operator(.)ident(to_f) operator(}) - - -comment(# @@PLEAC@@_4.15) -ident(ordered) operator(=) ident(unordered)operator(.)ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(compare)operator(()ident(a)operator(,)ident(b)operator(\)) operator(}) - -ident(precomputed) operator(=) ident(unordered)operator(.)ident(collect) operator({) operator(|)ident(e)operator(|) operator([)ident(compute)operator(,) ident(e)operator(]) operator(}) -ident(ordered_precomputed) operator(=) ident(precomputed)operator(.)ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(a)operator([)integer(0)operator(]) operator(<=>) ident(b)operator([)integer(0)operator(]) operator(}) -ident(ordered) operator(=) ident(ordered_precomputed)operator(.)ident(collect) operator({) operator(|)ident(e)operator(|) ident(e)operator([)integer(1)operator(]) operator(}) - -ident(ordered) operator(=) ident(unordered)operator(.)ident(collect) operator({) operator(|)ident(e)operator(|) operator([)ident(compute)operator(,) ident(e)operator(]) operator(})operator(.) - ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(a)operator([)integer(0)operator(]) operator(<=>) ident(b)operator([)integer(0)operator(]) operator(})operator(.) - ident(collect) operator({) operator(|)ident(e)operator(|) ident(e)operator([)integer(1)operator(]) operator(}) - -reserved(for) ident(employee) reserved(in) ident(employees)operator(.)ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(a)operator(.)ident(name) operator(<=>) ident(b)operator(.)ident(name) operator(}) - ident(print) ident(employee)operator(.)ident(name)operator(,) stringoperator(,) ident(employee)operator(.)ident(salary)operator(,) string -reserved(end) - -comment(# Beware! `0' is true in Ruby.) -comment(# For chaining comparisons, you may use Numeric#nonzero?, which) -comment(# returns num if num is not zero, nil otherwise) -ident(sorted) operator(=) ident(employees)operator(.)ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) operator(()ident(a)operator(.)ident(name) operator(<=>) ident(b)operator(.)ident(name)operator(\))operator(.)ident(nonzero?) operator(||) ident(b)operator(.)ident(age) operator(<=>) ident(a)operator(.)ident(age) operator(}) - -ident(users) operator(=) operator([)operator(]) -comment(# getpwent is not wrapped in Ruby... let's fallback) -constant(IO)operator(.)ident(readlines)operator(()stringoperator(\))operator(.)ident(each) operator({) operator(|)ident(u)operator(|) ident(users) operator(<<) ident(u)operator(.)ident(split)operator(()stringoperator(\)) operator(}) -ident(users)operator(.)ident(sort!) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(a)operator([)integer(0)operator(]) operator(<=>) ident(b)operator([)integer(0)operator(]) operator(}) -reserved(for) ident(user) reserved(in) ident(users) - ident(puts) ident(user)operator([)integer(0)operator(]) -reserved(end) - -ident(sorted) operator(=) ident(names)operator(.)ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(a)operator([)integer(1)operator(,) integer(1)operator(]) operator(<=>) ident(b)operator([)integer(1)operator(,) integer(1)operator(]) operator(}) -ident(sorted) operator(=) ident(strings)operator(.)ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(a)operator(.)ident(length) operator(<=>) ident(b)operator(.)ident(length) operator(}) - -comment(# let's show only the compact version) -ident(ordered) operator(=) ident(strings)operator(.)ident(collect) operator({) operator(|)ident(e)operator(|) operator([)ident(e)operator(.)ident(length)operator(,) ident(e)operator(]) operator(})operator(.) - ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(a)operator([)integer(0)operator(]) operator(<=>) ident(b)operator([)integer(0)operator(]) operator(})operator(.) - ident(collect) operator({) operator(|)ident(e)operator(|) ident(e)operator([)integer(1)operator(]) operator(}) - -ident(ordered) operator(=) ident(strings)operator(.)ident(collect) operator({) operator(|)ident(e)operator(|) operator([)regexpoperator(.)ident(match)operator(()ident(e)operator(\))operator([)integer(0)operator(])operator(.)ident(to_i)operator(,) ident(e)operator(]) operator(})operator(.) - ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(a)operator([)integer(0)operator(]) operator(<=>) ident(b)operator([)integer(0)operator(]) operator(})operator(.) - ident(collect) operator({) operator(|)ident(e)operator(|) ident(e)operator([)integer(1)operator(]) operator(}) - -ident(print) shelloperator(.)ident(collect) operator({) operator(|)ident(e)operator(|) operator([)ident(e)operator(,) ident(e)operator(.)ident(split)operator(()stringoperator(\))operator(.)ident(indexes)operator(()integer(3)operator(,)integer(2)operator(,)integer(0)operator(\))operator(])operator(.)ident(flatten) operator(})operator(.) - ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) operator(()ident(a)operator([)integer(1)operator(]) operator(<=>) ident(b)operator([)integer(1)operator(])operator(\))operator(.)ident(nonzero?) operator(||) operator(()ident(a)operator([)integer(2)operator(]) operator(<=>) ident(b)operator([)integer(2)operator(])operator(\))operator(.)ident(nonzero?) operator(||) ident(a)operator([)integer(3)operator(]) operator(<=>) ident(b)operator([)integer(3)operator(]) operator(})operator(.) - ident(collect) operator({) operator(|)ident(e)operator(|) ident(e)operator([)integer(0)operator(]) operator(}) - - -comment(# @@PLEAC@@_4.16) -ident(circular)operator(.)ident(unshift)operator(()ident(circular)operator(.)ident(pop)operator(\)) comment(# the last shall be first) -ident(circular)operator(.)ident(push)operator(()ident(circular)operator(.)ident(shift)operator(\)) comment(# and vice versa) - -reserved(def) method(grab_and_rotate)operator(()ident(l)operator(\)) - ident(l)operator(.)ident(push)operator(()ident(ret) operator(=) ident(l)operator(.)ident(shift)operator(\)) - ident(ret) -reserved(end) - -ident(processes) operator(=) operator([)integer(1)operator(,) integer(2)operator(,) integer(3)operator(,) integer(4)operator(,) integer(5)operator(]) -reserved(while) operator(()integer(1)operator(\)) - ident(process) operator(=) ident(grab_and_rotate)operator(()ident(processes)operator(\)) - ident(puts) stringdelimiter(")> - ident(sleep) integer(1) -reserved(end) - - -comment(# @@PLEAC@@_4.17) -reserved(def) method(fisher_yates_shuffle)operator(()ident(a)operator(\)) - operator(()ident(a)operator(.)ident(size)operator(-)integer(1)operator(\))operator(.)ident(downto)operator(()integer(1)operator(\)) operator({) operator(|)ident(i)operator(|) - ident(j) operator(=) ident(rand)operator(()ident(i)operator(+)integer(1)operator(\)) - ident(a)operator([)ident(i)operator(])operator(,) ident(a)operator([)ident(j)operator(]) operator(=) ident(a)operator([)ident(j)operator(])operator(,) ident(a)operator([)ident(i)operator(]) reserved(if) ident(i) operator(!=) ident(j) - operator(}) -reserved(end) - -reserved(def) method(naive_shuffle)operator(()ident(a)operator(\)) - reserved(for) ident(i) reserved(in) integer(0)operator(...)ident(a)operator(.)ident(size) - ident(j) operator(=) ident(rand)operator(()ident(a)operator(.)ident(size)operator(\)) - ident(a)operator([)ident(i)operator(])operator(,) ident(a)operator([)ident(j)operator(]) operator(=) ident(a)operator([)ident(j)operator(])operator(,) ident(a)operator([)ident(i)operator(]) - reserved(end) -reserved(end) - - -comment(# @@PLEAC@@_4.18) -comment(#!/usr/bin/env ruby) -comment(# example 4-2 words) -comment(# words - gather lines, present in colums) - -comment(# class to encapsulate the word formatting from the input) -reserved(class) class(WordFormatter) - reserved(def) method(initialize)operator(()ident(cols)operator(\)) - instance_variable(@cols) operator(=) ident(cols) - reserved(end) - - comment(# helper to return the length of the longest word in the wordlist) - reserved(def) method(maxlen)operator(()ident(wordlist)operator(\)) - ident(max) operator(=) integer(1) - reserved(for) ident(word) reserved(in) ident(wordlist) - reserved(if) ident(word)operator(.)ident(length) operator(>) ident(max) - ident(max) operator(=) ident(word)operator(.)ident(length) - reserved(end) - reserved(end) - ident(max) - reserved(end) - - comment(# process the wordlist and print it formmated into columns) - reserved(def) method(output)operator(()ident(wordlist)operator(\)) - ident(collen) operator(=) ident(maxlen)operator(()ident(wordlist)operator(\)) operator(+) integer(1) - ident(columns) operator(=) instance_variable(@cols) operator(/) ident(collen) - ident(columns) operator(=) integer(1) reserved(if) ident(columns) operator(==) integer(0) - ident(rows) operator(=) operator(()ident(wordlist)operator(.)ident(length) operator(+) ident(columns) operator(-) integer(1)operator(\)) operator(/) ident(columns) - comment(# now process each item, picking out proper piece for this position) - integer(0)operator(.)ident(upto)operator(()ident(rows) operator(*) ident(columns) operator(-) integer(1)operator(\)) operator({) operator(|)ident(item)operator(|) - ident(target) operator(=) operator(()ident(item) operator(%) ident(columns)operator(\)) operator(*) ident(rows) operator(+) operator(()ident(item) operator(/) ident(columns)operator(\)) - ident(eol) operator(=) operator(()operator(()ident(item)operator(+)integer(1)operator(\)) operator(%) ident(columns) operator(==) integer(0)operator(\)) - ident(piece) operator(=) ident(wordlist)operator([)ident(target)operator(]) operator(||) string - ident(piece) operator(=) ident(piece)operator(.)ident(ljust)operator(()ident(collen)operator(\)) reserved(unless) ident(eol) - ident(print) ident(piece) - ident(puts) reserved(if) ident(eol) - operator(}) - comment(# no need to finish it up, because eol is always true for the last element) - reserved(end) -reserved(end) - -comment(# get nr of chars that fit in window or console, see PLEAC 15.4) -comment(# not portable -- linux only (?\)) -reserved(def) method(getWinCharWidth)operator(()operator(\)) - ident(buf) operator(=) string operator(*) integer(8) - global_variable($stdout)operator(.)ident(ioctl)operator(()integer(0x5413)operator(,) ident(buf)operator(\)) - ident(ws_row)operator(,) ident(ws_col)operator(,) ident(ws_xpixel)operator(,) ident(ws_ypixel) operator(=) ident(buf)operator(.)ident(unpack)operator(()stringoperator(\)) - ident(ws_col) operator(||) integer(80) -reserved(rescue) - integer(80) -reserved(end) - -comment(# main program) -ident(cols) operator(=) ident(getWinCharWidth)operator(()operator(\)) -ident(formatter) operator(=) constant(WordFormatter)operator(.)ident(new)operator(()ident(cols)operator(\)) -ident(words) operator(=) ident(readlines)operator(()operator(\)) -ident(words)operator(.)ident(collect!) operator({) operator(|)ident(line)operator(|) - ident(line)operator(.)ident(chomp) -operator(}) -ident(formatter)operator(.)ident(output)operator(()ident(words)operator(\)) - - -comment(# @@PLEAC@@_4.19) -comment(# In ruby, Fixnum's are automatically converted to Bignum's when) -comment(# needed, so there is no need for an extra module) -reserved(def) method(factorial)operator(()ident(n)operator(\)) - ident(s) operator(=) integer(1) - reserved(while) ident(n) operator(>) integer(0) - ident(s) operator(*=) ident(n) - ident(n) operator(-=) integer(1) - reserved(end) - ident(s) -reserved(end) - -ident(puts) ident(factorial)operator(()integer(500)operator(\)) - -comment(#---------------------------------------------------------) -comment(# Example 4-3. tsc-permute) -comment(# tsc_permute: permute each word of input) -reserved(def) method(permute)operator(()ident(items)operator(,) ident(perms)operator(\)) - reserved(unless) ident(items)operator(.)ident(length) operator(>) integer(0) - ident(puts) ident(perms)operator(.)ident(join)operator(()stringoperator(\)) - reserved(else) - reserved(for) ident(i) reserved(in) ident(items) - ident(newitems) operator(=) ident(items)operator(.)ident(dup) - ident(newperms) operator(=) ident(perms)operator(.)ident(dup) - ident(newperms)operator(.)ident(unshift)operator(()ident(newitems)operator(.)ident(delete)operator(()ident(i)operator(\))operator(\)) - ident(permute)operator(()ident(newitems)operator(,) ident(newperms)operator(\)) - reserved(end) - reserved(end) -reserved(end) -comment(# In ruby the main program must be after all definitions it is using) -ident(permute)operator(()pre_constant(ARGV)operator(,) operator([)operator(])operator(\)) - -comment(#---------------------------------------------------------) -comment(# mjd_permute: permute each word of input) - -reserved(def) method(factorial)operator(()ident(n)operator(\)) - ident(s) operator(=) integer(1) - reserved(while) ident(n) operator(>) integer(0) - ident(s) operator(*=) ident(n) - ident(n) operator(-=) integer(1) - reserved(end) - ident(s) -reserved(end) - -comment(# we use a class with a class variable store the private cache) -comment(# for the results of the factorial function.) -reserved(class) class(Factorial) - class_variable(@@fact) operator(=) operator([) integer(1) operator(]) - reserved(def) constant(Factorial)operator(.)ident(compute)operator(()ident(n)operator(\)) - reserved(if) class_variable(@@fact)operator([)ident(n)operator(]) - class_variable(@@fact)operator([)ident(n)operator(]) - reserved(else) - class_variable(@@fact)operator([)ident(n)operator(]) operator(=) ident(n) operator(*) constant(Factorial)operator(.)ident(compute)operator(()ident(n) operator(-) integer(1)operator(\)) - reserved(end) - reserved(end) -reserved(end) - -comment(#---------------------------------------------------------) -comment(# Example 4-4- mjd-permute) -comment(# n2pat(n, len\): produce the N-th pattern of length len) - -comment(# We must use a lower case letter as parameter N, otherwise it is) -comment(# handled as constant Length is the length of the resulting) -comment(# array, not the index of the last element (length -1\) like in) -comment(# the perl example.) -reserved(def) method(n2pat)operator(()ident(n)operator(,) ident(length)operator(\)) - ident(pat) operator(=) operator([)operator(]) - ident(i) operator(=) integer(1) - reserved(while) ident(i) operator(<=) ident(length) - ident(pat)operator(.)ident(push)operator(()ident(n) operator(%) ident(i)operator(\)) - ident(n) regexp 0 - perm -end - -def n2perm(n, len\) - pat2perm(n2pat(n,len\)\) -end - -# In ruby the main program must be after all definitions -while gets - data = split - # the perl solution has used $#data, which is length-1 - num_permutations = Factorial.compute(data.length(\)\) - 0.upto(num_permutations - 1\) do |i| - # in ruby we can not use an array as selector for an array - # but by exchanging the two arrays, we can use the collect method - # which returns an array with the result of all block invocations - permutation = n2perm(i, data.length\).collect { - |j| data[j] - } - puts permutation.join(" "\) - end -end - - -# @@PLEAC@@_5.0 -age = { "Nat", 24, - "Jules", 25, - "Josh", 17 } - -age["Nat"] = 24 -age["Jules"] = 25 -age["Josh"] = 17 - -food_color = { - "Apple" => "red", - "Banana" => "yellow", - "Lemon" => "yellow", - "Carrot" => "orange" - } - -# In Ruby, you cannot avoid the double or simple quoting -# while manipulatin hashes - - -# @@PLEAC@@_5.1 -hash[key] = value - -food_color["Raspberry"] = "pink" -puts "Known foods:", food_color.keys - - -# @@PLEAC@@_5.2 -# does hash have a value for key ? -if (hash.has_key?(key\)\) - # it exists -else - # it doesn't -end - -[ "Banana", "Martini" ].each { |name| - print name, " is a ", food_color.has_key?(name\) ? "food" : "drink", ")char(\\n)content(" -} - -age = {} -age['Toddler'] = 3 -age['Unborn'] = 0 -age['Phantasm'] = nil - -for thing in ['Toddler', 'Unborn', 'Phantasm', 'Relic'] - print ")inlinecontent(: " - print "Has-key " if age.has_key?(thing\) - print "True " if age[thing] - print "Nonzero " if age[thing] && age[thing].nonzero? - print ")char(\\n)content(" -end - -#=> -# Toddler: Has-key True Nonzero -# Unborn: Has-key True -# Phantasm: Has-key -# Relic: - -# You use Hash#has_key? when you use Perl's exists -> it checks -# for existence of a key in a hash. -# All Numeric are "True" in ruby, so the test doesn't have the -# same semantics as in Perl; you would use Numeric#nonzero? to -# achieve the same semantics (false if 0, true otherwise\). - - -# @@PLEAC@@_5.3 -food_color.delete("Banana"\) - - -# @@PLEAC@@_5.4 -hash.each { |key, value| - # do something with key and value -} - -hash.each_key { |key| - # do something with key -} - -food_color.each { |food, color| - puts ")inlinecontent( is )inlinecontent(" -} - -food_color.each_key { |food| - puts ")inlinecontent( is )inlinecontent(" -} - -# IMO this demonstrates that OO style is by far more readable -food_color.keys.sort.each { |food| - puts ")inlinecontent( is )inlinecontent(." -} - -#----------------------------- -#!)delimiter(/)modifier(us)>ident(r)operator(/)ident(bin)operator(/)ident(ruby) -comment(# countfrom - count number of messages from each sender) - -comment(# Default value is 0) -ident(from) operator(=) constant(Hash)operator(.)ident(new)operator(()integer(0)operator(\)) -reserved(while) ident(gets) - regexp reserved(and) ident(from)operator([)global_variable($1)operator(]) operator(+=) integer(1) -reserved(end) - -comment(# More useful to sort by number of received mail by person) -ident(from)operator(.)ident(sort) operator({)operator(|)ident(a)operator(,)ident(b)operator(|) ident(b)operator([)integer(1)operator(])operator(<=>)ident(a)operator([)integer(1)operator(])operator(})operator(.)ident(each) operator({) operator(|)ident(v)operator(|) - ident(puts) stringcontent(: )inlinedelimiter(")> -operator(}) -comment(#-----------------------------) - - -comment(# @@PLEAC@@_5.5) -comment(# You may use the built-in 'inspect' method this way:) -ident(p) ident(hash) - -comment(# Or do it the Cookbook way:) -ident(hash)operator(.)ident(each) operator({) operator(|)ident(k)operator(,)ident(v)operator(|) ident(puts) stringcontent( => )inlinedelimiter(")> operator(}) - -comment(# Sorted by keys) -ident(hash)operator(.)ident(sort)operator(.)ident(each) operator({) operator(|)ident(e)operator(|) ident(puts) stringcontent( => )inlinedelimiter(")> operator(}) -comment(# Sorted by values) -ident(hash)operator(.)ident(sort)operator({)operator(|)ident(a)operator(,)ident(b)operator(|) ident(a)operator([)integer(1)operator(])operator(<=>)ident(b)operator([)integer(1)operator(])operator(})operator(.)ident(each) operator({) operator(|)ident(e)operator(|) ident(puts) stringcontent( => )inlinedelimiter(")> operator(}) - - -comment(# @@PLEAC@@_5.7) -ident(ttys) operator(=) constant(Hash)operator(.)ident(new) -reserved(for) ident(i) reserved(in) shell - ident(user)operator(,) ident(tty) operator(=) ident(i)operator(.)ident(split) - operator(()ident(ttys)operator([)ident(user)operator(]) operator(||=) operator([)operator(])operator(\)) operator(<<) ident(tty) comment(# see problems_ruby for more infos) -reserved(end) -ident(ttys)operator(.)ident(keys)operator(.)ident(sort)operator(.)ident(each) operator({) operator(|)ident(k)operator(|) - ident(puts) stringcontent(: )inlinedelimiter(")> comment(# from 4.2) -operator(}) - - -comment(# @@PLEAC@@_5.8) -ident(surname) operator(=) operator({) string operator(=)operator(>) stringoperator(,) string operator(=)operator(>) string operator(}) -ident(puts) ident(surname)operator(.)ident(index)operator(()stringoperator(\)) - -comment(# If you really needed to 'invert' the whole hash, use Hash#invert) - -comment(#-----------------------------) -comment(#!/usr/bin/ruby -w) -comment(# foodfind - find match for food or color) - -ident(given) operator(=) pre_constant(ARGV)operator(.)ident(shift) reserved(or) ident(raise) string - -ident(color) operator(=) operator({) - string operator(=)operator(>) stringoperator(,) - string operator(=)operator(>) stringoperator(,) - string operator(=)operator(>) stringoperator(,) - string operator(=)operator(>) stringoperator(,) -operator(}) - -reserved(if) operator(()ident(color)operator(.)ident(has_key?)operator(()ident(given)operator(\))operator(\)) - ident(puts) stringcontent( is a food with color )inlinecontent(.)delimiter(")> -reserved(end) -reserved(if) operator(()ident(color)operator(.)ident(has_value?)operator(()ident(given)operator(\))operator(\)) - ident(puts) stringcontent( is a food with color )inlinecontent(.)delimiter(")> -reserved(end) -comment(#-----------------------------) - - -comment(# @@PLEAC@@_5.9) -comment(# Sorted by keys (Hash#sort gives an Array of pairs made of each key,value\)) -ident(food_color)operator(.)ident(sort)operator(.)ident(each) operator({) operator(|)ident(f)operator(|) - ident(puts) stringcontent( is )inlinecontent(.)delimiter(")> -operator(}) - -comment(# Sorted by values) -ident(food_color)operator(.)ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(a)operator([)integer(1)operator(]) operator(<=>) ident(b)operator([)integer(1)operator(]) operator(})operator(.)ident(each) operator({) operator(|)ident(f)operator(|) - ident(puts) stringcontent( is )inlinecontent(.)delimiter(")> -operator(}) - -comment(# Sorted by length of values) -ident(food_color)operator(.)ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(a)operator([)integer(1)operator(])operator(.)ident(length) operator(<=>) ident(b)operator([)integer(1)operator(])operator(.)ident(length) operator(})operator(.)ident(each) operator({) operator(|)ident(f)operator(|) - ident(puts) stringcontent( is )inlinecontent(.)delimiter(")> -operator(}) - - -comment(# @@PLEAC@@_5.10) -ident(merged) operator(=) ident(a)operator(.)ident(clone)operator(.)ident(update)operator(()ident(b)operator(\)) comment(# because Hash#update changes object in place) - -ident(drink_color) operator(=) operator({) string operator(=)operator(>) stringoperator(,) string operator(=)operator(>) string operator(}) -ident(ingested_color) operator(=) ident(drink_color)operator(.)ident(clone)operator(.)ident(update)operator(()ident(food_color)operator(\)) - -ident(substance_color) operator(=) operator({)operator(}) -reserved(for) ident(i) reserved(in) operator([) ident(food_color)operator(,) ident(drink_color) operator(]) - ident(i)operator(.)ident(each_key) operator({) operator(|)ident(k)operator(|) - reserved(if) ident(substance_color)operator(.)ident(has_key?)operator(()ident(k)operator(\)) - ident(puts) stringcontent( seen twice. Using the first definition.)delimiter(")> - reserved(next) - reserved(end) - ident(substance_color)operator([)ident(k)operator(]) operator(=) integer(1) - operator(}) -reserved(end) - - -comment(# @@PLEAC@@_5.11) -ident(common) operator(=) ident(hash1)operator(.)ident(keys) operator(&) ident(hash2)operator(.)ident(keys) - -ident(this_not_that) operator(=) ident(hash1)operator(.)ident(keys) operator(-) ident(hash2)operator(.)ident(keys) - - -comment(# @@PLEAC@@_5.12) -comment(# no problem here, Ruby handles any kind of object for key-ing) -comment(# (it takes Object#hash, which defaults to Object#id\)) - - -comment(# @@PLEAC@@_5.13) -comment(# AFAIK, not possible in Ruby) - - -comment(# @@PLEAC@@_5.14) -comment(# Be careful, the following is possible only because Fixnum objects are) -comment(# special (documentation says: there is effectively only one Fixnum object) -comment(# instance for any given integer value\).) -ident(count) operator(=) constant(Hash)operator(.)ident(new)operator(()integer(0)operator(\)) -ident(array)operator(.)ident(each) operator({) operator(|)ident(e)operator(|) - ident(count)operator([)ident(e)operator(]) operator(+=) integer(1) -operator(}) - - -comment(# @@PLEAC@@_5.15) -ident(father) operator(=) operator({) - string operator(,) stringoperator(,) - string operator(,) stringoperator(,) - string operator(,) stringoperator(,) - string operator(,) stringoperator(,) - string operator(,) stringoperator(,) - string operator(,) stringoperator(,) - string operator(,) stringoperator(,) - string operator(,) stringoperator(,) - string operator(,) stringoperator(,) - string operator(,) stringoperator(,) - string operator(,) stringoperator(,) - string operator(,) stringoperator(,) -operator(}) - -reserved(while) ident(gets) - ident(chomp) - reserved(begin) - ident(print) global_variable($_)operator(,) string - reserved(end) reserved(while) global_variable($_) operator(=) ident(father)operator([)global_variable($_)operator(]) - ident(puts) -reserved(end) - -ident(children) operator(=) operator({)operator(}) -ident(father)operator(.)ident(each) operator({) operator(|)ident(k)operator(,)ident(v)operator(|) - operator(()ident(children)operator([)ident(v)operator(]) operator(||=) operator([)operator(])operator(\)) operator(<<) ident(k) -operator(}) -reserved(while) ident(gets) - ident(chomp) - ident(puts) stringcontent( begat )inlineoperator(])operator(\))operator(.)ident(join)operator(()stringoperator(\))delimiter(})>content(.)char(\\n)delimiter(")> -reserved(end) - -ident(includes) operator(=) operator({)operator(}) -ident(files)operator(.)ident(each) operator({) operator(|)ident(f)operator(|) - reserved(begin) - reserved(for) ident(l) reserved(in) constant(IO)operator(.)ident(readlines)operator(()ident(f)operator(\)) - reserved(next) reserved(unless) ident(l) operator(=)operator(~) regexp]+\)>)delimiter(/)> - operator(()ident(includes)operator([)global_variable($1)operator(]) operator(||=) operator([)operator(])operator(\)) operator(<<) ident(f) - reserved(end) - reserved(rescue) constant(SystemCallError) - global_variable($stderr)operator(.)ident(puts) string - reserved(end) -operator(}) - -ident(include_free) operator(=) ident(includes)operator(.)ident(values)operator(.)ident(flatten)operator(.)ident(uniq) operator(-) ident(includes)operator(.)ident(keys) - - -comment(# @@PLEAC@@_5.16) -comment(# dutree - print sorted intented rendition of du output) -comment(#% dutree) -comment(#% dutree /usr) -comment(#% dutree -a) -comment(#% dutree -a /bin) - -comment(# The DuNode class collects all information about a directory,) -comment(# and provides some convenience methods) -reserved(class) class(DuNode) - - ident(attr_reader) symbol(:name) - ident(attr_accessor) symbol(:size) - ident(attr_accessor) symbol(:kids) - - reserved(def) method(initialize)operator(()ident(name)operator(\)) - instance_variable(@name) operator(=) ident(name) - instance_variable(@kids) operator(=) operator([)operator(]) - instance_variable(@size) operator(=) integer(0) - reserved(end) - - comment(# support for sorting nodes with side) - reserved(def) method(size_compare)operator(()ident(node2)operator(\)) - instance_variable(@size) operator(<=>) ident(node2)operator(.)ident(size) - reserved(end) - - reserved(def) method(basename) - instance_variable(@name)operator(.)ident(sub)operator(()regexpoperator(,) stringoperator(\)) - reserved(end) - - comment(#returns substring before last "/", nil if not there) - reserved(def) method(parent) - ident(p) operator(=) instance_variable(@name)operator(.)ident(sub)operator(()regexpoperator(,)stringoperator(\)) - reserved(if) ident(p) operator(==) instance_variable(@name) - pre_constant(nil) - reserved(else) - ident(p) - reserved(end) - reserved(end) - -reserved(end) - -comment(# The DuTree does the acdtual work of) -comment(# getting the input, parsing it, builging up a tree) -comment(# and format it for output) -reserved(class) class(Dutree) - - ident(attr_reader) symbol(:topdir) - - reserved(def) method(initialize) - instance_variable(@nodes) operator(=) constant(Hash)operator(.)ident(new) - instance_variable(@dirsizes) operator(=) constant(Hash)operator(.)ident(new)operator(()integer(0)operator(\)) - instance_variable(@kids) operator(=) constant(Hash)operator(.)ident(new)operator(()operator([)operator(])operator(\)) - reserved(end) - - comment(# get a node by name, create it if it does not exist yet) - reserved(def) method(get_create_node)operator(()ident(name)operator(\)) - reserved(if) instance_variable(@nodes)operator(.)ident(has_key?)operator(()ident(name)operator(\)) - instance_variable(@nodes)operator([)ident(name)operator(]) - reserved(else) - ident(node) operator(=) constant(DuNode)operator(.)ident(new)operator(()ident(name)operator(\)) - instance_variable(@nodes)operator([)ident(name)operator(]) operator(=) ident(node) - ident(node) - reserved(end) - reserved(end) - - comment(# run du, read in input, save sizes and kids) - comment(# stores last directory read in instance variable topdir) - reserved(def) method(input)operator(()ident(arguments)operator(\)) - ident(name) operator(=) string - ident(cmd) operator(=) string operator(+) ident(arguments)operator(.)ident(join)operator(()stringoperator(\)) - constant(IO)operator(.)ident(popen)operator(()ident(cmd)operator(\)) operator({) operator(|)ident(pipe)operator(|) - ident(pipe)operator(.)ident(each) operator({) operator(|)ident(line)operator(|) - ident(size)operator(,) ident(name) operator(=) ident(line)operator(.)ident(chomp)operator(.)ident(split)operator(()regexpoperator(,) integer(2)operator(\)) - ident(node) operator(=) ident(get_create_node)operator(()ident(name)operator(\)) - ident(node)operator(.)ident(size) operator(=) ident(size)operator(.)ident(to_i) - instance_variable(@nodes)operator([)ident(name)operator(]) operator(=) ident(node) - ident(parent) operator(=) ident(node)operator(.)ident(parent) - reserved(if) ident(parent) - ident(get_create_node)operator(()ident(parent)operator(\))operator(.)ident(kids)operator(.)ident(push)operator(()ident(node)operator(\)) - reserved(end) - operator(}) - operator(}) - instance_variable(@topdir) operator(=) instance_variable(@nodes)operator([)ident(name)operator(]) - reserved(end) - - comment(# figure out how much is taken in each directory) - comment(# that isn't stored in the subdirectories. Add a new) - comment(# fake kid called "." containing that much.) - reserved(def) method(get_dots)operator(()ident(node)operator(\)) - ident(cursize) operator(=) ident(node)operator(.)ident(size) - reserved(for) ident(kid) reserved(in) ident(node)operator(.)ident(kids) - ident(cursize) operator(-=) ident(kid)operator(.)ident(size) - ident(get_dots)operator(()ident(kid)operator(\)) - reserved(end) - reserved(if) ident(node)operator(.)ident(size) operator(!=) ident(cursize) - ident(newnode) operator(=) ident(get_create_node)operator(()ident(node)operator(.)ident(name) operator(+) stringoperator(\)) - ident(newnode)operator(.)ident(size) operator(=) ident(cursize) - ident(node)operator(.)ident(kids)operator(.)ident(push)operator(()ident(newnode)operator(\)) - reserved(end) - reserved(end) - - comment(# recursively output everything) - comment(# passing padding and number width as well) - comment(# on recursive calls) - reserved(def) method(output)operator(()ident(node)operator(,) ident(prefix)operator(=)stringoperator(,) ident(width)operator(=)integer(0)operator(\)) - ident(line) operator(=) ident(sprintf)operator(()stringcontent(d %s)delimiter(")>operator(,) ident(node)operator(.)ident(size)operator(,) ident(node)operator(.)ident(basename)operator(\)) - ident(puts)operator(()ident(prefix) operator(+) ident(line)operator(\)) - ident(prefix) operator(+=) ident(line)operator(.)ident(sub)operator(()regexpoperator(,) stringoperator(\)) - ident(prefix)operator(.)ident(gsub!)operator(()regexpoperator(,) stringoperator(\)) - reserved(if) ident(node)operator(.)ident(kids)operator(.)ident(length) operator(>) integer(0) comment(# not a bachelor node) - ident(kids) operator(=) ident(node)operator(.)ident(kids) - ident(kids)operator(.)ident(sort!) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) - ident(b)operator(.)ident(size_compare)operator(()ident(a)operator(\)) - operator(}) - ident(width) operator(=) ident(kids)operator([)integer(0)operator(])operator(.)ident(size)operator(.)ident(to_s)operator(.)ident(length) - reserved(for) ident(kid) reserved(in) ident(kids) - ident(output)operator(()ident(kid)operator(,) ident(prefix)operator(,) ident(width)operator(\)) - reserved(end) - reserved(end) - reserved(end) - -reserved(end) - -ident(tree) operator(=) constant(Dutree)operator(.)ident(new) -ident(tree)operator(.)ident(input)operator(()pre_constant(ARGV)operator(\)) -ident(tree)operator(.)ident(get_dots)operator(()ident(tree)operator(.)ident(topdir)operator(\)) -ident(tree)operator(.)ident(output)operator(()ident(tree)operator(.)ident(topdir)operator(\)) - - -comment(# @@PLEAC@@_6.0) -comment(# The verbose version are match, sub, gsub, sub! and gsub!;) -comment(# pattern needs to be a Regexp object; it yields a MatchData) -comment(# object.) -ident(pattern)operator(.)ident(match)operator(()ident(string)operator(\)) -ident(string)operator(.)ident(sub)operator(()ident(pattern)operator(,) ident(replacement)operator(\)) -ident(string)operator(.)ident(gsub)operator(()ident(pattern)operator(,) ident(replacement)operator(\)) -comment(# As usual in Ruby, sub! does the same as sub but also modifies) -comment(# the object, the same for gsub!/gsub.) - -comment(# Sugared syntax yields the position of the match (or nil if no) -comment(# match\). Note that the object at the right of the operator needs) -comment(# not to be a Regexp object (it can be a String\). The "dont) -comment(# match" operator yields true or false.) -ident(meadow) operator(=)operator(~) regexp comment(# position of the match, nil if no match) -ident(meadow) operator(!)operator(~) regexp comment(# true if doesn't match, false if it does) -comment(# There is no sugared version for the substitution) - -ident(meadow) operator(=)operator(~) regexp reserved(and) ident(print) string - -ident(string) operator(=) string -ident(string)operator(.)ident(sub!)operator(()regexpoperator(,) stringoperator(\)) - -comment(# % echo ababacaca | ruby -ne 'puts $& if /(a|ba|b\)+(a|ac\)+/') -comment(# ababa) - -comment(# The "global" (or "multiple"\) match is handled by String#scan) -ident(scan) operator(()regexpoperator(\)) operator({) - ident(puts) stringdelimiter(")> -operator(}) - -comment(# String#scan yields an Array if not used with a block) -ident(numbers) operator(=) ident(scan)operator(()regexpoperator(\)) - -ident(digits) operator(=) string -ident(nonlap) operator(=) ident(digits)operator(.)ident(scan)operator(()regexpoperator(\)) -ident(yeslap) operator(=) ident(digits)operator(.)ident(scan)operator(()regexpoperator(\)) -ident(puts) stringoperator(\))delimiter(})>delimiter(")> -ident(puts) stringoperator(\))delimiter(})>delimiter(")>operator(;) -comment(# Non-overlapping: 123 456 789) -comment(# Overlapping: 123 234 345 456 567 678 789) - -ident(string) operator(=) string -ident(string) operator(=)operator(~) regexp -ident(puts) string -comment(# (And \) (little lambs\) ( eat ivy\)) - - -comment(# @@PLEAC@@_6.1) -comment(# Ruby doesn't have the same problem:) -ident(dst) operator(=) ident(src)operator(.)ident(sub)operator(()stringoperator(,) stringoperator(\)) - -ident(progname) operator(=) global_variable($0)operator(.)ident(sub)operator(()stringoperator(,) stringoperator(\)) - -ident(bindirs) operator(=) string -ident(libdirs) operator(=) ident(bindirs)operator(.)ident(map) operator({) operator(|)ident(l)operator(|) ident(l)operator(.)ident(sub)operator(()stringoperator(,) stringoperator(\)) operator(}) - - -comment(# @@PLEAC@@_6.3) -regexp comment(# as many non-whitespace bytes as possible) -regexp comment(# as many letters, apostrophes, and hyphens) - -regexp comment(# usually best) -regexp comment(# fails at ends or w/ punctuation) - - -comment(# @@PLEAC@@_6.4) -ident(require) string -ident(str) operator(=) string -ident(re) operator(=) regexp comment(# /x for nice formatting) - -ident(str)operator(.)ident(gsub!) ident(re) reserved(do) comment(# pass a block to execute replacement) - ident(host) operator(=) constant(TCPsocket)operator(.)ident(gethostbyname)operator(()global_variable($1)operator(\)) - stringcontent( [)inlinecontent(])delimiter(")> -reserved(end) - -ident(puts) ident(str) -comment(#-----------------------------) -comment(# to match whitespace or #-characters in an extended re you need to escape) -comment(# them.) - -ident(foo) operator(=) integer(42) -ident(str) operator(=) string -ident(str)operator(.)ident(gsub!) regexp reserved(do) - ident(eval) global_variable($1) comment(# with the value of a local variable) - reserved(end) -ident(puts) ident(str) comment(# => blah 42 blah) - - -comment(# @@PLEAC@@_6.5) -comment(# The 'g' modifier doesn't exist in Ruby, a regexp can't be used) -comment(# directly in a while loop; instead, use String#scan { |match| .. }) -ident(fish) operator(=) string -constant(WANT) operator(=) integer(3) -ident(count) operator(=) integer(0) -ident(fish)operator(.)ident(scan)operator(()regexpoperator(\)) operator({) - reserved(if) operator(()ident(count) operator(+=) integer(1)operator(\)) operator(==) constant(WANT) - ident(puts) stringcontent( one.)delimiter(")> - reserved(end) -operator(}) - -reserved(if) ident(fish) operator(=)operator(~) regexp - ident(puts) stringcontent( one.)delimiter(")> -reserved(end) - -ident(pond) operator(=) string -comment(# String#scan without a block gives an array of matches, each match) -comment(# being an array of all the specified groups) -ident(colors) operator(=) ident(pond)operator(.)ident(scan)operator(()regexpoperator(\))operator(.)ident(flatten) comment(# get all matches) -ident(color) operator(=) ident(colors)operator([)integer(2)operator(]) comment(# then the one we want) -comment(# or without a temporary array) -ident(color) operator(=) ident(pond)operator(.)ident(scan)operator(()regexpoperator(\))operator(.)ident(flatten)operator([)integer(2)operator(]) comment(# just grab element 3) -ident(puts) stringcontent(.)delimiter(")> - -ident(count) operator(=) integer(0) -ident(fishes) operator(=) string -ident(evens) operator(=) ident(fishes)operator(.)ident(scan)operator(()regexpoperator(\))operator(.)ident(select) operator({) operator(()ident(count)operator(+=)integer(1)operator(\)) operator(%) integer(2) operator(==) integer(0) operator(}) -ident(print) stringoperator(\))delimiter(})>content(.)delimiter(")> - -ident(count) operator(=) integer(0) -ident(fishes)operator(.)ident(gsub)operator(()regexpoperator(\)) operator({) - reserved(if) operator(()ident(count) operator(+=) integer(1)operator(\)) operator(==) integer(4) - string operator(+) global_variable($2) - reserved(else) - global_variable($1) operator(+) global_variable($2) - reserved(end) -operator(}) - -ident(pond) operator(=) string -ident(puts) stringoperator(\))operator(.)ident(flatten)operator([)operator(-)integer(1)operator(])delimiter(})>delimiter(")> - -regexp - -comment(# The "s" perl modifier is "m" in Ruby (not very nice since there is) -comment(# also an "m" in perl..\)) -ident(pond) operator(=) string -reserved(if) operator(()ident(pond) operator(=)operator(~) regexpoperator(\)) - ident(puts) stringcontent(.)delimiter(")> -reserved(else) - ident(puts) string -reserved(end) - - -comment(# @@PLEAC@@_6.6) -comment(#-----------------------------) -comment(#!/usr/bin/ruby -w) -comment(# killtags - very bad html killer) -global_variable($/) operator(=) pre_constant(nil)operator(;) comment(# each read is whole file) -reserved(while) ident(file) operator(=) ident(gets)operator(()operator(\)) reserved(do) - ident(file)operator(.)ident(gsub!)operator(()regexp)delimiter(/)modifier(m)>operator(,)stringoperator(\))operator(;) comment(# strip tags (terribly\)) - ident(puts) ident(file) comment(# print file to STDOUT) -reserved(end) -comment(#-----------------------------) -comment(#!/usr/bin/ruby -w) -comment(#headerfy - change certain chapter headers to html) -global_variable($/) operator(=) string -reserved(while) ident(file) operator(=) ident(gets)operator(()operator(\)) reserved(do) - ident(pattern) operator(=) regexp - ident(puts) ident(file)operator(.)ident(gsub)operator(()ident(pattern)operator(,)string)content(\\1)content()delimiter(')>operator(\)) -reserved(end) -comment(#-----------------------------) -comment(#% ruby -00pe "gsub!(/\\A(Chapter\\s+\\d+\\s*:.*\)/,'

\\1

'\)" datafile) - -comment(#!/usr/bin/ruby -w) -comment(#-----------------------------) -reserved(for) ident(file) reserved(in) pre_constant(ARGV) - ident(file) operator(=) constant(File)operator(.)ident(open)operator(()pre_constant(ARGV)operator(.)ident(shift)operator(\)) - reserved(while) ident(file)operator(.)ident(gets)operator(()stringoperator(\)) reserved(do) comment(# each read is a paragraph) - ident(print) stringcontent( in $ARGV has <<)inlinecontent(>>)char(\\n)delimiter(")> reserved(while) regexp - reserved(end) comment(# /m activates the multiline mode) -reserved(end) -comment(#-----------------------------) - -comment(# @@PLEAC@@_6.7) -comment(#-----------------------------) -global_variable($/) operator(=) pre_constant(nil)operator(;) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()stringoperator(\)) -ident(chunks) operator(=) ident(file)operator(.)ident(gets)operator(.)ident(split)operator(()regexpoperator(\)) -comment(#-----------------------------) -comment(# .Ch, .Se and .Ss divide chunks of STDIN) -ident(chunks) operator(=) ident(gets)operator(()pre_constant(nil)operator(\))operator(.)ident(split)operator(()regexpoperator(\)) -ident(print) stringcontent( chunks.)char(\\n)delimiter(")> -comment(#-----------------------------) - - -comment(# @@PLEAC@@_6.8) -reserved(while) ident(gets) - reserved(if) operator(~)regexp operator(..) operator(~)regexp - comment(# line falls between BEGIN and END inclusive) - reserved(end) -reserved(end) - -reserved(while) ident(gets) - reserved(if) operator(()global_variable($.) operator(==) ident(firstnum)operator(\)) operator(..) operator(()global_variable($.) operator(==) ident(lastnum)operator(\)) - comment(# operate between firstnum and lastnum line number) - reserved(end) -reserved(end) - -comment(# in ruby versions prior to 1.8, the above two conditional) -comment(# expressions could be shortened to:) -comment(# if /BEGIN/ .. /END/) -comment(# and) -comment(# if firstnum .. lastnum) -comment(# but these now only work this way from the command line) - -comment(#-----------------------------) - -reserved(while) ident(gets) - reserved(if) operator(~)regexp operator(...) operator(~)regexp - comment(# line falls between BEGIN and END on different lines) - reserved(end) -reserved(end) - -reserved(while) ident(gets) - reserved(if) operator(()global_variable($.) operator(==) ident(first)operator(\)) operator(...) operator(()global_variable($.) operator(==) ident(last)operator(\)) - comment(# operate between first and last line number on different lines) - reserved(end) -reserved(end) - -comment(#-----------------------------) -comment(# command-line to print lines 15 through 17 inclusive (see below\)) -ident(ruby) operator(-)ident(ne) string ident(datafile) - -comment(# print out all .. displays from HTML doc) -reserved(while) ident(gets) - ident(print) reserved(if) operator(~)regexp)delimiter(#)modifier(i)> operator(..) operator(~)regexp)delimiter(#)modifier(i)>operator(;) -reserved(end) - -comment(# same, but as shell command) -comment(# ruby -ne 'print if %r##i .. %r##i' document.html) -comment(#-----------------------------) -comment(# ruby -ne 'BEGIN { $top=3; $bottom=5 }; \\) -comment(# print if $top .. $bottom' /etc/passwd # FAILS) -comment(# ruby -ne 'BEGIN { $top=3; $bottom=5 }; \\) -comment(# print if $. == $top .. $. == $bottom' /etc/passwd # works) -comment(# ruby -ne 'print if 3 .. 5' /etc/passwd # also works) -comment(#-----------------------------) -ident(print) reserved(if) operator(~)regexp operator(..) operator(~)regexpoperator(;) -ident(print) reserved(if) operator(~)regexp operator(...) operator(~)regexpoperator(;) -comment(#-----------------------------) -reserved(while) ident(gets) - global_variable($in_header) operator(=) global_variable($.) operator(==) integer(1) operator(..) operator(~)regexp operator(?) pre_constant(true) operator(:) pre_constant(false) - global_variable($in_body) operator(=) operator(~)regexp operator(..) pre_constant(ARGF)operator(.)ident(eof) operator(?) pre_constant(true) operator(:) pre_constant(false) -reserved(end) -comment(#-----------------------------) -ident(seen) operator(=) operator({)operator(}) -pre_constant(ARGF)operator(.)ident(each) reserved(do) operator(|)ident(line)operator(|) - reserved(next) reserved(unless) ident(line) operator(=)operator(~) regexp operator(..) ident(line) operator(=)operator(~) regexpoperator(;) - ident(line)operator(.)ident(scan)operator(()regexp(\),;)char(\\s)content(]+)char(\\@)content([^<>(\),;)char(\\s)content(]+\))delimiter(/)>operator(\))operator(.)ident(each) reserved(do) operator(|)ident(addr)operator(|) - ident(puts) ident(addr) reserved(unless) ident(seen)operator([)ident(addr)operator(]) - ident(seen)operator([)ident(addr)operator(]) operator(||=) integer(1) - reserved(end) -reserved(end) - - -comment(# @@PLEAC@@_6.9) -reserved(def) method(glob2pat)operator(()ident(globstr)operator(\)) - ident(patmap) operator(=) operator({) - string operator(=)operator(>) stringoperator(,) - string operator(=)operator(>) stringoperator(,) - string operator(=)operator(>) stringoperator(,) - string operator(=)operator(>) stringoperator(,) - operator(}) - ident(globstr)operator(.)ident(gsub!)operator(()regexpoperator(\)) operator({) operator(|)ident(c)operator(|) ident(patmap)operator([)ident(c)operator(]) operator(||) constant(Regexp)operator(::)ident(escape)operator(()ident(c)operator(\)) operator(}) - string operator(+) ident(globstr) operator(+) string -reserved(end) - - -comment(# @@PLEAC@@_6.10) -comment(# avoid interpolating patterns like this if the pattern) -comment(# isn't going to change:) -ident(pattern) operator(=) pre_constant(ARGV)operator(.)ident(shift) -pre_constant(ARGF)operator(.)ident(each) reserved(do) operator(|)ident(line)operator(|) - ident(print) ident(line) reserved(if) ident(line) operator(=)operator(~) regexpdelimiter(/)> -reserved(end) - -comment(# the above creates a new regex each iteration. Instead,) -comment(# use the /o modifier so the regex is compiled only once) - -ident(pattern) operator(=) pre_constant(ARGV)operator(.)ident(shift) -pre_constant(ARGF)operator(.)ident(each) reserved(do) operator(|)ident(line)operator(|) - ident(print) ident(line) reserved(if) ident(line) operator(=)operator(~) regexpdelimiter(/)modifier(o)> -reserved(end) - -comment(#-----------------------------) - -comment(#!/usr/bin/ruby) -comment(# popgrep1 - grep for abbreviations of places that say "pop") -comment(# version 1: slow but obvious way) -ident(popstates) operator(=) string -pre_constant(ARGF)operator(.)ident(each) reserved(do) operator(|)ident(line)operator(|) - ident(popstates)operator(.)ident(each) reserved(do) operator(|)ident(state)operator(|) - reserved(if) ident(line) operator(=)operator(~) regexpchar(\\b)delimiter(/)> - ident(print) ident(line) - ident(last) - reserved(end) - reserved(end) -reserved(end) - -comment(#-----------------------------) -comment(#!/usr/bin/ruby) -comment(# popgrep2 - grep for abbreviations of places that say "pop") -comment(# version 2: eval strings; fast but hard to quote) -ident(popstates) operator(=) string -ident(code) operator(=) string -ident(popstates)operator(.)ident(each) reserved(do) operator(|)ident(state)operator(|) - ident(code) operator(+=) stringchar(\\\\)content(b/; print(line\); next; end)char(\\n)delimiter(")> -reserved(end) -ident(code) operator(+=) string -ident(print) stringchar(\\n)content(---)char(\\n)delimiter(")> reserved(if) pre_constant(false) comment(# turn on for debugging) -ident(eval) ident(code) - -comment(# CODE IS) -comment(# ---) -comment(# ARGF.each do |line|) -comment(# if line =~ /\\bCO\\b/; print(line\); next; end) -comment(# if line =~ /\\bON\\b/; print(line\); next; end) -comment(# if line =~ /\\bMI\\b/; print(line\); next; end) -comment(# if line =~ /\\bWI\\b/; print(line\); next; end) -comment(# if line =~ /\\bMN\\b/; print(line\); next; end) -comment(# end) -comment(#) -comment(# ---) - -comment(## alternatively, the same idea as above but compiling) -comment(## to a case statement: (not in perlcookbook\)) -comment(#!/usr/bin/ruby -w) -comment(# popgrep2.5 - grep for abbreviations of places that say "pop") -comment(# version 2.5: eval strings; fast but hard to quote) -ident(popstates) operator(=) string -ident(code) operator(=) string -ident(popstates)operator(.)ident(each) reserved(do) operator(|)ident(state)operator(|) - ident(code) operator(+=) stringchar(\\\\)content(b/ : print line)char(\\n)delimiter(")> -reserved(end) -ident(code) operator(+=) string -ident(print) stringchar(\\n)content(---)char(\\n)delimiter(")> reserved(if) pre_constant(false) comment(# turn on for debugging) -ident(eval) ident(code) - -comment(# CODE IS) -comment(# ---) -comment(# ARGF.each do |line|) -comment(# case line) -comment(# when /\\bCO\\b/ : print line) -comment(# when /\\bON\\b/ : print line) -comment(# when /\\bMI\\b/ : print line) -comment(# when /\\bWI\\b/ : print line) -comment(# when /\\bMN\\b/ : print line) -comment(# end) -comment(# end) -comment(#) -comment(# ---) - -comment(# Note: (above\) Ruby 1.8+ allows the 'when EXP : EXPR' on one line) -comment(# with the colon separator.) - -comment(#-----------------------------) -comment(#!/usr/bin/ruby) -comment(# popgrep3 - grep for abbreviations of places that say "pop") -comment(# version3: build a match_any function) -ident(popstates) operator(=) string -ident(expr) operator(=) ident(popstates)operator(.)ident(map)operator({)operator(|)ident(e)operator(|)stringchar(\\\\)content(b/)delimiter(")>operator(})operator(.)ident(join)operator(()stringoperator(\)) -ident(eval) stringcontent(;end)delimiter(")> -pre_constant(ARGF)operator(.)ident(each) reserved(do) operator(|)ident(line)operator(|) - ident(print) ident(line) reserved(if) ident(match_any)operator(()ident(line)operator(\)) -reserved(end) -comment(#-----------------------------) - -comment(## building a match_all function is a trivial) -comment(## substitution of && for ||) -comment(## here is a generalized example:) -comment(#!/usr/bin/ruby -w) -comment(## grepauth - print lines that mention both foo and bar) -reserved(class) class(MultiMatch) - reserved(def) method(initialize)operator(()operator(*)ident(patterns)operator(\)) - ident(_any) operator(=) ident(build_match)operator(()stringoperator(,)ident(patterns)operator(\)) - ident(_all) operator(=) ident(build_match)operator(()stringoperator(,)ident(patterns)operator(\)) - ident(eval) stringcontent(;end)char(\\n)delimiter(")> - ident(eval) stringcontent(;end)char(\\n)delimiter(")> - reserved(end) - reserved(def) method(build_match)operator(()ident(sym)operator(,)ident(args)operator(\)) - ident(args)operator(.)ident(map)operator({)operator(|)ident(e)operator(|)stringcontent(/)delimiter(")>operator(})operator(.)ident(join)operator(()ident(sym)operator(\)) - reserved(end) -reserved(end) - -ident(mm) operator(=) constant(MultiMatch)operator(.)ident(new)operator(()stringoperator(,)stringoperator(\)) -pre_constant(ARGF)operator(.)ident(each) reserved(do) operator(|)ident(line)operator(|) - ident(print) ident(line) reserved(if) ident(mm)operator(.)ident(match_all)operator(()ident(line)operator(\)) -reserved(end) -comment(#-----------------------------) - -comment(#!/usr/bin/ruby) -comment(# popgrep4 - grep for abbreviations of places that say "pop") -comment(# version4: pretty fast, but simple: compile all re's first:) -ident(popstates) operator(=) string -ident(popstates) operator(=) ident(popstates)operator(.)ident(map)operator({)operator(|)ident(re)operator(|) regexpchar(\\b)delimiter(/)>operator(}) -pre_constant(ARGF)operator(.)ident(each) reserved(do) operator(|)ident(line)operator(|) - ident(popstates)operator(.)ident(each) reserved(do) operator(|)ident(state_re)operator(|) - reserved(if) ident(line) operator(=)operator(~) ident(state_re) - ident(print) ident(line) - reserved(break) - reserved(end) - reserved(end) -reserved(end) - -comment(## speeds trials on the jargon file(412\): 26006 lines, 1.3MB) -comment(## popgrep1 => 7.040s) -comment(## popgrep2 => 0.656s) -comment(## popgrep2.5 => 0.633s) -comment(## popgrep3 => 0.675s) -comment(## popgrep4 => 1.027s) - -comment(# unless speed is criticial, the technique in popgrep4 is a) -comment(# reasonable balance between speed and logical simplicity.) - - -comment(# @@PLEAC@@_6.11) -reserved(begin) - ident(print) string - ident(pat) operator(=) global_variable($stdin)operator(.)ident(gets)operator(.)ident(chomp) - constant(Regexp)operator(.)ident(new)operator(()ident(pat)operator(\)) -reserved(rescue) - ident(warn) string - reserved(retry) -reserved(end) - - -comment(# @@PLEAC@@_6.13) -comment(# uses the 'amatch' extension found on:) -comment(# http://raa.ruby-lang.org/project/amatch/) -ident(require) string -ident(matcher) operator(=) constant(Amatch)operator(.)ident(new)operator(()stringoperator(\)) -comment(#$relative, $distance = 0, 1) -constant(File)operator(.)ident(open)operator(()stringoperator(\))operator(.)ident(each_line) reserved(do) operator(|)ident(line)operator(|) - ident(print) ident(line) reserved(if) ident(matcher)operator(.)ident(search)operator(()ident(line)operator(\)) operator(<=) integer(1) -reserved(end) -comment(__END__ -)comment(#CODE) -ident(ballast) -ident(ballasts) -ident(balustrade) -ident(balustrades) -ident(blast) -ident(blasted) -ident(blaster) -ident(blasters) -ident(blasting) -ident(blasts) - - -comment(# @@PLEAC@@_6.14) -ident(str)operator(.)ident(scan)operator(()regexpoperator(\))operator(.)ident(each) reserved(do) operator(|)ident(token)operator(|) - ident(puts) stringdelimiter(")> -reserved(end) -comment(#-----------------------------) -ident(n) operator(=) string -ident(n)operator(.)ident(gsub!)operator(()regexpoperator(,)stringoperator(\)) -ident(puts) ident(n) -comment(#-----------------------------) -ident(str) operator(=) string -ident(str)operator(.)ident(scan)operator(()regexpoperator(\))operator(.)ident(each) reserved(do) operator(|)ident(num)operator(|) - ident(puts) stringdelimiter(")> -reserved(end) -comment(#-----------------------------) -comment(# Ruby doesn't have the String.pos or a /c re modifier like Perl) -comment(# But it does have StringScanner in the standard library (strscn\)) -comment(# which allows similar functionality:) - -ident(require) string -ident(text) operator(=) string -ident(sc) operator(=) constant(StringScanner)operator(.)ident(new)operator(()ident(text)operator(\)) -reserved(while) ident(sc)operator(.)ident(scan)operator(()regexpoperator(\)) - ident(print) stringchar(\\n)delimiter(")> -reserved(end) -reserved(if) ident(sc)operator(.)ident(scan)operator(()regexpoperator(\)) - ident(puts) stringcontent( after last number)delimiter(")> -reserved(end) -comment(#-----------------------------) -comment(# assuming continuing from above:) -ident(puts) stringdelimiter(")> -ident(sc)operator(.)ident(pos) operator(=) integer(30) -ident(puts) stringdelimiter(")> - - -comment(# @@PLEAC@@_6.15) -comment(#-----------------------------) -comment(# greedy pattern) -ident(str)operator(.)ident(gsub!)operator(()regexp)delimiter(/)modifier(m)>operator(,)stringoperator(\)) comment(# not good) - -comment(# non-greedy (minimal\) pattern) -ident(str)operator(.)ident(gsub!)operator(()regexp)delimiter(/)modifier(m)>operator(,)stringoperator(\)) comment(# not great) - - -comment(#-----------------------------) -comment(#this and that are important Oh, me too!) -comment(#-----------------------------) -regexp(.*?\) )delimiter(})modifier(mx)> -comment(#-----------------------------) -regexp -comment(#-----------------------------) -regexp( (?: (?!|\). \)* \) )delimiter(})modifier(mx)> -comment(#-----------------------------) -regexp( (?: (?!\). \)* \) )delimiter(})modifier(mx)> -comment(#-----------------------------) -regexp - [^<]* # stuff not possibly bad, and not possibly the end. - (?: - # at this point, we can have '<' if not part of something bad - (?! \) # what we can't have - < # okay, so match the '<' - [^<]* # and continue with more safe stuff - \) * - - )delimiter(})modifier(mx)> - - -comment(# @@PLEAC@@_6.16) -comment(#-----------------------------) -global_variable($/) operator(=) string -pre_constant(ARGF)operator(.)ident(each) reserved(do) operator(|)ident(para)operator(|) - ident(para)operator(.)ident(scan) regexp reserved(do) - ident(puts) stringcontent(' at paragraph )inlinedelimiter(")> - reserved(end) -reserved(end) -comment(#-----------------------------) -ident(astr) operator(=) string -ident(bstr) operator(=) string -reserved(if) stringcontent( )inlinedelimiter(")> operator(=)operator(~) regexp - ident(print) stringcontent( overlaps in )inlinecontent(-)inlinecontent(-)inlinedelimiter(")> -reserved(end) -comment(#-----------------------------) -comment(#!/usr/bin/ruby -w) -comment(# prime_pattern -- find prime factors of argument using patterns) -pre_constant(ARGV) operator(<<) integer(180) -ident(cap) operator(=) string operator(*) pre_constant(ARGV)operator(.)ident(shift) -reserved(while) ident(cap) operator(=)operator(~) regexp - ident(print) global_variable($1)operator(.)ident(size)operator(,) string - ident(cap)operator(.)ident(gsub!)operator(()regexpdelimiter(/)>operator(,)stringoperator(\)) -reserved(end) -ident(puts) ident(cap)operator(.)ident(size) -comment(#-----------------------------) -comment(#diophantine) -comment(# solve for 12x + 15y + 16z = 281, maximizing x) -reserved(if) operator(()string operator(*) integer(281)operator(\))operator(.)ident(match)operator(()regexpoperator(\)) - ident(x)operator(,) ident(y)operator(,) ident(z) operator(=) global_variable($1)operator(.)ident(size)operator(,) global_variable($2)operator(.)ident(size)operator(,) global_variable($3)operator(.)ident(size) - ident(puts) stringcontent(; y=)inlinecontent(; z=)inlinedelimiter(")> -reserved(else) - ident(puts) string -reserved(end) -comment(# => One solution is: x=17; y=3; z=2) - -comment(#-----------------------------) -comment(# using different quantifiers:) -operator(()string operator(*) integer(281)operator(\))operator(.)ident(match)operator(()regexpoperator(\)) -comment(# => One solution is: x=17; y=3; z=2) - -operator(()string operator(*) integer(281)operator(\))operator(.)ident(match)operator(()regexpoperator(\)) -comment(# => One solution is: x=0; y=7; z=11) - -operator(()string operator(*) integer(281)operator(\))operator(.)ident(match)operator(()regexpoperator(\)) -comment(# => One solution is: x=1; y=3; z=14) - - -comment(# @@PLEAC@@_6.17) -comment(# alpha OR beta) -regexp - -comment(# alpha AND beta) -regexp - -comment(# alpha AND beta, no overlap) -regexp - -comment(# NOT beta) -regexp - -comment(# NOT bad BUT good) -regexp -comment(#-----------------------------) - -reserved(if) operator(!)operator(()ident(string) operator(=)operator(~) regexpoperator(\)) comment(# ugly) - ident(something)operator(()operator(\)) -reserved(end) - -reserved(if) ident(string) operator(!)operator(~) regexp comment(# preferred) - ident(something)operator(()operator(\)) -reserved(end) - - -comment(#-----------------------------) -reserved(if) ident(string) operator(=)operator(~) regexp operator(&&) ident(string) operator(=)operator(~) regexp - ident(something)operator(()operator(\)) -reserved(end) -comment(#-----------------------------) -reserved(if) ident(string) operator(=)operator(~) regexp operator(||) ident(string) operator(=)operator(~) regexp - ident(something)operator(()operator(\)) -reserved(end) -comment(#-----------------------------) -comment(#!/usr/bin/ruby -w) -comment(# minigrep - trivial grep) -ident(pat) operator(=) pre_constant(ARGV)operator(.)ident(shift) -pre_constant(ARGF)operator(.)ident(each) reserved(do) operator(|)ident(line)operator(|) - ident(print) ident(line) reserved(if) ident(line) operator(=)operator(~) regexpdelimiter(/)modifier(o)> -reserved(end) -comment(#-----------------------------) - string operator(=)operator(~) regexp -comment(#-----------------------------) -global_variable($string) operator(=)operator(~) regexp operator(&&) global_variable($string) operator(=)operator(~) regexp -comment(#-----------------------------) -global_variable($murray_hill) operator(=) string -reserved(if) global_variable($murray_hill) operator(=)operator(~) regexp comment(# /m means . can match newline) - - ident(print) stringoperator(;) -reserved(end) -comment(#-----------------------------) -string operator(=)operator(~) regexp -comment(#-----------------------------) -global_variable($brand) operator(=) stringoperator(;) -reserved(if) global_variable($brand) operator(=)operator(~) regexp comment(# /m means . can match newline) - ident(print) stringoperator(;) -reserved(end) -comment(#-----------------------------) -global_variable($map) operator(=)operator(~) regexp -comment(#-----------------------------) -global_variable($map) operator(=) string -reserved(if) global_variable($map) operator(=)operator(~) regexp comment(# /m means . can match newline) - ident(print) stringoperator(;) -reserved(end) -comment(=begin - 7:15am up 206 days, 13:30, 4 users, load average: 1.04, 1.07, 1.04 - -USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT - -tchrist tty1 5:16pm 36days 24:43 0.03s xinit - -tchrist tty2 5:19pm 6days 0.43s 0.43s -tcsh - -tchrist ttyp0 chthon 7:58am 3days 23.44s 0.44s -tcsh - -gnat ttyS4 coprolith 2:01pm 13:36m 0.30s 0.30s -tcsh -=end) -comment(#% w | minigrep '^(?!.*ttyp\).*tchrist') -comment(#-----------------------------) -regexp -comment(#-----------------------------) -comment(#% w | grep tchrist | grep -v ttyp) -comment(#-----------------------------) -comment(#% grep -i 'pattern' files) -comment(#% minigrep '(?i\)pattern' files) -comment(#-----------------------------) - - -comment(# @@PLEAC@@_6.20) -ident(ans) operator(=) global_variable($stdin)operator(.)ident(gets)operator(.)ident(chomp) -ident(re) operator(=) regexpdelimiter(/)> -reserved(case) - reserved(when) string operator(=)operator(~) ident(re) operator(:) ident(puts) string - reserved(when) string operator(=)operator(~) ident(re) operator(:) ident(puts) string - reserved(when) string operator(=)operator(~) ident(re) operator(:) ident(puts) string - reserved(when) string operator(=)operator(~) ident(re) operator(:) ident(puts) string -reserved(end) -comment(#-----------------------------) -ident(require) string -ident(table) operator(=) constant(Abbrev)operator(.)ident(abbrev) string -ident(loop) reserved(do) - ident(print) string - ident(ans) operator(=) global_variable($stdin)operator(.)ident(gets)operator(.)ident(chomp) - ident(puts) stringcontent( is )inlinedelimiter(")> -reserved(end) - - -comment(#-----------------------------) -comment(# dummy values are defined for 'file', 'PAGER', and) -comment(# the 'invoke_editor' and 'deliver_message' methods) -comment(# do not do anything interesting in this example.) -comment(#!/usr/bin/ruby -w) -ident(require) string - -ident(file) operator(=) string -constant(PAGER) operator(=) string - -reserved(def) method(invoke_editor) - ident(puts) string -reserved(end) - -reserved(def) method(deliver_message) - ident(puts) string -reserved(end) - -ident(actions) operator(=) operator({) - string operator(=)operator(>) pre_constant(self)operator(.)ident(method)operator(()symbol(:invoke_editor)operator(\))operator(,) - string operator(=)operator(>) pre_constant(self)operator(.)ident(method)operator(()symbol(:deliver_message)operator(\))operator(,) - string operator(=)operator(>) ident(proc) operator({)ident(system)operator(()constant(PAGER)operator(,) ident(file)operator(\))operator(})operator(,) - string operator(=)operator(>) ident(proc) operator({)ident(puts) stringoperator(;) ident(exit)operator(})operator(,) - string operator(=)operator(>) ident(proc) operator({)ident(puts) stringoperator(}) -operator(}) - -ident(dtable) operator(=) constant(Abbrev)operator(.)ident(abbrev)operator(()ident(actions)operator(.)ident(keys)operator(\)) -ident(loop) reserved(do) - ident(print) string - ident(ans) operator(=) global_variable($stdin)operator(.)ident(gets)operator(.)ident(chomp)operator(.)ident(delete)operator(()stringoperator(\)) - ident(actions)operator([) ident(dtable)operator([)ident(ans)operator(.)ident(downcase)operator(]) operator(||) string operator(])operator(.)ident(call) -reserved(end) - - -comment(# @@PLEAC@@_6.19) -comment(#-----------------------------) -comment(# basically, the Perl Cookbook categorizes this as an) -comment(# unsolvable problem ...) -comment(#-----------------------------) -integer(1) reserved(while) ident(addr)operator(.)ident(gsub!)operator(()regexpoperator(,)stringoperator(\)) -comment(#-----------------------------) -constant(Dear) ident(someuser)instance_variable(@host)operator(.)ident(com)operator(,) - -constant(Please) ident(confirm) ident(the) ident(mail) ident(address) ident(you) ident(gave) ident(us) constant(Wed) constant(May) integer(6) integer(09)operator(:)integer(38)operator(:)integer(41) -constant(MDT) integer(1998) ident(by) ident(replying) ident(to) ident(this) ident(message)operator(.) constant(Include) ident(the) ident(string) -string reserved(in) ident(that) ident(reply)operator(,) ident(but) ident(spelled) reserved(in) ident(reverse)operator(;) ident(that) ident(is)operator(,) -ident(start) ident(with) stringoperator(.) constant(Once) ident(this) ident(is) ident(done)operator(,) ident(your) ident(confirmed) ident(address) ident(will) -ident(be) ident(entered) ident(into) ident(our) ident(records)operator(.) - - -comment(# @@PLEAC@@_6.21) -comment(#-----------------------------) -comment(#% gunzip -c ~/mail/archive.gz | urlify > archive.urlified) -comment(#-----------------------------) -comment(#% urlify ~/mail/*.inbox > ~/allmail.urlified) -comment(#-----------------------------) -comment(#!/usr/bin/ruby -w) -comment(# urlify - wrap HTML links around URL-like constructs) - -ident(urls) operator(=) stringoperator(;) -ident(ltrs) operator(=) stringoperator(;) -ident(gunk) operator(=) stringoperator(;) -ident(punc) operator(=) stringoperator(;) -ident(any) operator(=) stringinlineinlinedelimiter(")>operator(;) - -pre_constant(ARGF)operator(.)ident(each) reserved(do) operator(|)ident(line)operator(|) - ident(line)operator(.)ident(gsub!) regexpcontent( : # need resource and a colon - [)inlinecontent(] +? # followed by on or more - # of any valid character, but - # be conservative and take only - # what you need to.... - \) # end $1 } - (?= # look-ahead non-consumptive assertion - [)inlinecontent(]* # either 0 or more punctuation - [^)inlinecontent(] # followed by a non-url char - | # or else - $ # then end of the string - \) - )delimiter(/)modifier(iox)> reserved(do) - string)inlinecontent()delimiter(|)> - reserved(end) - ident(print) ident(line) -reserved(end) - - -comment(# @@PLEAC@@_6.23) -regexp -comment(#-----------------------------) -ident(str)operator(.)ident(sub!)operator(()regexpoperator(,) stringoperator(\)) -comment(#-----------------------------) -regexp comment(# keyword is $1, value is $2) -comment(#-----------------------------) -regexp -comment(#-----------------------------) -regexp -comment(#-----------------------------) -ident(str)operator(.)ident(gsub!)operator(()regexpoperator(,)stringoperator(\)) -comment(#-----------------------------) -ident(str)operator(.)ident(gsub!)operator(()regexpoperator(\))operator({) global_variable($1)operator(.)ident(hex)operator(.)ident(chr) operator(}) -comment(#-----------------------------) -ident(str)operator(.)ident(gsub!)operator(()regexpoperator(,)stringoperator(\)) -comment(#-----------------------------) -ident(str)operator(.)ident(sub!)operator(()regexpoperator(,) stringoperator(\)) -ident(str)operator(.)ident(sub!)operator(()regexpoperator(,) stringoperator(\)) - -comment(# but really, in Ruby we'd just do:) -ident(str)operator(.)ident(strip!) -comment(#-----------------------------) -ident(str)operator(.)ident(gsub!)operator(()regexpoperator(,)stringoperator(\)) -comment(#-----------------------------) -ident(str)operator(.)ident(sub!)operator(()regexpoperator(,) stringoperator(\)) -comment(#-----------------------------) -regexp -comment(#-----------------------------) -ident(str)operator(.)ident(sub!)operator(()regexpoperator(,) stringoperator(\)) -comment(#-----------------------------) -ident(cols) operator(=) operator(() operator(()constant(ENV)operator([)stringoperator(]) operator(||) stringoperator(\)) operator(=)operator(~) regexp operator(\)) operator(?) global_variable($1) operator(:) integer(80)operator(;) -comment(#-----------------------------) -ident(name) operator(=) stringcontent( )inlinedelimiter(")>operator(.)ident(gsub)operator(()regexpoperator(,) stringoperator(\)) -comment(#-----------------------------) -ident(require) string -ident(include) constant(Config) -ident(raise) string reserved(unless) constant(CONFIG)operator([)stringoperator(]) operator(=)operator(~) regexpoperator(;) -comment(#-----------------------------) -ident(str)operator(.)ident(gsub!)operator(()regexpoperator(,) stringoperator(\)) -comment(#-----------------------------) -ident(nums) operator(=) ident(str)operator(.)ident(scan)operator(()regexpoperator(\)) -comment(#-----------------------------) -ident(capwords) operator(=) ident(str)operator(.)ident(scan)operator(()regexpoperator(\)) -comment(#-----------------------------) -ident(lowords) operator(=) ident(str)operator(.)ident(scan)operator(()regexpoperator(\)) -comment(#-----------------------------) -ident(icwords) operator(=) ident(str)operator(.)ident(scan)operator(()regexpoperator(\)) -comment(#-----------------------------) -ident(links) operator(=) ident(str)operator(.)ident(scan)operator(()regexp]+?HREF)char(\\s)content(*=)char(\\s)content(*["']?([^'" >]+?\)[ '"]?>)delimiter(/)modifier(mi)>operator(\)) -comment(#-----------------------------) -ident(initial) operator(=) ident(str) operator(=)operator(~) regexp operator(?) global_variable($1) operator(:) string -comment(#-----------------------------) -ident(str)operator(.)ident(gsub!)operator(()regexpoperator(,) stringoperator(\)) -comment(#-----------------------------) - -global_variable($/) operator(=) string -ident(sentences) operator(=) operator([)operator(]) -pre_constant(ARGF)operator(.)ident(each) reserved(do) operator(|)ident(para)operator(|) - ident(para)operator(.)ident(gsub!)operator(()regexpoperator(,) stringoperator(\)) - ident(para)operator(.)ident(gsub!)operator(()regexpoperator(,)stringoperator(\)) - ident(sentences) operator(<<) ident(para)operator(.)ident(scan)operator(()regexpoperator(\)) -reserved(end) - -comment(#-----------------------------) -regexp comment(# YYYY in $1, MM in $2, DD in $3) -comment(#-----------------------------) -regexp -comment(#-----------------------------) -regexp -comment(#-----------------------------) -ident(lines) operator(=) operator([)operator(]) -ident(lines) operator(<<) global_variable($1) reserved(while) ident(input)operator(.)ident(sub!)operator(()regexpoperator(,)stringoperator(\)) - - -comment(# @@PLEAC@@_7.0) -comment(# An IO object being Enumerable, we can use 'each' directly on it) -constant(File)operator(.)ident(open)operator(()stringoperator(\))operator(.)ident(each) operator({) operator(|)ident(line)operator(|) - ident(puts) ident(line) reserved(if) ident(line) operator(=)operator(~) regexp -operator(}) - -ident(logfile) operator(=) constant(File)operator(.)ident(new)operator(()stringoperator(,) stringoperator(\)) -ident(mysub)operator(()global_variable($stdin)operator(,) ident(logfile)operator(\)) - -comment(# The method IO#readline is similar to IO#gets) -comment(# but throws an exception when it reaches EOF) -ident(f) operator(=) constant(File)operator(.)ident(new)operator(()stringoperator(\)) -reserved(begin) - reserved(while) operator(()ident(line) operator(=) ident(f)operator(.)ident(readline)operator(\)) - ident(line)operator(.)ident(chomp) - global_variable($stdout)operator(.)ident(print) ident(line) reserved(if) ident(line) operator(=)operator(~) regexp - reserved(end) -reserved(rescue) constant(EOFError) - ident(f)operator(.)ident(close) -reserved(end) - -reserved(while) global_variable($stdin)operator(.)ident(gets) comment(# reads from STDIN) - reserved(unless) operator(()regexpoperator(\)) - global_variable($stderr)operator(.)ident(puts) string comment(# writes to STDERR) - reserved(end) - ident(puts) stringdelimiter(")> comment(# writes to STDOUT) -reserved(end) - -ident(logfile) operator(=) constant(File)operator(.)ident(new)operator(()stringoperator(,) stringoperator(\)) - -ident(logfile)operator(.)ident(close) - -comment(# $defout (or its synonym '$>'\) is the destination of output) -comment(# for Kernel#print, Kernel#puts, and family functions) -ident(logfile) operator(=) constant(File)operator(.)ident(new)operator(()stringoperator(,) stringoperator(\)) -ident(old) operator(=) global_variable($defout) -global_variable($defout) operator(=) ident(logfile) comment(# switch to logfile for output) -ident(puts) string -global_variable($defout) operator(=) ident(old) comment(# return to original output) -ident(puts) string - - -comment(# @@PLEAC@@_7.1) -ident(source) operator(=) constant(File)operator(.)ident(new)operator(()ident(path)operator(,) stringoperator(\)) comment(# open file "path" for reading only) -ident(sink) operator(=) constant(File)operator(.)ident(new)operator(()ident(path)operator(,) stringoperator(\)) comment(# open file "path" for writing only) - -ident(source) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) constant(File)operator(::)constant(RDONLY)operator(\)) comment(# open file "path" for reading only) -ident(sink) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) constant(File)operator(::)constant(WRONLY)operator(\)) comment(# open file "path" for writing only) - -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) stringoperator(\)) comment(# open "path" for reading and writing) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) ident(flags)operator(\)) comment(# open "path" with the flags "flags" (see examples below for flags\)) - -comment(# open file "path" read only) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) stringoperator(\)) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) constant(File)operator(::)constant(RDONLY)operator(\)) - -comment(# open file "path" write only, create it if it does not exist) -comment(# truncate it to zero length if it exists) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) stringoperator(\)) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) constant(File)operator(::)constant(WRONLY)operator(|)constant(File)operator(::)constant(TRUNC)operator(|)constant(File)operator(::)constant(CREAT)operator(\)) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) constant(File)operator(::)constant(WRONLY)operator(|)constant(File)operator(::)constant(TRUNC)operator(|)constant(File)operator(::)constant(CREAT)operator(,) integer(0666)operator(\)) comment(# with permission 0666) - -comment(# open file "path" write only, fails if file exists) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) constant(File)operator(::)constant(WRONLY)operator(|)constant(File)operator(::)constant(EXCL)operator(|)constant(File)operator(::)constant(CREAT)operator(\)) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) constant(File)operator(::)constant(WRONLY)operator(|)constant(File)operator(::)constant(EXCL)operator(|)constant(File)operator(::)constant(CREAT)operator(,) integer(0666)operator(\)) - -comment(# open file "path" for appending) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) stringoperator(\)) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) constant(File)operator(::)constant(WRONLY)operator(|)constant(File)operator(::)constant(APPEND)operator(|)constant(File)operator(::)constant(CREAT)operator(\)) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) constant(File)operator(::)constant(WRONLY)operator(|)constant(File)operator(::)constant(APPEND)operator(|)constant(File)operator(::)constant(CREAT)operator(,) integer(0666)operator(\)) - -comment(# open file "path" for appending only when file exists) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) constant(File)operator(::)constant(WRONLY)operator(|)constant(File)operator(::)constant(APPEND)operator(\)) - -comment(# open file "path" for reading and writing) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) stringoperator(\)) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) constant(File)operator(::)constant(RDWR)operator(\)) - -comment(# open file for reading and writing, create a new file if it does not exist) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) constant(File)operator(::)constant(RDWR)operator(|)constant(File)operator(::)constant(CREAT)operator(\)) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) constant(File)operator(::)constant(RDWR)operator(|)constant(File)operator(::)constant(CREAT)operator(,) integer(0600)operator(\)) - -comment(# open file "path" reading and writing, fails if file exists) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) constant(File)operator(::)constant(RDWR)operator(|)constant(File)operator(::)constant(EXCL)operator(|)constant(File)operator(::)constant(CREAT)operator(\)) -ident(file) operator(=) constant(File)operator(.)ident(open)operator(()ident(path)operator(,) constant(File)operator(::)constant(RDWR)operator(|)constant(File)operator(::)constant(EXCL)operator(|)constant(File)operator(::)constant(CREAT)operator(,) integer(0600)operator(\)) - - -comment(# @@PLEAC@@_7.2) -comment(# No problem with Ruby since the filename doesn't contain characters with) -comment(# special meaning; like Perl's sysopen) -constant(File)operator(.)ident(open)operator(()ident(filename)operator(,) stringoperator(\)) - - -comment(# @@PLEAC@@_7.3) -constant(File)operator(.)ident(expand_path)operator(()stringoperator(\)) -comment(#=> "/root/tmp") -constant(File)operator(.)ident(expand_path)operator(()stringoperator(\)) -comment(#=> "/var/lib/nfs") - -comment(# To expand ~/.. it explicitely needs the environment variable HOME) -constant(File)operator(.)ident(expand_path)operator(()stringoperator(\)) -comment(#=> "/home/gc/tmp") - - -comment(# @@PLEAC@@_7.4) -comment(# The exception raised in Ruby reports the filename) -constant(File)operator(.)ident(open)operator(()stringoperator(\)) - - -comment(# @@PLEAC@@_7.5) -comment(# Standard Ruby distribution provides the following useful extension) -ident(require) string -comment(# With the Tempfile class, the file is automatically deleted on garbage) -comment(# collection, so you won't need to remove it, later on.) -ident(tf) operator(=) constant(Tempfile)operator(.)ident(new)operator(()stringoperator(\)) comment(# a name is required to create the filename) - -comment(# If you need to pass the filename to an external program you can use) -comment(# File#path, but don't forget to File#flush in order to flush anything) -comment(# living in some buffer somewhere.) -ident(tf)operator(.)ident(flush) -ident(system)operator(()stringdelimiter(")>operator(\)) - -ident(fh) operator(=) constant(Tempfile)operator(.)ident(new)operator(()stringoperator(\)) -ident(fh)operator(.)ident(sync) operator(=) pre_constant(true) comment(# autoflushes) -integer(10)operator(.)ident(times) operator({) operator(|)ident(i)operator(|) ident(fh)operator(.)ident(puts) ident(i) operator(}) -ident(fh)operator(.)ident(rewind) -ident(puts) stringoperator(,) ident(fh)operator(.)ident(readlines) - - -comment(# @@PLEAC@@_7.6) -reserved(while) operator(()pre_constant(DATA)operator(.)ident(gets)operator(\)) reserved(do) - comment(# process the line) -reserved(end) -comment(__END__ -# your data goes here -# __DATA__ doesn't exist in Ruby - -)comment(#CODE) -comment(# get info about the script (size, date of last modification\)) -ident(kilosize) operator(=) pre_constant(DATA)operator(.)ident(stat)operator(.)ident(size) operator(/) integer(1024) -ident(last_modif) operator(=) pre_constant(DATA)operator(.)ident(stat)operator(.)ident(mtime) -ident(puts) stringScript size is )inlinedelimiter(")> -ident(puts) stringLast script update: )inlinedelimiter(")> -comment(__END__ -# DO NOT REMOVE THE PRECEEDING LINE. -# Everything else in this file will be ignored. -)comment(#CODE) - - -comment(# @@PLEAC@@_7.7) -reserved(while) ident(line) operator(=) ident(gets) reserved(do) - comment(# do something with line.) -reserved(end) - -comment(# or) -reserved(while) ident(gets) reserved(do) - comment(# do something with $_) -reserved(end) - -comment(# or more rubyish) -global_variable($stdun)operator(.)ident(each) reserved(do) operator(|)ident(line)operator(|) - comment(# do stuff with line) -reserved(end) - - -comment(# ARGF may makes this more easy) -comment(# this is skipped if ARGV.size==0) -pre_constant(ARGV)operator(.)ident(each) reserved(do) operator(|)ident(filename)operator(|) - comment(# closing and exception handling are done by the block) - ident(open)operator(()ident(filename)operator(\)) reserved(do) operator(|)ident(fd)operator(|) - ident(fd)operator(.)ident(each) reserved(do) operator(|)ident(line)operator(|) - comment(# do stuff with line) - reserved(end) - reserved(end) reserved(rescue) ident(abort)operator(()string operator(%) ident(filename)operator(\)) -reserved(end) - -comment(# globbing is done in the Dir module) -pre_constant(ARGV) operator(=) constant(Dir)operator([)stringoperator(]) reserved(if) pre_constant(ARGV)operator(.)ident(empty?) - -comment(# note: optparse is the preferred way to handle this) -reserved(if) operator(()pre_constant(ARGV)operator([)integer(0)operator(]) operator(==) stringoperator(\)) - ident(chop_first) operator(+=) integer(1) - pre_constant(ARGV)operator(.)ident(shift) -reserved(end) - - -comment(# processing numerical options) -reserved(if) pre_constant(ARGV)operator([)integer(0)operator(]) operator(=)operator(~) regexp - ident(columns) operator(=) global_variable($1) - pre_constant(ARGV)operator(.)ident(shift) -reserved(end) - -comment(# again, better to use optparse:) -ident(require) string -ident(nostdout) operator(=) integer(0) -ident(append) operator(=) integer(0) -ident(unbuffer) operator(=) integer(0) -ident(ignore_ints) operator(=) integer(0) -pre_constant(ARGV)operator(.)ident(options) reserved(do) operator(|)ident(opt)operator(|) - ident(opt)operator(.)ident(on)operator(()stringoperator(\)) operator({) ident(nostdout) operator(+=)integer(1) operator(}) - ident(opt)operator(.)ident(on)operator(()stringoperator(\)) operator({) ident(append) operator(+=)integer(1) operator(}) - ident(opt)operator(.)ident(on)operator(()stringoperator(\)) operator({) ident(unbuffer) operator(+=)integer(1) operator(}) - ident(opt)operator(.)ident(on)operator(()stringoperator(\)) operator({) ident(ignore_ints) operator(+=)integer(1) operator(}) - ident(opt)operator(.)ident(parse!) -reserved(end) reserved(or) ident(abort)operator(()string operator(+) pre_constant(__FILE__) operator(+) stringoperator(\)) - -comment(# no need to do undef $/, we have File.read) -ident(str) operator(=) constant(File)operator(.)ident(read)operator(()pre_constant(ARGV)operator([)integer(0)operator(])operator(\)) - -comment(# again we have File.read) -ident(str) operator(=) constant(File)operator(.)ident(read)operator(()pre_constant(ARGV)operator([)integer(0)operator(])operator(\)) - -comment(# not sure what this should do:) -comment(# I believe open the file, print filename, lineno and line:) -pre_constant(ARGF)operator(.)ident(each_with_index) reserved(do) operator(|)ident(line)operator(,) ident(idx)operator(|) - ident(print) pre_constant(ARGF)operator(.)ident(filename)operator(,) stringoperator(,) ident(idx)operator(,) stringoperator(,) ident(line) -reserved(end) - -comment(# print all the lines in every file passed via command line that contains login) -pre_constant(ARGF)operator(.)ident(each) reserved(do) operator(|)ident(line)operator(|) - ident(puts) ident(line) reserved(if) ident(line) operator(=)operator(~) regexp -reserved(end) -comment(#) -comment(# even this would fit) -comment(#%ruby -ne "print if /f/" 2.log) -comment(#) - -pre_constant(ARGF)operator(.)ident(each) operator({) operator(|)ident(l)operator(|) ident(puts) ident(l)operator(.)ident(downcase!) operator(}) - -comment(#------------------) -comment(#!/usr/bin/ruby -p) -comment(# just like perl's -p) -global_variable($_)operator(.)ident(downcase!) -comment(#) - -comment(# I don't know who should I trust.) -comment(# perl's version splits on \\w+ while python's on \\w.) - -ident(chunks) operator(=) integer(0) - -constant(File)operator(.)ident(read)operator(()pre_constant(ARGV)operator([)integer(0)operator(])operator(\))operator(.)ident(split)operator(.)ident(each) reserved(do) operator(|)ident(word)operator(|) - reserved(next) reserved(if) ident(word) operator(=)operator(~) regexp - reserved(break) reserved(if) operator([)stringoperator(,) stringoperator(])operator(.)ident(member?) ident(word) - ident(chunks) operator(+=) integer(1) -reserved(end) - -ident(print) stringoperator(,) ident(chunks)operator(,) string - - -comment(# @@PLEAC@@_7.8) -ident(old) operator(=) constant(File)operator(.)ident(open)operator(()ident(old_file)operator(\)) -ident(new) operator(=) constant(File)operator(.)ident(open)operator(()ident(new_file)operator(,) stringoperator(\)) -reserved(while) ident(old)operator(.)ident(gets) reserved(do) - comment(# change $_, then...) - ident(new)operator(.)ident(print) global_variable($_) -reserved(end) -ident(old)operator(.)ident(close) -ident(new)operator(.)ident(close) -constant(File)operator(.)ident(rename)operator(()ident(old_file)operator(,) stringoperator(\)) -constant(File)operator(.)ident(rename)operator(()ident(new_file)operator(,) ident(old_file)operator(\)) - -reserved(while) ident(old)operator(.)ident(gets) reserved(do) - reserved(if) global_variable($.) operator(==) integer(20) reserved(then) comment(# we are at the 20th line) - ident(new)operator(.)ident(puts) string - ident(new)operator(.)ident(puts) string - reserved(end) - ident(new)operator(.)ident(print) global_variable($_) -reserved(end) - -reserved(while) ident(old)operator(.)ident(gets) reserved(do) - reserved(next) reserved(if) integer(20)operator(..)integer(30) comment(# skip the 20th line to the 30th) - comment(# Ruby (and Perl\) permit to write if 20..30) - comment(# instead of if (20 <= $.\) and ($. <= 30\)) - ident(new)operator(.)ident(print) global_variable($_) -reserved(end) - - -comment(# @@PLEAC@@_7.9) -comment(#% ruby -i.orig -pe 'FILTER COMMAND' file1 file2 file3 ...) -comment(#) -comment(#-----------------------------) -comment(##!/usr/bin/ruby -i.orig -p) -comment(# filter commands go here) -comment(#-----------------------------) - -comment(#% ruby -pi.orig -e 'gsub!(/DATE/\){Time.now\)') - -comment(# effectively becomes:) -pre_constant(ARGV) operator(<<) string -ident(oldfile) operator(=) string -reserved(while) ident(gets) - reserved(if) pre_constant(ARGF)operator(.)ident(filename) operator(!=) ident(oldfile) - ident(newfile) operator(=) pre_constant(ARGF)operator(.)ident(filename) - constant(File)operator(.)ident(rename)operator(()ident(newfile)operator(,) ident(newfile) operator(+) stringoperator(\)) - global_variable($stdout) operator(=) constant(File)operator(.)ident(open)operator(()ident(newfile)operator(,)stringoperator(\)) - ident(oldfile) operator(=) ident(newfile) - reserved(end) - ident(gsub!)operator(()regexpoperator(\))operator({)constant(Time)operator(.)ident(now)operator(}) - ident(print) -reserved(end) -global_variable($stdout) operator(=) constant(STDOUT) -comment(#-----------------------------) -comment(#% ruby -i.old -pe 'gsub!(%r{\\bhisvar\\b}, 'hervar'\)' *.[Cchy]) - -comment(#-----------------------------) -comment(# set up to iterate over the *.c files in the current directory,) -comment(# editing in place and saving the old file with a .orig extension) -global_variable($-i) operator(=) string comment(# set up -i mode) -pre_constant(ARGV)operator(.)ident(replace)operator(()constant(Dir)operator([)stringoperator(])operator(\)) -reserved(while) ident(gets) - reserved(if) global_variable($.) operator(==) integer(1) - ident(print) string - reserved(end) - ident(gsub!)operator(()regexpoperator(,) stringoperator(\)) comment(# Correct typos, preserving case) - ident(print) - pre_constant(ARGF)operator(.)ident(close) reserved(if) pre_constant(ARGF)operator(.)ident(eof) -reserved(end) - - -comment(# @@PLEAC@@_7.10) -constant(File)operator(.)ident(open)operator(()stringoperator(,) stringoperator(\)) reserved(do) operator(|)ident(f)operator(|) comment(# open file for update) - ident(lines) operator(=) ident(f)operator(.)ident(readlines) comment(# read into array of lines) - ident(lines)operator(.)ident(each) reserved(do) operator(|)ident(it)operator(|) comment(# modify lines) - ident(it)operator(.)ident(gsub!)operator(()regexpoperator(,) stringoperator(\)) - reserved(end) - ident(f)operator(.)ident(pos) operator(=) integer(0) comment(# back to start) - ident(f)operator(.)ident(print) ident(lines) comment(# write out modified lines) - ident(f)operator(.)ident(truncate)operator(()ident(f)operator(.)ident(pos)operator(\)) comment(# truncate to new length) -reserved(end) comment(# file is automatically closed) -comment(#-----------------------------) -constant(File)operator(.)ident(open)operator(()stringoperator(,) stringoperator(\)) reserved(do) operator(|)ident(f)operator(|) - ident(out) operator(=) string - ident(f)operator(.)ident(each) reserved(do) operator(|)ident(line)operator(|) - ident(out) operator(<<) ident(line)operator(.)ident(gsub)operator(()regexpoperator(\)) operator({)constant(Time)operator(.)ident(now)operator(}) - reserved(end) - ident(f)operator(.)ident(pos) operator(=) integer(0) - ident(f)operator(.)ident(print) ident(out) - ident(f)operator(.)ident(truncate)operator(()ident(f)operator(.)ident(pos)operator(\)) -reserved(end) - -comment(# @@PLEAC@@_7.11) -constant(File)operator(.)ident(open)operator(()stringoperator(,) stringoperator(\)) reserved(do) operator(|)ident(f)operator(|) - ident(f)operator(.)ident(flock) constant(File)operator(::)constant(LOCK_EX) - comment(# update file) -reserved(end) -comment(#-----------------------------) -constant(File)operator(::)constant(LOCK_SH) comment(# shared lock (for reading\)) -constant(File)operator(::)constant(LOCK_EX) comment(# exclusive lock (for writing\)) -constant(File)operator(::)constant(LOCK_NB) comment(# non-blocking request) -constant(File)operator(::)constant(LOCK_UN) comment(# free lock) -comment(#-----------------------------) -reserved(unless) ident(f)operator(.)ident(flock) constant(File)operator(::)constant(LOCK_EX) operator(|) constant(File)operator(::)constant(LOCK_NB) - ident(warn) string - ident(f)operator(.)ident(flock) constant(File)operator(::)constant(LOCK_EX) -reserved(end) -comment(#-----------------------------) -constant(File)operator(.)ident(open)operator(()stringoperator(,) constant(File)operator(::)constant(RDWR)operator(|)constant(File)operator(::)constant(CREAT)operator(\)) reserved(do) operator(|)ident(f)operator(|) - ident(f)operator(.)ident(flock)operator(()constant(File)operator(::)constant(LOCK_EX)operator(\)) - ident(num) operator(=) ident(f)operator(.)ident(gets)operator(.)ident(to_i) operator(||) integer(0) - ident(f)operator(.)ident(pos) operator(=) integer(0) - ident(f)operator(.)ident(truncate) integer(0) - ident(f)operator(.)ident(puts) ident(num) operator(+) integer(1)ident(q) -reserved(end) - - -comment(# @@PLEAC@@_7.12) -ident(output_handle)operator(.)ident(sync) operator(=) pre_constant(true) -comment(# Please note that like in Perl, $stderr is already unbuffered) -comment(#-----------------------------) -comment(#!/usr/bin/ruby -w) -comment(# seeme - demo stdio output buffering) -global_variable($stdout)operator(.)ident(sync) operator(=) pre_constant(ARGV)operator(.)ident(size) operator(>) integer(0) -ident(print) string -ident(sleep) integer(2) -ident(puts) string -comment(#-----------------------------) -global_variable($stderr)operator(.)ident(sync) operator(=) pre_constant(true) -ident(afile)operator(.)ident(sync) operator(=) pre_constant(false) -comment(#-----------------------------) -comment(# assume 'remote_con' is an interactive socket handle,) -comment(# but 'disk_file' is a handle to a regular file.) -ident(remote_con)operator(.)ident(sync) operator(=) pre_constant(true) comment(# unbuffer for clarity) -ident(disk_file)operator(.)ident(sync) operator(=) pre_constant(false) comment(# buffered for speed) -comment(#-----------------------------) -ident(require) string -ident(sock) operator(=) constant(TCPSocket)operator(.)ident(new)operator(()stringoperator(,) integer(80)operator(\)) -ident(sock)operator(.)ident(sync) operator(=) pre_constant(true) -ident(sock)operator(.)ident(puts) string -ident(resp) operator(=) ident(sock)operator(.)ident(read) -ident(print) stringchar(\\n)delimiter(")> - - -comment(# @@PLEAC@@_7.13) -comment(#-----------------------------) -comment(# assumes fh1, fh2, fh2 are oen IO objects) -ident(nfound) operator(=) ident(select)operator(()operator([)global_variable($stdin)operator(,) ident(fh1)operator(,) ident(fh2)operator(,) ident(fh3)operator(])operator(,) pre_constant(nil)operator(,) pre_constant(nil)operator(,) integer(0)operator(\)) -ident(nfound)operator([)integer(0)operator(])operator(.)ident(each) reserved(do) operator(|)ident(file)operator(|) - reserved(case) ident(file) - reserved(when) ident(fh1) - comment(# do something with fh1) - reserved(when) ident(fh2) - comment(# do something with fh2) - reserved(when) ident(fh3) - comment(# do something with fh3) - reserved(end) -reserved(end) -comment(#-----------------------------) -ident(input_files) operator(=) operator([)operator(]) -comment(# repeat next line for all in-files to poll) -ident(input_files) operator(<<) ident(fh1) -reserved(if) ident(nfound) operator(=) ident(select)operator(()ident(input_files)operator(,) pre_constant(nil)operator(,) pre_constant(nil)operator(,) integer(0)operator(\)) - comment(# input ready on files in nfound[0]) -reserved(end) - - -comment(# @@PLEAC@@_8.0) -comment(#-----------------------------) -comment(# datafile is a file or IO object) -ident(datafile)operator(.)ident(readlines)operator(.)ident(each) operator({) operator(|)ident(line)operator(|) - ident(line)operator(.)ident(chomp!) - ident(size) operator(=) ident(line)operator(.)ident(length) - ident(puts) ident(size) -operator(}) -comment(#-----------------------------) -ident(datafile)operator(.)ident(readlines)operator(.)ident(each) operator({) operator(|)ident(line)operator(|) - ident(puts) ident(line)operator(.)ident(chomp!)operator(.)ident(length) -operator(}) -comment(#-----------------------------) -ident(lines) operator(=) ident(datafile)operator(.)ident(readlines) -comment(#-----------------------------) -ident(whole_file) operator(=) ident(file)operator(.)ident(read) -comment(#-----------------------------) -comment(# ruby -040 -e 'word = gets; puts "First word is #{word}"') -comment(#-----------------------------) -comment(# ruby -ne 'BEGIN { $/="%%\\n" }; $_.chomp; puts $_ if( $_=~/Unix/i\)' fortune.dat) -comment(#-----------------------------) -ident(handle)operator(.)ident(print) stringoperator(,) stringoperator(,) string comment(# "onetwothree") -ident(puts) string comment(# sent to $stdout) -comment(#-----------------------------) -ident(buffer) operator(=) ident(handle)operator(.)ident(read)operator(()integer(4096)operator(\)) -ident(rv) operator(=) ident(buffer)operator(.)ident(length) -comment(#-----------------------------) -ident(handle)operator(.)ident(truncate)operator(()ident(length)operator(\)) -ident(open)operator(()stringcontent(.pid)delimiter(")>operator(,) stringoperator(\)) operator({) operator(|)ident(handle)operator(|) ident(handle)operator(.)ident(truncate)operator(()ident(length)operator(\)) operator(}) -comment(#-----------------------------) -ident(pos) operator(=) ident(datafile)operator(.)ident(pos) comment(# tell is an alias of pos) -ident(puts) stringcontent( bytes from the start of datafile)delimiter(")> -comment(#-----------------------------) -ident(logfile)operator(.)ident(seek)operator(()integer(0)operator(,) constant(IO)operator(::)constant(SEEK_END)operator(\)) -ident(datafile)operator(.)ident(seek)operator(()ident(pos)operator(\)) comment(# IO::SEEK_SET is the default) -ident(out)operator(.)ident(seek)operator(()operator(-)integer(20)operator(,) constant(IO)operator(::)constant(SEEK_CUR)operator(\)) -comment(#-----------------------------) -ident(written) operator(=) ident(datafile)operator(.)ident(syswrite)operator(()ident(mystring)operator(\)) -ident(raise) constant(RunTimeError) reserved(unless) ident(written) operator(==) ident(mystring)operator(.)ident(length) -ident(block) operator(=) ident(infile)operator(.)ident(sysread)operator(()integer(256)operator(\)) comment(# no equivalent to perl offset parameter in sysread) -ident(puts) stringcontent( bytes)delimiter(")> reserved(if) integer(256) operator(!=) ident(block)operator(.)ident(length) -comment(#-----------------------------) -ident(pos) operator(=) ident(handle)operator(.)ident(sysseek)operator(()integer(0)operator(,) constant(IO)operator(::)constant(SEEK_CUR)operator(\)) comment(# don't change position) - - -comment(# @@PLEAC@@_8.1) -reserved(while) operator(()ident(line) operator(=) ident(fh)operator(.)ident(gets)operator(\)) - ident(line)operator(.)ident(chomp!) - ident(nextline) operator(=) pre_constant(nil) - ident(line)operator(.)ident(gsub!)operator(()regexpoperator(\)) operator({) operator(|)ident(match)operator(|) ident(nextline) operator(=) ident(fh)operator(.)ident(gets)operator(;) string operator(}) - reserved(if) operator(()ident(nextline) operator(!=) pre_constant(nil)operator(\)) - ident(line) operator(+=) ident(nextline) - reserved(redo) - reserved(end) - comment(# process full record in line here) -reserved(end) -comment(#-----------------------------) -comment(# DISTFILES = $(DIST_COMMON\) $(SOURCES\) $(HEADERS\) \\) -comment(# $(TEXINFOS\) $(INFOS\) $(MANS\) $(DATA\)) -comment(# DEP_DISTFILES = $(DIST_COMMON\) $(SOURCES\) $(HEADERS\) \\) -comment(# $(TEXINFOS\) $(INFO_DEPS\) $(MANS\) $(DATA\) \\) -comment(# $(EXTRA_DIST\)) -comment(#-----------------------------) -ident(line)operator(.)ident(gsub!)operator(()regexpoperator(,) stringoperator(\)) operator({) - comment(# as before) -operator(}) - - -comment(# @@PLEAC@@_8.2) -comment(#-----------------------------) -ident(count) operator(=) shelldelimiter(`)> -ident(fail) stringdelimiter(")> reserved(if) global_variable($?) operator(!=) integer(0) -ident(count)operator(.)ident(chomp!) -comment(#-----------------------------) -ident(count) operator(=) integer(0) -constant(File)operator(.)ident(open)operator(()ident(file)operator(,) stringoperator(\)) operator({) operator(|)ident(fh)operator(|) - ident(count) operator(+=) integer(1) reserved(while) ident(fh)operator(.)ident(gets) -operator(}) -comment(# count now holds the number of lines read) -comment(#-----------------------------) -ident(count) operator(=) integer(0) -reserved(while) operator(()ident(chunk) operator(=) ident(file)operator(.)ident(sysread)operator(()integer(2)operator(**)integer(16)operator(\))operator(\)) - ident(count) operator(+=) ident(chunk)operator(.)ident(count)operator(()stringoperator(\)) -reserved(end) reserved(rescue) constant(EOFError) -comment(#-----------------------------) -constant(File)operator(.)ident(open)operator(()ident(filename)operator(,)stringoperator(\)) operator({) operator(|)ident(fh)operator(|) - ident(count) operator(+=) integer(1) reserved(while) ident(fh)operator(.)ident(gets) -operator(}) -comment(# count now holds the number of lines read) -comment(#-----------------------------) -comment(# As ruby doesn't quite have an equivalent to using a for) -comment(# statement as in perl, I threw this in) -ident(count) operator(=) constant(File)operator(.)ident(readlines)operator(()ident(filename)operator(\))operator(.)ident(size) -comment(#-----------------------------) -integer(1) reserved(while) ident(file)operator(.)ident(gets) -ident(count) operator(=) global_variable($.) -comment(#-----------------------------) -global_variable($/) operator(=) string -ident(open)operator(()ident(filename)operator(,) stringoperator(\)) operator({) operator(|)ident(fh)operator(|) - integer(1) reserved(while) ident(fh)operator(.)ident(gets) - ident(para_count) operator(=) global_variable($.) -operator(}) reserved(rescue) ident(fail)operator(()stringcontent(: $!)delimiter(")>operator(\)) -comment(#-----------------------------) - - -comment(# ^^PLEAC^^_8.3) -comment(#-----------------------------) -reserved(while) operator(()ident(gets)operator(\)) - ident(split)operator(.)ident(each) operator({) operator(|)ident(chunk)operator(|) - comment(# do something with chunk) - operator(}) -reserved(end) -comment(#-----------------------------) -reserved(while) operator(()ident(gets)operator(\)) - ident(gsub)operator(()regexpoperator(\)) operator({) operator(|)ident(word)operator(|) - comment(# do something with word) - operator(}) -reserved(end) -comment(#-----------------------------) -comment(# Make a word frequency count) -comment(# normally hashes can be created using {} or just Hash.new) -comment(# but we want the default value of an entry to be 0 instead) -comment(# of nil. (nil can't be incremented\)) -ident(seen) operator(=) constant(Hash)operator(.)ident(new)operator(()integer(0)operator(\)) -reserved(while) operator(()ident(gets)operator(\)) - ident(gsub)operator(()regexpoperator(\)) operator({) operator(|)ident(word)operator(|) - ident(seen)operator([)ident(word)operator(.)ident(downcase)operator(]) operator(+=) integer(1) - operator(}) -reserved(end) -comment(# output hash in a descending numeric sort of its values) -ident(seen)operator(.)ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(b)operator([)integer(1)operator(]) operator(<=>) ident(a)operator([)integer(1)operator(]) operator(})operator(.)ident(each) reserved(do) operator(|)ident(k)operator(,)ident(v)operator(|) - ident(printf)operator(()stringoperator(,) ident(v)operator(,) ident(k) operator(\)) -reserved(end) - -comment(#-----------------------------) -comment(# Line frequency count) -ident(seen) operator(=) constant(Hash)operator(.)ident(new)operator(()integer(0)operator(\)) -reserved(while) operator(()ident(gets)operator(\)) - ident(seen)operator([)global_variable($_)operator(.)ident(downcase)operator(]) operator(+=) integer(1) -reserved(end) -ident(seen)operator(.)ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(b)operator([)integer(1)operator(]) operator(<=>) ident(a)operator([)integer(1)operator(]) operator(})operator(.)ident(each) reserved(do) operator(|)ident(k)operator(,)ident(v)operator(|) - ident(printf)operator(()stringoperator(,) ident(v)operator(,) ident(k) operator(\)) -reserved(end) -comment(#-----------------------------) - - -comment(# @@PLEAC@@_8.4) -comment(#-----------------------------) -comment(# instead of file handle FILE, we can just) -comment(# use a string containing the filename) -constant(File)operator(.)ident(readlines)operator(()ident(file)operator(\))operator(.)ident(each) operator({) operator(|)ident(line)operator(|) - comment(# do something with line) -operator(}) -comment(#-----------------------------) -constant(File)operator(.)ident(readlines)operator(()ident(file)operator(\))operator(.)ident(reverse_each) operator({) operator(|)ident(line)operator(|) - comment(# do something with line) -operator(}) -comment(#-----------------------------) -comment(# the variable lines might have been created) -comment(# this way) -comment(# lines = File.readlines(file\)) -comment(#) -comment(# normally one would use the reverse_each, but) -comment(# if you insist on using a numerical index to) -comment(# iterate over the lines array...) -operator(()ident(lines)operator(.)ident(size) operator(-) integer(1)operator(\))operator(.)ident(downto)operator(()integer(0)operator(\)) operator({) operator(|)ident(i)operator(|) - ident(line) operator(=) ident(lines)operator([)ident(i)operator(]) -operator(}) -comment(#-----------------------------) -comment(# the second readlines argument is a the) -comment(# record separator $/, just like perl, a blank) -comment(# separator splits the records into paragraphs) -constant(File)operator(.)ident(readlines)operator(()ident(file)operator(,) stringoperator(\))operator(.)ident(each) operator({) operator(|)ident(paragraph)operator(|) - comment(# do something with paragraph) - ident(puts) stringParagraph )inlinedelimiter(")> -operator(}) -comment(#-----------------------------) - - -comment(# @@PLEAC@@_8.6) - -global_variable($/) operator(=) stringoperator(;) -ident(srand)operator(;) - -constant(File)operator(.)ident(open)operator(()stringoperator(\))operator(.)ident(each) reserved(do) operator(|)ident(line)operator(|) - ident(adage) operator(=) ident(line) reserved(if) ident(rand)operator(()global_variable($.)operator(\)) operator(<) integer(1) -reserved(end) - -ident(puts) ident(adage)operator(;) - - -comment(# @@PLEAC@@_8.10) -reserved(begin) - ident(fh) operator(=) constant(File)operator(.)ident(open)operator(()ident(file)operator(,) stringoperator(\)) - ident(addr) operator(=) ident(fh)operator(.)ident(tell) reserved(unless) ident(fh)operator(.)ident(eof) reserved(while) ident(fh)operator(.)ident(gets) - ident(fh)operator(.)ident(truncate)operator(()ident(addr)operator(\)) -reserved(rescue) constant(SystemCallError) - global_variable($stderr)operator(.)ident(puts) string -reserved(end) - - -comment(# @@PLEAC@@_9.0) -ident(entry) operator(=) constant(File)operator(.)ident(stat)operator(()stringoperator(\)) -ident(entry) operator(=) constant(File)operator(.)ident(stat)operator(()stringoperator(\)) -ident(entry) operator(=) constant(File)operator(.)ident(stat)operator(()constant(INFILE)operator(\)) - -ident(entry) operator(=) constant(File)operator(.)ident(stat)operator(()stringoperator(\)) -ident(ctime) operator(=) ident(entry)operator(.)ident(ctime) -ident(size) operator(=) ident(entry)operator(.)ident(size) - -ident(f) operator(=) constant(File)operator(.)ident(open)operator(()ident(filename)operator(,) stringoperator(\)) - -comment(## There is no -T equivalent in Ruby, but we can still test emptiness) -reserved(if) ident(test)operator(()integer(?s)operator(,) ident(filename)operator(\)) - ident(puts) stringcontent( doesn't have text in it.)delimiter(")> - ident(exit) -reserved(end) - -constant(Dir)operator(.)ident(new)operator(()stringoperator(\))operator(.)ident(each) reserved(do) operator(|)ident(filename)operator(|) - ident(puts) stringdelimiter(")> -reserved(end) - - -comment(# @@PLEAC@@_9.1) -ident(file) operator(=) constant(File)operator(.)ident(stat)operator(()stringoperator(\)) -ident(readtime)operator(,) ident(writetime) operator(=) ident(file)operator(.)ident(atime)operator(,) ident(file)operator(.)ident(mtime) -ident(file)operator(.)ident(utime)operator(()ident(readtime)operator(,) ident(writetime)operator(\)) - -constant(SECONDS_PER_DAY) operator(=) integer(60) operator(*) integer(60) operator(*) integer(24) -ident(file) operator(=) constant(File)operator(.)ident(stat)operator(()stringoperator(\)) -ident(atime)operator(,) ident(mtime) operator(=) ident(file)operator(.)ident(atime)operator(,) ident(file)operator(.)ident(mtime) - -ident(atime) operator(-=) integer(7) operator(*) constant(SECONDS_PER_DAY) -ident(mtime) operator(-=) integer(7) operator(*) constant(SECONDS_PER_DAY) - -constant(File)operator(.)ident(utime)operator(()ident(atime)operator(,) ident(mtime)operator(,) ident(file)operator(\)) -ident(mtime) operator(=) constant(File)operator(.)ident(stat)operator(()ident(file)operator(\))operator(.)ident(mtime) -constant(File)operator(.)ident(utime)operator(()constant(Time)operator(.)ident(new)operator(,) ident(mtime)operator(,) ident(file)operator(\)) -constant(File)operator(.)ident(utime)operator(()constant(Time)operator(.)ident(new)operator(,) constant(File)operator(.)ident(stat)operator(()stringoperator(\))operator(.)ident(mtime)operator(,) ident(file)operator(\)) - -comment(#-----------------------------) -comment(#!/usr/bin/ruby -w) -comment(## uvi - vi a file without changing it's access times) - -reserved(if) pre_constant(ARGV)operator(.)ident(length) operator(!=) integer(1) - ident(puts) string - ident(exit) -reserved(end) -ident(file) operator(=) pre_constant(ARGV)operator([)integer(0)operator(]) -ident(atime)operator(,) ident(mtime) operator(=) constant(File)operator(.)ident(stat)operator(()ident(file)operator(\))operator(.)ident(atime)operator(,) constant(File)operator(.)ident(stat)operator(()ident(file)operator(\))operator(.)ident(mtime) -ident(system)operator(()constant(ENV)operator([)stringoperator(]) operator(||) stringoperator(,) ident(file)operator(\)) -constant(File)operator(.)ident(utime)operator(()ident(atime)operator(,) ident(mtime)operator(,) ident(file)operator(\)) -comment(#-----------------------------) - - -comment(# @@PLEAC@@_9.2) -constant(File)operator(.)ident(unlink)operator(()constant(FILENAME)operator(\)) - -ident(err_flg) operator(=) pre_constant(false) -ident(filenames)operator(.)ident(each) reserved(do) operator(|)ident(file)operator(|) - reserved(begin) - constant(File)operator(.)ident(unlink)operator(()ident(file)operator(\)) - reserved(rescue) - ident(err_flg) operator(=) global_variable($!) - reserved(end) -reserved(end) -ident(err_flg) reserved(and) ident(raise) stringoperator(\))delimiter(})>content(: )inlinedelimiter(")> - -constant(File)operator(.)ident(unlink)operator(()ident(file)operator(\)) - -ident(count) operator(=) ident(filenames)operator(.)ident(length) -ident(filenames)operator(.)ident(each) reserved(do) operator(|)ident(file)operator(|) - reserved(begin) - constant(File)operator(.)ident(unlink)operator(()ident(file)operator(\)) - reserved(rescue) - ident(count) operator(-=) integer(1) - reserved(end) -reserved(end) -reserved(if) ident(count) operator(!=) ident(filenames)operator(.)ident(length) - constant(STDERR)operator(.)ident(puts) stringcontent( of )inlinecontent( files)delimiter(")> -reserved(end) - - -comment(# @@PLEAC@@_9.3) -ident(require) string -constant(File)operator(.)ident(copy)operator(()ident(oldfile)operator(,) ident(newfile)operator(\)) - -ident(infile) operator(=) constant(File)operator(.)ident(open)operator(()ident(oldfile)operator(,) stringoperator(\)) -ident(outfile) operator(=) constant(File)operator(.)ident(open)operator(()ident(newfile)operator(,) stringoperator(\)) - -ident(blksize) operator(=) ident(infile)operator(.)ident(stat)operator(.)ident(blksize) -comment(# This doesn't handle partial writes or ^Z) -comment(# like the Perl version does.) -reserved(while) operator(()ident(line) operator(=) ident(infile)operator(.)ident(read)operator(()ident(blksize)operator(\))operator(\)) - ident(outfile)operator(.)ident(write)operator(()ident(line)operator(\)) -reserved(end) - -ident(infile)operator(.)ident(close) -ident(outfile)operator(.)ident(close) - -ident(system)operator(()stringcontent( )inlinedelimiter(")>operator(\)) comment(# unix) -ident(system)operator(()stringcontent( )inlinedelimiter(")>operator(\)) comment(# dos, vms) - -ident(require) string -constant(File)operator(.)ident(copy)operator(()stringoperator(,) stringoperator(\)) -constant(File)operator(.)ident(move)operator(()stringoperator(,) stringoperator(\)) - - -comment(# @@PLEAC@@_9.4) -global_variable($seen) operator(=) operator({)operator(}) comment(# must use global var to be seen inside of method below) - -reserved(def) method(do_my_thing)operator(()ident(filename)operator(\)) - ident(dev)operator(,) ident(ino) operator(=) constant(File)operator(.)ident(stat)operator(()ident(filename)operator(\))operator(.)ident(dev)operator(,) constant(File)operator(.)ident(stat)operator(()ident(filename)operator(\))operator(.)ident(ino) - reserved(unless) global_variable($seen)operator([)operator([)ident(dev)operator(,) ident(ino)operator(])operator(]) - comment(# do something with $filename because we haven't) - comment(# seen it before) - reserved(end) - global_variable($seen)operator([)operator([)ident(dev)operator(,) ident(ino)operator(])operator(]) operator(=) global_variable($seen)operator([)operator([)ident(dev)operator(,) ident(ino)operator(])operator(])operator(.)ident(to_i) operator(+) integer(1) -reserved(end) - -ident(files)operator(.)ident(each) reserved(do) operator(|)ident(filename)operator(|) - ident(dev)operator(,) ident(ino) operator(=) constant(File)operator(.)ident(stat)operator(()ident(filename)operator(\))operator(.)ident(dev)operator(,) constant(File)operator(.)ident(stat)operator(()ident(filename)operator(\))operator(.)ident(ino) - reserved(if) operator(!)global_variable($seen)operator(.)ident(has_key?)operator(()operator([)ident(dev)operator(,) ident(ino)operator(])operator(\)) - global_variable($seen)operator([)operator([)ident(dev)operator(,) ident(ino)operator(])operator(]) operator(=) operator([)operator(]) - reserved(end) - global_variable($seen)operator([)operator([)ident(dev)operator(,) ident(ino)operator(])operator(])operator(.)ident(push)operator(()ident(filename)operator(\)) -reserved(end) - -global_variable($seen)operator(.)ident(keys)operator(.)ident(sort)operator(.)ident(each) reserved(do) operator(|)ident(devino)operator(|) - ident(ino)operator(,) ident(dev) operator(=) ident(devino) - reserved(if) global_variable($seen)operator([)ident(devino)operator(])operator(.)ident(length) operator(>) integer(1) - comment(# $seen[devino] is a list of filenames for the same file) - reserved(end) -reserved(end) - - -comment(# @@PLEAC@@_9.5) -constant(Dir)operator(.)ident(open)operator(()ident(dirname)operator(\)) reserved(do) operator(|)ident(dir)operator(|) - ident(dir)operator(.)ident(each) reserved(do) operator(|)ident(file)operator(|) - comment(# do something with dirname/file) - ident(puts) ident(file) - reserved(end) -reserved(end) -comment(# Dir.close is automatic) - -comment(# No -T equivalent in Ruby) - -ident(dir)operator(.)ident(each) reserved(do) operator(|)ident(file)operator(|) - reserved(next) reserved(if) ident(file) operator(=)operator(~) regexp - comment(# ...) -reserved(end) - -reserved(def) method(plainfiles)operator(()ident(dir)operator(\)) - ident(dh) operator(=) constant(Dir)operator(.)ident(open)operator(()ident(dir)operator(\)) - ident(dh)operator(.)ident(entries)operator(.)ident(grep)operator(()regexpoperator(\))operator(.) - ident(map) operator({)operator(|)ident(file)operator(|) stringcontent(/)inlinedelimiter(")>operator(})operator(.) - ident(find_all) operator({)operator(|)ident(file)operator(|) ident(test)operator(()integer(?f)operator(,) ident(file)operator(\))operator(})operator(.) - ident(sort) -reserved(end) - - -comment(# @@PLEAC@@_9.6) -ident(list) operator(=) constant(Dir)operator(.)ident(glob)operator(()stringoperator(\)) - -ident(dir) operator(=) constant(Dir)operator(.)ident(open)operator(()ident(path)operator(\)) -ident(files) operator(=) ident(dir)operator(.)ident(entries)operator(.)ident(grep)operator(()regexpoperator(\)) -ident(dir)operator(.)ident(close) - -ident(files) operator(=) constant(Dir)operator(.)ident(glob)operator(()stringoperator(\)) -ident(files) operator(=) constant(Dir)operator(.)ident(open)operator(()ident(path)operator(\))operator(.)ident(entries)operator(.)ident(grep)operator(()regexpoperator(\)) - -ident(dir) operator(=) constant(Dir)operator(.)ident(new)operator(()ident(path)operator(\)) -ident(files) operator(=) ident(dir)operator(.)ident(entries)operator(.)ident(grep)operator(()regexpoperator(\)) - -reserved(begin) - ident(d) operator(=) constant(Dir)operator(.)ident(open)operator(()ident(dir)operator(\)) -reserved(rescue) constant(Errno)operator(::)constant(ENOENT) - ident(raise) stringcontent( for reading: )inlinedelimiter(")> -reserved(end) - -ident(files) operator(=) operator([)operator(]) -ident(d)operator(.)ident(each) reserved(do) operator(|)ident(file)operator(|) - ident(puts) ident(file) - reserved(next) reserved(unless) ident(file) operator(=)operator(~) regexp - - ident(filename) operator(=) stringcontent(/)inlinedelimiter(")> - comment(# There is no -T equivalent in Ruby, but we can still test emptiness) - ident(files)operator(.)ident(push)operator(()ident(filename)operator(\)) reserved(if) ident(test)operator(()integer(?s)operator(,) ident(filename)operator(\)) -reserved(end) - -ident(dirs)operator(.)ident(entries)operator(.)ident(grep)operator(()regexpoperator(\))operator(.) - ident(map) operator({) operator(|)ident(file)operator(|) operator([)ident(file)operator(,) stringcontent(/)inlinedelimiter(")>operator(])operator(}) operator(.) - ident(select) operator({) operator(|)ident(file)operator(|) ident(test)operator(()integer(?d)operator(,) ident(file)operator([)integer(1)operator(])operator(\)) operator(})operator(.) - ident(sort) operator({) operator(|)ident(a)operator(,)ident(b)operator(|) ident(a)operator([)integer(0)operator(]) operator(<=>) ident(b)operator([)integer(0)operator(]) operator(})operator(.) - ident(map) operator({) operator(|)ident(file)operator(|) ident(file)operator([)integer(1)operator(]) operator(}) - - -comment(# @@PLEAC@@_9.7) -ident(require) string -constant(Find)operator(.)ident(find)operator(()ident(dirlist)operator(\)) reserved(do) operator(|)ident(file)operator(|) - comment(# do whatever) -reserved(end) - -ident(require) string -ident(argv) operator(=) pre_constant(ARGV)operator(.)ident(empty?) operator(?) string operator(:) pre_constant(ARGV) -constant(Find)operator(.)ident(find)operator(()operator(*)ident(argv)operator(\)) reserved(do) operator(|)ident(file)operator(|) - ident(print) ident(file)operator(,) operator(()ident(test)operator(()integer(?d)operator(,) ident(file)operator(\)) operator(?) string operator(:) stringoperator(\)) -reserved(end) - -ident(require) string -ident(argv) operator(=) pre_constant(ARGV)operator(.)ident(empty?) operator(?) string operator(:) pre_constant(ARGV) -ident(sum) operator(=) integer(0) -constant(Find)operator(.)ident(find)operator(()operator(*)ident(argv)operator(\)) reserved(do) operator(|)ident(file)operator(|) - ident(size) operator(=) ident(test)operator(()integer(?s)operator(,) ident(file)operator(\)) operator(||) integer(0) - ident(sum) operator(+=) ident(size) -reserved(end) -ident(puts) stringoperator(\))delimiter(})>content( contains )inlinecontent( bytes)delimiter(")> - -ident(require) string -ident(argv) operator(=) pre_constant(ARGV)operator(.)ident(empty?) operator(?) string operator(:) pre_constant(ARGV) -ident(saved_size)operator(,) ident(saved_name) operator(=) operator(-)integer(1)operator(,) string -constant(Find)operator(.)ident(find)operator(()operator(*)ident(argv)operator(\)) reserved(do) operator(|)ident(file)operator(|) - ident(size) operator(=) ident(test)operator(()integer(?s)operator(,) ident(file)operator(\)) operator(||) integer(0) - reserved(next) reserved(unless) ident(test)operator(()integer(?f)operator(,) ident(file)operator(\)) operator(&&) ident(size) operator(>) ident(saved_size) - ident(saved_size) operator(=) ident(size) - ident(saved_name) operator(=) ident(file) -reserved(end) -ident(puts) stringcontent( in )inlineoperator(\))delimiter(})>content( is )inlinedelimiter(")> - -ident(require) string -ident(argv) operator(=) pre_constant(ARGV)operator(.)ident(empty?) operator(?) string operator(:) pre_constant(ARGV) -ident(age)operator(,) ident(name) operator(=) pre_constant(nil) -constant(Find)operator(.)ident(find)operator(()operator(*)ident(argv)operator(\)) reserved(do) operator(|)ident(file)operator(|) - ident(mtime) operator(=) constant(File)operator(.)ident(stat)operator(()ident(file)operator(\))operator(.)ident(mtime) - reserved(next) reserved(if) ident(age) operator(&&) ident(age) operator(>) ident(mtime) - ident(age) operator(=) ident(mtime) - ident(name) operator(=) ident(file) -reserved(end) -ident(puts) stringcontent( )inlinedelimiter(")> - -comment(#-----------------------------) -comment(#!/usr/bin/ruby -w) -comment(# fdirs - find all directories) -ident(require) string -ident(argv) operator(=) pre_constant(ARGV)operator(.)ident(empty?) operator(?) string operator(:) pre_constant(ARGV) -constant(File)operator(.)ident(find)operator(()operator(*)ident(argv)operator(\)) operator({) operator(|)ident(file)operator(|) ident(puts) ident(file) reserved(if) ident(test)operator(()integer(?d)operator(,) ident(file)operator(\)) operator(}) -comment(#-----------------------------) - - -comment(# @@PLEAC@@_9.8) -ident(require) string - -ident(puts) stringcontent( dir ...)delimiter(")> reserved(if) pre_constant(ARGV)operator(.)ident(empty?) -pre_constant(ARGV)operator(.)ident(each) reserved(do) operator(|)ident(dir)operator(|) - constant(FileUtils)operator(.)ident(rmtree)operator(()ident(dir)operator(\)) -reserved(end) - - -comment(# @@PLEAC@@_9.9) -ident(require) string -ident(names)operator(.)ident(each) reserved(do) operator(|)ident(file)operator(|) - ident(newname) operator(=) ident(file) - reserved(begin) - constant(File)operator(.)ident(move)operator(()ident(file)operator(,) ident(newname)operator(\)) - reserved(rescue) constant(Errno)operator(::)constant(EPERM) - global_variable($stderr)operator(.)ident(puts) stringcontent( to )inlinecontent(: )inlinedelimiter(")> - reserved(end) -reserved(end) - -ident(require) string -ident(op) operator(=) pre_constant(ARGV)operator(.)ident(empty?) operator(?) operator(()ident(raise) stringoperator(\)) operator(:) pre_constant(ARGV)operator(.)ident(shift) -ident(argv) operator(=) pre_constant(ARGV)operator(.)ident(empty?) operator(?) global_variable($stdin)operator(.)ident(readlines)operator(.)ident(map) operator({) operator(|)ident(f)operator(|) ident(f)operator(.)ident(chomp) operator(}) operator(:) pre_constant(ARGV) -ident(argv)operator(.)ident(each) reserved(do) operator(|)ident(file)operator(|) - ident(was) operator(=) ident(file) - ident(file) operator(=) ident(eval)operator(()stringdelimiter(")>operator(\)) - constant(File)operator(.)ident(move)operator(()ident(was)operator(,) ident(file)operator(\)) reserved(unless) ident(was) operator(==) ident(file) -reserved(end) - - -comment(# @@PLEAC@@_9.10) -ident(base) operator(=) constant(File)operator(.)ident(basename)operator(()ident(path)operator(\)) -ident(dir) operator(=) constant(File)operator(.)ident(dirname)operator(()ident(path)operator(\)) -comment(# ruby has no fileparse equivalent) -ident(dir)operator(,) ident(base) operator(=) constant(File)operator(.)ident(split)operator(()ident(path)operator(\)) -ident(ext) operator(=) ident(base)operator(.)ident(scan)operator(()regexpoperator(\))operator(.)ident(to_s) - -ident(path) operator(=) string -ident(file) operator(=) constant(File)operator(.)ident(basename)operator(()ident(path)operator(\)) -ident(dir) operator(=) constant(File)operator(.)ident(dirname)operator(()ident(path)operator(\)) - -ident(puts) stringcontent(, file is )inlinedelimiter(")> -comment(# dir is /usr/lib, file is libc.a) - -ident(path) operator(=) string -ident(dir)operator(,) ident(filename) operator(=) constant(File)operator(.)ident(split)operator(()ident(path)operator(\)) -ident(name)operator(,) ident(ext) operator(=) ident(filename)operator(.)ident(split)operator(()regexpoperator(\)) -ident(puts) stringcontent(, name is )inlinecontent(, ext is )inlinedelimiter(")> -comment(# NOTE: The Ruby code prints) -comment(# dir is /usr/lib, name is libc, extension is .a) -comment(# while the Perl code prints a '/' after the directory name) -comment(# dir is /usr/lib/, name is libc, extension is .a) - -comment(# No fileparse_set_fstype(\) equivalent in ruby) - -reserved(def) method(extension)operator(()ident(path)operator(\)) - ident(ext) operator(=) ident(path)operator(.)ident(scan)operator(()regexpoperator(\))operator(.)ident(to_s) - ident(ext)operator(.)ident(sub)operator(()regexpoperator(,) stringoperator(\)) -reserved(end) - - -comment(# @@PLEAC@@_9.11) -comment(#-----------------------------) -comment(#!/usr/bin/ruby -w) -comment(# symirror - build spectral forest of symlinks) - -ident(require) string -ident(require) string - -ident(raise) stringcontent( realdir mirrordir)delimiter(")> reserved(unless) pre_constant(ARGV)operator(.)ident(size) operator(==) integer(2) - -ident(srcdir)operator(,)ident(dstdir) operator(=) pre_constant(ARGV) -ident(srcmode) operator(=) constant(File)operator(::)ident(stat)operator(()ident(srcdir)operator(\))operator(.)ident(mode) -constant(Dir)operator(.)ident(mkdir)operator(()ident(dstdir)operator(,) ident(srcmode) operator(&) integer(07777)operator(\)) reserved(unless) ident(test)operator(()integer(?d)operator(,) ident(dstdir)operator(\)) - -comment(# fix relative paths) -constant(Dir)operator(.)ident(chdir)operator(()ident(srcdir)operator(\)) operator({)ident(srcdir) operator(=) constant(Dir)operator(.)ident(pwd)operator(}) -constant(Dir)operator(.)ident(chdir)operator(()ident(dstdir)operator(\)) operator({)ident(dstdir) operator(=) constant(Dir)operator(.)ident(pwd)operator(}) - -constant(Find)operator(.)ident(find)operator(()ident(srcdir)operator(\)) reserved(do) operator(|)ident(srcfile)operator(|) - reserved(if) ident(test)operator(()integer(?d)operator(,) ident(srcfile)operator(\)) - ident(dest) operator(=) ident(srcfile)operator(.)ident(sub)operator(()regexpdelimiter(/)>operator(,) ident(dstdir)operator(\)) - ident(dmode) operator(=) constant(File)operator(::)ident(stat)operator(()ident(srcfile)operator(\))operator(.)ident(mode) operator(&) integer(07777) - constant(Dir)operator(.)ident(mkdir)operator(()ident(dest)operator(,) ident(dmode)operator(\)) reserved(unless) ident(test)operator(()integer(?d)operator(,) ident(dest)operator(\)) - ident(a) operator(=) constant(Dir)operator([)stringcontent(/*)delimiter(")>operator(])operator(.)ident(reject)operator({)operator(|)ident(f)operator(|) ident(test)operator(()integer(?d)operator(,) ident(f)operator(\))operator(}) - constant(FileUtils)operator(.)ident(ln_s)operator(()ident(a)operator(,) ident(dest)operator(\)) - reserved(end) -reserved(end) - - -comment(# @@PLEAC@@_9.12) -comment(# we use the Getopt/Declare library here for convenience:) -comment(# http://raa.ruby-lang.org/project/getoptdeclare/) -comment(#-----------------------------) -comment(#!/usr/bin/ruby -w) -comment(# lst - list sorted directory contents (depth first\)) - -ident(require) string -ident(require) string -ident(require) string - -comment(# Note: in the option-spec below there must by at least one hard) -comment(# tab in between each -option and its description. For example) -comment(# -i read from stdin) - -ident(opts) operator(=) constant(Getopt)operator(::)constant(Declare)operator(.)ident(new)operator(()stringoperator(\))string - -global_variable($sort_criteria) operator(||=) symbol(:mtime) -ident(files) operator(=) operator({)operator(}) -constant(DIRS) operator(=) ident(opts)operator([)stringoperator(]) operator(?) global_variable($stdin)operator(.)ident(readlines)operator(.)ident(map)operator({)operator(|)ident(f)operator(|)ident(f)operator(.)ident(chomp!)operator(}) operator(:) pre_constant(ARGV) -constant(DIRS)operator(.)ident(each) reserved(do) operator(|)ident(dir)operator(|) - constant(Find)operator(.)ident(find)operator(()ident(dir)operator(\)) reserved(do) operator(|)ident(ent)operator(|) - ident(files)operator([)ident(ent)operator(]) operator(=) constant(File)operator(::)ident(stat)operator(()ident(ent)operator(\)) - reserved(end) -reserved(end) -ident(entries) operator(=) ident(files)operator(.)ident(keys)operator(.)ident(sort_by)operator({)operator(|)ident(f)operator(|) ident(files)operator([)ident(f)operator(])operator(.)ident(send)operator(()global_variable($sort_criteria)operator(\))operator(}) -ident(entries) operator(=) ident(entries)operator(.)ident(reverse) reserved(unless) ident(opts)operator([)stringoperator(]) - -ident(entries)operator(.)ident(each) reserved(do) operator(|)ident(ent)operator(|) - reserved(unless) ident(opts)operator([)stringoperator(]) - ident(puts) ident(ent) - reserved(next) - reserved(end) - ident(stats) operator(=) ident(files)operator([)ident(ent)operator(]) - ident(ftime) operator(=) ident(stats)operator(.)ident(send)operator(()global_variable($sort_criteria) operator(==) symbol(:size) operator(?) symbol(:mtime) operator(:) global_variable($sort_criteria)operator(\)) - ident(printf) stringoperator(,) - ident(stats)operator(.)ident(ino)operator(,) - ident(stats)operator(.)ident(mode) operator(&) integer(07777)operator(,) - ident(stats)operator(.)ident(nlink)operator(,) - constant(ETC)operator(::)constant(PASSWD)operator([)ident(stats)operator(.)ident(uid)operator(])operator(.)ident(name)operator(,) - constant(ETC)operator(::)constant(GROUP)operator([)ident(stats)operator(.)ident(gid)operator(])operator(.)ident(name)operator(,) - ident(stats)operator(.)ident(size)operator(,) - ident(ftime)operator(.)ident(strftime)operator(()stringoperator(\))operator(,) - ident(ent) -reserved(end) - - -comment(# @@PLEAC@@_10.0) -reserved(def) method(hello) - global_variable($greeted) operator(+=) integer(1) comment(# in Ruby, a variable beginning with $ is global (can be any type of course\)) - ident(puts) string -reserved(end) - -comment(# We need to initialize $greeted before it can be used, because "+=" is waiting a Numeric object) -global_variable($greeted) operator(=) integer(0) -ident(hello) comment(# note that appending (\) is optional to function calls with no parameters) - - -comment(# @@PLEAC@@_10.1) -comment(# In Ruby, parameters are named anyway) -reserved(def) method(hypotenuse)operator(()ident(side1)operator(,) ident(side2)operator(\)) - constant(Math)operator(.)ident(sqrt)operator(()ident(side1)operator(**)integer(2) operator(+) ident(side2)operator(**)integer(2)operator(\)) comment(# the sqrt function comes from the Math module) -reserved(end) -ident(diag) operator(=) ident(hypotenuse)operator(()integer(3)operator(,) integer(4)operator(\)) - -ident(puts) ident(hypotenuse)operator(()integer(3)operator(,) integer(4)operator(\)) - -ident(a) operator(=) operator([)integer(3)operator(,) integer(4)operator(]) -ident(print) ident(hypotenuse)operator(()operator(*)ident(a)operator(\)) comment(# the star operator will magically convert an Array into a "tuple") - -ident(both) operator(=) ident(men) operator(+) ident(women) - -comment(# In Ruby, all objects are references, so the same problem arises; we then return a new object) -ident(nums) operator(=) operator([)float(1.4)operator(,) float(3.5)operator(,) float(6.7)operator(]) -reserved(def) method(int_all)operator(()ident(n)operator(\)) - ident(n)operator(.)ident(collect) operator({) operator(|)ident(v)operator(|) ident(v)operator(.)ident(to_i) operator(}) -reserved(end) -ident(ints) operator(=) ident(int_all)operator(()ident(nums)operator(\)) - -ident(nums) operator(=) operator([)float(1.4)operator(,) float(3.5)operator(,) float(6.7)operator(]) -reserved(def) method(trunc_em)operator(()ident(n)operator(\)) - ident(n)operator(.)ident(collect!) operator({) operator(|)ident(v)operator(|) ident(v)operator(.)ident(to_i) operator(}) comment(# the bang-version of collect modifies the object) -reserved(end) -ident(trunc_em)operator(()ident(nums)operator(\)) - -comment(# Ruby has two chomp version:) -comment(# ``chomp'' chomps the record separator and returns what's expected) -comment(# ``chomp!'' does the same but also modifies the parameter object) - - -comment(# @@PLEAC@@_10.2) -reserved(def) method(somefunc) - ident(variable) operator(=) ident(something) comment(# variable is local by default) -reserved(end) - -ident(name)operator(,) ident(age) operator(=) pre_constant(ARGV) -ident(start) operator(=) ident(fetch_time) - -ident(a)operator(,) ident(b) operator(=) ident(pair) comment(# will succeed if pair is an Array object (like ARGV is\)) -ident(c) operator(=) ident(fetch_time) - -comment(# In ruby, run_check can't access a, b, or c until they are) -comment(# explicitely defined global (using leading $\), even if they are) -comment(# both defined in the same scope) - -reserved(def) method(check_x)operator(()ident(x)operator(\)) - ident(y) operator(=) string - ident(run_check) - reserved(if) global_variable($condition) - ident(puts) string - reserved(end) -reserved(end) - -comment(# The following will keep a reference to the array, though the) -comment(# results will be slightly different from perl: the last element) -comment(# of $global_array will be itself an array) -reserved(def) method(save_array)operator(()ident(ary)operator(\)) - global_variable($global_array) operator(<<) ident(ary) -reserved(end) - -comment(# The following gives the same results as in Perl for $global_array,) -comment(# though it doesn't illustrate anymore the way to keep a reference) -comment(# to an object: $global_array is extended with the elements of ary) -reserved(def) method(save_array)operator(()ident(ary)operator(\)) - global_variable($global_array) operator(+=) ident(ary) -reserved(end) - - -comment(# @@PLEAC@@_10.3) -comment(# In Ruby, AFAIK a method cannot access "local variables" defined) -comment(# upper scope; mostly because everything is an object, so you'll) -comment(# do the same by defining an attribute or a static attribute) - -comment(# In Ruby the BEGIN also exists:) -reserved(BEGIN) operator({) ident(puts) string operator(}) -ident(puts) string -reserved(BEGIN) operator({) ident(puts) string operator(}) -comment(# gives:) -comment(# hello from BEGIN) -comment(# hello from 2nd BEGIN) -comment(# hello from main) - -comment(# In Ruby, it can be written as a static method and a static) -comment(# variable) -reserved(class) class(Counter) - class_variable(@@counter) operator(=) integer(0) - reserved(def) constant(Counter)operator(.)ident(next_counter)operator(;) class_variable(@@counter) operator(+=) integer(1)operator(;) reserved(end) -reserved(end) - -comment(# There is no need of BEGIN since the variable will get) -comment(# initialized when parsing) -reserved(class) class(Counter) - class_variable(@@counter) operator(=) integer(42) - reserved(def) constant(Counter)operator(.)ident(next_counter)operator(;) class_variable(@@counter) operator(+=) integer(1)operator(;) reserved(end) - reserved(def) constant(Counter)operator(.)ident(prev_counter)operator(;) class_variable(@@counter) operator(-=) integer(1)operator(;) reserved(end) -reserved(end) - - -comment(# @@PLEAC@@_10.4) -comment(# You can either get the whole trace as an array of strings, each) -comment(# string telling which file, line and method is calling:) -ident(caller) - -comment(# ...or only the last caller) -ident(caller)operator([)integer(0)operator(]) - -comment(# We need to extract just the method name of the backtrace:) -reserved(def) method(whoami)operator(;) ident(caller)operator(()operator(\))operator([)integer(0)operator(]) operator(=)operator(~) regexp operator(?) global_variable($1) operator(:) stringoperator(;) reserved(end) -reserved(def) method(whowasi)operator(;) ident(caller)operator(()operator(\))operator([)integer(1)operator(]) operator(=)operator(~) regexp operator(?) global_variable($1) operator(:) stringoperator(;) reserved(end) - - -comment(# @@PLEAC@@_10.5) -comment(# In Ruby, every value is a reference on an object, thus there is) -comment(# no such problem) -ident(array_diff)operator(()ident(array1)operator(,) ident(array2)operator(\)) - -reserved(def) method(add_vecpair)operator(()ident(a1)operator(,) ident(a2)operator(\)) - ident(results) operator(=) operator([)operator(]) - ident(a1)operator(.)ident(each_index) operator({) operator(|)ident(i)operator(|) ident(results) operator(<<) operator(()ident(a1)operator([)ident(i)operator(]) operator(+) ident(a2)operator([)ident(i)operator(])operator(\)) operator(}) - ident(results) -reserved(end) -ident(a) operator(=) operator([)integer(1)operator(,) integer(2)operator(]) -ident(b) operator(=) operator([)integer(5)operator(,) integer(8)operator(]) -ident(c) operator(=) ident(add_vecpair)operator(()ident(a)operator(,) ident(b)operator(\)) -ident(p) ident(c) - -comment(# Add this to the beginning of the function to check if we were) -comment(# given two arrays) -ident(a1)operator(.)ident(type) operator(==) constant(Array) operator(&&) ident(a2)operator(.)ident(type) operator(==) constant(Array) reserved(or) - ident(raise) stringcontent( )inlinecontent(\))delimiter(")> - - -comment(# @@PLEAC@@_10.6) -comment(# There is no return context in Ruby) - - -comment(# @@PLEAC@@_10.7) -comment(# Like in Perl, we need to fake with a hash, but it's dirty :-() -reserved(def) method(thefunc)operator(()ident(param_args)operator(\)) - ident(args) operator(=) operator({) string operator(=)operator(>) stringoperator(,) string operator(=)operator(>) stringoperator(,) string operator(=)operator(>) integer(0) operator(}) - ident(args)operator(.)ident(update)operator(()ident(param_args)operator(\)) - reserved(if) operator(()ident(args)operator([)stringoperator(]) operator(=)operator(~) regexp operator(\)) - comment(# .....) - reserved(end) -reserved(end) - -ident(thefunc)operator(()operator({) string operator(=)operator(>) stringoperator(,) string operator(=)operator(>) stringoperator(,) string operator(=)operator(>) string operator(})operator(\)) -ident(thefunc)operator(()operator({)operator(})operator(\)) - - -comment(# @@PLEAC@@_10.8) -comment(# there is no "undef" direct equivalent but there is the slice equiv:) -ident(a)operator(,) ident(c) operator(=) ident(func)operator(.)ident(indexes)operator(()integer(0)operator(,) integer(2)operator(\)) - - -comment(# @@PLEAC@@_10.9) -comment(# Ruby has no such limitation:) -reserved(def) method(somefunc) - ident(ary) operator(=) operator([)operator(]) - ident(hash) operator(=) operator({)operator(}) - comment(# ...) - reserved(return) ident(ary)operator(,) ident(hash) -reserved(end) -ident(arr)operator(,) ident(dict) operator(=) ident(somefunc) - -ident(array_of_hashes) operator(=) ident(fn) -ident(h1)operator(,) ident(h2)operator(,) ident(h3) operator(=) ident(fn) - - -comment(# @@PLEAC@@_10.10) -reserved(return) -comment(# or (equivalent\)) -reserved(return) pre_constant(nil) - - -comment(# @@PLEAC@@_10.11) -comment(# You can't prototype in Ruby regarding types :-() -comment(# Though, you can force the number of arguments:) -reserved(def) method(func_with_no_arg)operator(;) reserved(end) -reserved(def) method(func_with_no_arg)operator(()operator(\))operator(;) reserved(end) -reserved(def) method(func_with_one_arg)operator(()ident(a1)operator(\))operator(;) reserved(end) -reserved(def) method(func_with_two_args)operator(()ident(a1)operator(,) ident(a2)operator(\))operator(;) reserved(end) -reserved(def) method(func_with_any_number_of_args)operator(()operator(*)ident(args)operator(\))operator(;) reserved(end) - - -comment(# @@PLEAC@@_10.12) -ident(raise) string comment(# raise exception) - -reserved(begin) - ident(val) operator(=) ident(func) -reserved(rescue) constant(Exception) operator(=)operator(>) ident(msg) - global_variable($stderr)operator(.)ident(puts) stringdelimiter(")> -reserved(end) - -comment(# In Ruby the rescue statement uses an exception class, every) -comment(# exception which is not matched is still continuing) -reserved(begin) - ident(val) operator(=) ident(func) -reserved(rescue) constant(FullMoonError) - operator(...) -reserved(end) - - -comment(# @@PLEAC@@_10.13) -comment(# Saving Global Values) -comment(# Of course we can just save the value and restore it later:) -reserved(def) method(print_age) - ident(puts) stringdelimiter(")> -reserved(end) - -global_variable($age) operator(=) integer(18) comment(# global variable) -ident(print_age)operator(()operator(\)) -reserved(if) ident(condition) - ident(safeage) operator(=) global_variable($age) - global_variable($age) operator(=) integer(23) - ident(print_age)operator(()operator(\)) - global_variable($age) operator(=) ident(safeage) -reserved(end) - -comment(# We can also use a method that saves the global variable and) -comment(# restores it automatically when the block is left:) - -reserved(def) method(local)operator(()ident(var)operator(\)) - ident(eval)operator(()stringdelimiter(")>operator(\)) - reserved(begin) - ident(result) operator(=) reserved(yield) - reserved(ensure) - comment(# we want to call this even if we got an exception) - ident(eval)operator(()stringcontent( = save)delimiter(")>operator(\)) - reserved(end) - ident(result) -reserved(end) - -ident(condition) operator(=) pre_constant(true) -global_variable($age) operator(=) integer(18) -ident(print_age)operator(()operator(\)) -reserved(if) ident(condition) - ident(local)operator(()symbol(:$age)operator(\)) operator({) - global_variable($age) operator(=) integer(23) - ident(print_age)operator(()operator(\)) - operator(}) -reserved(end) -ident(print_age)operator(()operator(\)) - -comment(# There is no need to use local(\) for filehandles or directory) -comment(# handles in ruby because filehandles are normal objects.) - - -comment(# @@PLEAC@@_10.14) -comment(# In Ruby you may redefine a method [but not overload it :-(]) -comment(# just by defining again with the same name.) -reserved(def) method(foo)operator(;) ident(puts) stringoperator(;) reserved(end) -reserved(def) method(foo)operator(;) ident(puts) stringoperator(;) reserved(end) -ident(foo) -comment(#=> bar) - -comment(# You can also take a reference to an existing method before) -comment(# redefining a new one, using the `alias' keyword) -reserved(def) method(foo)operator(;) ident(puts) stringoperator(;) reserved(end) -reserved(alias) ident(foo_orig) ident(foo) -reserved(def) method(foo)operator(;) ident(puts) stringoperator(;) reserved(end) -ident(foo_orig) -ident(foo) -comment(#=> foo) -comment(#=> bar) - -comment(# AFAIK, there is no direct way to create a new method whose name) -comment(# comes from a variable, so use "eval") -ident(colors) operator(=) string -ident(colors)operator(.)ident(each) operator({) operator(|)ident(c)operator(|) - ident(eval) stringstringcontent((*a\) - "" + a.to_s + "" - end)delimiter( - EOS)> -operator(}) - - -comment(# @@PLEAC@@_10.15) -reserved(def) method(method_missing)operator(()ident(name)operator(,) operator(*)ident(args)operator(\)) - string)delimiter(")> operator(+) ident(args)operator(.)ident(join)operator(()stringoperator(\)) operator(+) string)delimiter(")> -reserved(end) -ident(puts) ident(chartreuse)operator(()stringoperator(\)) - - -comment(# @@PLEAC@@_10.16) -reserved(def) method(outer)operator(()ident(arg)operator(\)) - ident(x) operator(=) ident(arg) operator(+) integer(35) - ident(inner) operator(=) ident(proc) operator({) ident(x) operator(*) integer(19) operator(}) - ident(x) operator(+) ident(inner)operator(.)ident(call)operator(()operator(\)) -reserved(end) - - -comment(# @@PLEAC@@_10.17) -comment(#!/usr/bin/ruby -w) -comment(# mailsort - sort mbox by different criteria) -ident(require) string -ident(require) string - -comment(# Objects of class Mail represent a single mail.) -reserved(class) class(Mail) - ident(attr_accessor) symbol(:no) - ident(attr_accessor) symbol(:subject) - ident(attr_accessor) symbol(:fulltext) - ident(attr_accessor) symbol(:date) - - reserved(def) method(initialize) - instance_variable(@fulltext) operator(=) string - instance_variable(@subject) operator(=) string - reserved(end) - - reserved(def) method(append)operator(()ident(para)operator(\)) - instance_variable(@fulltext) operator(<<) ident(para) - reserved(end) - - comment(# this is called if you call puts(mail\)) - reserved(def) method(to_s) - instance_variable(@fulltext) - reserved(end) -reserved(end) - -comment(# represents a list of mails.) -reserved(class) class(Mailbox) operator(<) constant(Array) - - constant(Subjectpattern) operator(=) constant(Regexp)operator(.)ident(new)operator(()stringoperator(\)) - constant(Datepattern) operator(=) constant(Regexp)operator(.)ident(new)operator(()stringoperator(\)) - - comment(# reads mails from open file and stores them) - reserved(def) method(read)operator(()ident(file)operator(\)) - global_variable($INPUT_RECORD_SEPARATOR) operator(=) string comment(# paragraph reads) - ident(msgno) operator(=) operator(-)integer(1) - ident(file)operator(.)ident(each) operator({) operator(|)ident(para)operator(|) - reserved(if) ident(para) operator(=)operator(~) regexp - ident(mail) operator(=) constant(Mail)operator(.)ident(new) - ident(mail)operator(.)ident(no) operator(=) operator(()ident(msgno) operator(+=) integer(1)operator(\)) - ident(md) operator(=) constant(Subjectpattern)operator(.)ident(match)operator(()ident(para)operator(\)) - reserved(if) ident(md) - ident(mail)operator(.)ident(subject) operator(=) ident(md)operator([)integer(1)operator(]) - reserved(end) - ident(md) operator(=) constant(Datepattern)operator(.)ident(match)operator(()ident(para)operator(\)) - reserved(if) ident(md) - ident(mail)operator(.)ident(date) operator(=) constant(DateTime)operator(.)ident(parse)operator(()ident(md)operator([)integer(1)operator(])operator(\)) - reserved(else) - ident(mail)operator(.)ident(date) operator(=) constant(DateTime)operator(.)ident(now) - reserved(end) - pre_constant(self)operator(.)ident(push)operator(()ident(mail)operator(\)) - reserved(end) - ident(mail)operator(.)ident(append)operator(()ident(para)operator(\)) reserved(if) ident(mail) - operator(}) - reserved(end) - - reserved(def) method(sort_by_subject_and_no) - pre_constant(self)operator(.)ident(sort_by) operator({) operator(|)ident(m)operator(|) - operator([)ident(m)operator(.)ident(subject)operator(,) ident(m)operator(.)ident(no)operator(]) - operator(}) - reserved(end) - - comment(# sorts by a list of attributs of mail, given as symbols) - reserved(def) method(sort_by_attributs)operator(()operator(*)ident(attrs)operator(\)) - comment(# you can sort an Enumerable by an array of) - comment(# values, they would be compared) - comment(# from ary[0] to ary[n]t, say:) - comment(# ['b',1] > ['a',10] > ['a',9]) - pre_constant(self)operator(.)ident(sort_by) operator({) operator(|)ident(elem)operator(|) - ident(attrs)operator(.)ident(map) operator({) operator(|)ident(attr)operator(|) - ident(elem)operator(.)ident(send)operator(()ident(attr)operator(\)) - operator(}) - operator(}) - reserved(end) - -reserved(end) - -ident(mailbox) operator(=) constant(Mailbox)operator(.)ident(new) -ident(mailbox)operator(.)ident(read)operator(()pre_constant(ARGF)operator(\)) - -comment(# print only subjects sorted by subject and number) -reserved(for) ident(m) reserved(in) ident(mailbox)operator(.)ident(sort_by_subject_and_no) - ident(puts)operator(()ident(m)operator(.)ident(subject)operator(\)) -reserved(end) - -comment(# print complete mails sorted by date, then subject, then number) -reserved(for) ident(m) reserved(in) ident(mailbox)operator(.)ident(sort_by_attributs)operator(()symbol(:date)operator(,) symbol(:subject)operator(\)) - ident(puts)operator(()ident(m)operator(\)) -reserved(end) - - -comment(# @@PLEAC@@_11.7) -reserved(def) method(mkcounter)operator(()ident(count)operator(\)) - ident(start) operator(=) ident(count) - ident(bundle) operator(=) operator({) - string operator(=)operator(>) ident(proc) operator({) ident(count) operator(+=) integer(1) operator(})operator(,) - string operator(=)operator(>) ident(proc) operator({) ident(count) operator(-=) integer(1) operator(})operator(,) - string operator(=)operator(>) ident(proc) operator({) ident(count) operator(=) ident(start) operator(}) - operator(}) - ident(bundle)operator([)stringoperator(]) operator(=) ident(bundle)operator([)stringoperator(]) - reserved(return) ident(bundle) -reserved(end) - -ident(c1) operator(=) ident(mkcounter)operator(()integer(20)operator(\)) -ident(c2) operator(=) ident(mkcounter)operator(()integer(77)operator(\)) - -ident(puts) stringoperator(])operator(.)ident(call)delimiter(})>delimiter(")> comment(# 21) -ident(puts) stringoperator(])operator(.)ident(call)delimiter(})>delimiter(")> comment(# 78) -ident(puts) stringoperator(])operator(.)ident(call)delimiter(})>delimiter(")> comment(# 22) -ident(puts) stringoperator(])operator(.)ident(call)delimiter(})>delimiter(")> comment(# 21) -ident(puts) stringoperator(])operator(.)ident(call)delimiter(})>delimiter(")> comment(# 20) -ident(puts) stringoperator(])operator(.)ident(call)delimiter(})>delimiter(")> comment(# 77) - - -comment(# @@PLEAC@@_11.15) -reserved(class) class(Binary_tree) - reserved(def) method(initialize)operator(()ident(val)operator(\)) - instance_variable(@value) operator(=) ident(val) - instance_variable(@left) operator(=) pre_constant(nil) - instance_variable(@right) operator(=) pre_constant(nil) - reserved(end) - - comment(# insert given value into proper point of) - comment(# provided tree. If no tree provided,) - comment(# use implicit pass by reference aspect of @_) - comment(# to fill one in for our caller.) - reserved(def) method(insert)operator(()ident(val)operator(\)) - reserved(if) ident(val) operator(<) instance_variable(@value) reserved(then) - reserved(if) instance_variable(@left) reserved(then) - instance_variable(@left)operator(.)ident(insert)operator(()ident(val)operator(\)) - reserved(else) - instance_variable(@left) operator(=) constant(Binary_tree)operator(.)ident(new)operator(()ident(val)operator(\)) - reserved(end) - reserved(elsif) ident(val) operator(>) instance_variable(@value) reserved(then) - reserved(if) instance_variable(@right) reserved(then) - instance_variable(@right)operator(.)ident(insert)operator(()ident(val)operator(\)) - reserved(else) - instance_variable(@right) operator(=) constant(Binary_tree)operator(.)ident(new)operator(()ident(val)operator(\)) - reserved(end) - reserved(else) - ident(puts) string - comment(# do nothing, no double values) - reserved(end) - reserved(end) - - comment(# recurse on left child,) - comment(# then show current value,) - comment(# then recurse on right child.) - reserved(def) method(in_order) - instance_variable(@left)operator(.)ident(in_order) reserved(if) instance_variable(@left) - ident(print) instance_variable(@value)operator(,) string - instance_variable(@right)operator(.)ident(in_order) reserved(if) instance_variable(@right) - reserved(end) - - comment(# show current value,) - comment(# then recurse on left child,) - comment(# then recurse on right child.) - reserved(def) method(pre_order) - ident(print) instance_variable(@value)operator(,) string - instance_variable(@left)operator(.)ident(pre_order) reserved(if) instance_variable(@left) - instance_variable(@right)operator(.)ident(pre_order) reserved(if) instance_variable(@right) - reserved(end) - - comment(# recurse on left child,) - comment(# then recurse on right child,) - comment(# then show current value.) - reserved(def) method(post_order) - instance_variable(@left)operator(.)ident(post_order) reserved(if) instance_variable(@left) - instance_variable(@right)operator(.)ident(post_order) reserved(if) instance_variable(@right) - ident(print) instance_variable(@value)operator(,) string - reserved(end) - - comment(# find out whether provided value is in the tree.) - comment(# if so, return the node at which the value was found.) - comment(# cut down search time by only looking in the correct) - comment(# branch, based on current value.) - reserved(def) method(search)operator(()ident(val)operator(\)) - reserved(if) ident(val) operator(==) instance_variable(@value) reserved(then) - reserved(return) pre_constant(self) - reserved(elsif) ident(val) operator(<) instance_variable(@value) reserved(then) - reserved(return) instance_variable(@left)operator(.)ident(search)operator(()ident(val)operator(\)) reserved(if) instance_variable(@left) - reserved(return) pre_constant(nil) - reserved(else) - reserved(return) instance_variable(@right)operator(.)ident(search)operator(()ident(val)operator(\)) reserved(if) instance_variable(@right) - reserved(return) pre_constant(nil) - reserved(end) - reserved(end) -reserved(end) - -comment(# first generate 20 random inserts) -ident(test) operator(=) constant(Binary_tree)operator(.)ident(new)operator(()integer(0)operator(\)) -reserved(for) ident(a) reserved(in) integer(0)operator(..)integer(20) - ident(test)operator(.)ident(insert)operator(()ident(rand)operator(()integer(1000)operator(\))operator(\)) -reserved(end) - -comment(# now dump out the tree all three ways) -ident(print) stringoperator(;) ident(test)operator(.)ident(pre_order)operator(;) ident(puts) string -ident(print) stringoperator(;) ident(test)operator(.)ident(in_order)operator(;) ident(puts) string -ident(print) stringoperator(;) ident(test)operator(.)ident(post_order)operator(;) ident(puts) string - -ident(print) string -reserved(while) ident(gets) - ident(print) ident(test)operator(.)ident(search)operator(()global_variable($_)operator(.)ident(to_i)operator(\)) - ident(print) string -reserved(end) - - -comment(# @@PLEAC@@_12.0) -comment(# class and module names need to have the first letter capitalized) -reserved(module) class(Alpha) - constant(NAME) operator(=) string -reserved(end) -reserved(module) class(Omega) - constant(NAME) operator(=) string -reserved(end) -ident(puts) stringcontent(, Omega is )inlinedelimiter(")> - -comment(# ruby doesn't differentiate beteen compile-time and run-time) -ident(require) string -ident(require) string comment(# assumes the .rb) -ident(require) string -ident(require) string comment(# assumes the .rb) -ident(load) string comment(# require only loads the file once) - -reserved(module) class(Cards) - reserved(module) class(Poker) - instance_variable(@card_deck) operator(=) constant(Array)operator(.)ident(new) comment(# or @card_deck = []) - reserved(def) method(shuffle) - reserved(end) - reserved(end) -reserved(end) - - -comment(# @@PLEAC@@_12.1) -comment(# a module exports all of its functions) -reserved(module) class(Your_Module) - reserved(def) pre_constant(self)operator(.)ident(function) - comment(# this would be called as Your_Module.function) - reserved(end) - - reserved(def) constant(Your_Module)operator(.)ident(another) - comment(# this is the same as above, but more specific) - reserved(end) -reserved(end) - -comment(# @@PLEAC@@_12.2) -reserved(begin) - ident(require) string -reserved(rescue) constant(LoadError) - ident(puts) stringdelimiter(")> comment(# $! contains the last error string) -reserved(end) - -comment(# @@PLEAC@@_12.4) -comment(# module variables are private unless access functions are defined) -reserved(module) class(Alpha) - instance_variable(@aa) operator(=) integer(10) - instance_variable(@bb) operator(=) integer(11) - - reserved(def) pre_constant(self)operator(.)ident(put_aa) - ident(puts) instance_variable(@aa) - reserved(end) - - reserved(def) pre_constant(self)operator(.)ident(bb)operator(=)operator(()ident(val)operator(\)) - instance_variable(@bb) operator(=) ident(val) - reserved(end) -reserved(end) - -constant(Alpha)operator(.)ident(bb) operator(=) integer(12) -comment(# Alpha.aa = 10 # error, no aa=method) - - -comment(# @@PLEAC@@_12.5) -comment(# caller provides a backtrace of the call stack) -reserved(module) class(MyModule) - reserved(def) method(find_caller) - ident(caller) - reserved(end) - - reserved(def) method(find_caller2)operator(()ident(i)operator(\)) - ident(caller)operator(()ident(i)operator(\)) comment(# an argument limits the size of the stack returned) - reserved(end) -reserved(end) - - -comment(# @@PLEAC@@_12.6) -reserved(BEGIN) operator({) - global_variable($logfile) operator(=) string reserved(unless) reserved(defined?) global_variable($logfile) - global_variable($LF) operator(=) constant(File)operator(.)ident(open)operator(()global_variable($logfile)operator(,) stringoperator(\)) -operator(}) - -reserved(module) class(Logger) - reserved(def) pre_constant(self)operator(.)ident(logmsg)operator(()ident(msg)operator(\)) - global_variable($LF)operator(.)ident(puts) ident(msg) - reserved(end) - - ident(logmsg)operator(()stringoperator(\)) -reserved(end) - -reserved(END) operator({) - constant(Logger)operator(::)ident(logmsg)operator(()stringoperator(\)) - global_variable($LF)operator(.)ident(close) -operator(}) - - -comment(# @@PLEAC@@_12.7) -comment(#-----------------------------) -comment(# results may be different on your system) -comment(# % ruby -e "$LOAD_PATH.each_index { |i| printf("%d %s\\n", i, $LOAD_PATH[i] }) -comment(#0 /usr/local/lib/site_ruby/1.6) -comment(#1 /usr/local/lib/site_ruby/1.6/i386-linux) -comment(#2 /usr/local/lib/site_ruby/) -comment(#3 /usr/lib/ruby/1.6) -comment(#4 /usr/lib/ruby/1.6/i136-linux) -comment(#5 .) -comment(#-----------------------------) -comment(# syntax for sh, bash, ksh, or zsh) -comment(#$ export RUBYLIB=$HOME/rubylib) - -comment(# syntax for csh or tcsh) -comment(# % setenv RUBYLIB ~/rubylib) -comment(#-----------------------------) -global_variable($LOAD_PATH)operator(.)ident(unshift) stringoperator(;) - - -comment(# @@PLEAC@@_12.8) -comment(# equivalents in ruby are mkmf, SWIG, or Ruby/DL depending on usage) - - -comment(# @@PLEAC@@_12.9) -comment(# no equivalent in ruby) - - -comment(# @@PLEAC@@_12.10) -comment(# no equivalent in ruby) - - -comment(# @@PLEAC@@_12.11) -reserved(module) class(FineTime) - reserved(def) pre_constant(self)operator(.)ident(time) - comment(# to be defined later) - reserved(end) -reserved(end) - - -reserved(module) class(FineTime) - reserved(def) pre_constant(self)operator(.)ident(time) - string - reserved(end) -reserved(end) - -ident(puts) constant(FineTime)operator(.)ident(time) comment(#=> "its a fine time") - - -comment(# @@PLEAC@@_12.12) -reserved(def) method(even_only)operator(()ident(n)operator(\)) - ident(raise) stringcontent( is not even)delimiter(")> reserved(if) operator(()ident(n) operator(&) integer(1)operator(\)) operator(!=) integer(0) comment(# one way to test) - comment(# ...) -reserved(end) -reserved(def) method(even_only)operator(()ident(n)operator(\)) - global_variable($stderr)operator(.)ident(puts) stringcontent( is not even)delimiter(")> reserved(if) operator(()ident(n) operator(&) integer(1)operator(\)) operator(!=) integer(0) - comment(# ...) -reserved(end) - - -comment(# @@PLEAC@@_12.17) -comment(# The library archive for ruby is called Ruby Application archive,) -comment(# or shorter RAA, and can be found at http://raa.ruby-lang.org.) -comment(# A typical library is installed like this:) -comment(# % gunzip some-module-4.54.tar.gz) -comment(# % tar xf some-module-4.54.tar) -comment(# % cd some-module-4.54.tar) -comment(# % ruby install.rb config) -comment(# % ruby install.rb setup) -comment(# get superuser previleges here if needed for next step) -comment(# % ruby install.rb install) - -comment(# Some modules use a different process,) -comment(# you should find details in the documentation) -comment(# Here is an example of such a different process) -comment(# % ruby extconf.rb) -comment(# % make) -comment(# % make install) - -comment(# If you want the module installed in your own directory:) -comment(# For ruby version specific libraries) -comment(# % ruby install.rb config --site-ruby=~/lib) -comment(# For version independent libraries) -comment(# % ruby install.rb config --site-ruby-common=~/lib) - -comment(# Information about possible options for config) -comment(# % ruby install.rb --help) - -comment(# If you have your own complete distribution) -comment(# % ruby install.rb --prefix=path=~/ruby-private) - - -comment(# @@PLEAC@@_13.0) -comment(# Classes and objects in Ruby are rather straigthforward) -reserved(class) class(Person) - comment(# Class variables (also called static attributes\) are prefixed by @@) - class_variable(@@person_counter)operator(=)integer(0) - - comment(# object constructor) - reserved(def) method(initialize)operator(()ident(age)operator(,) ident(name)operator(,) ident(alive) operator(=) pre_constant(true)operator(\)) comment(# Default arg like in C++) - instance_variable(@age)operator(,) instance_variable(@name)operator(,) instance_variable(@alive) operator(=) ident(age)operator(,) ident(name)operator(,) ident(alive) comment(# Object attributes are prefixed by '@') - class_variable(@@person_counter) operator(+=) integer(1) - comment(# There is no '++' operator in Ruby. The '++'/'--' operators are in fact) - comment(# hidden assignments which affect variables, not objects. You cannot accomplish) - comment(# assignment via method. Since everything in Ruby is object, '++' and '--') - comment(# contradict Ruby OO ideology. Instead '-=' and '+=' are used.) - reserved(end) - - ident(attr_accessor) symbol(:name)operator(,) symbol(:age) comment(# This creates setter and getter methods for @name) - comment(# and @age. See 13.3 for detailes.) - - comment(# methods modifying the receiver object usually have the '!' suffix) - reserved(def) method(die!) - instance_variable(@alive) operator(=) pre_constant(false) - ident(puts) stringcontent( has died at the age of )inlinecontent(.)delimiter(")> - instance_variable(@alive) - reserved(end) - - reserved(def) method(kill)operator(()ident(anotherPerson)operator(\)) - ident(print) instance_variable(@name)operator(,) stringoperator(,) ident(anotherPerson)operator(.)ident(name)operator(,) string - ident(anotherPerson)operator(.)ident(die!) - reserved(end) - - comment(# methods used as queries) - comment(# usually have the '?' suffix) - reserved(def) method(alive?) - instance_variable(@alive) operator(&&) pre_constant(true) - reserved(end) - - reserved(def) method(year_of_birth) - constant(Time)operator(.)ident(now)operator(.)ident(year) operator(-) instance_variable(@age) - reserved(end) - - comment(# Class method (also called static method\)) - reserved(def) constant(Person)operator(.)ident(number_of_people) - class_variable(@@person_counter) - reserved(end) -reserved(end) - -comment(# Using the class:) -comment(# Create objects of class Person) -ident(lecter) operator(=) constant(Person)operator(.)ident(new)operator(()integer(47)operator(,) stringoperator(\)) -ident(starling) operator(=) constant(Person)operator(.)ident(new)operator(()integer(29)operator(,) stringoperator(,) pre_constant(true)operator(\)) -ident(pazzi) operator(=) constant(Person)operator(.)ident(new)operator(()integer(40)operator(,) stringoperator(,) pre_constant(true)operator(\)) - -comment(# Calling a class method) -ident(print) stringoperator(,) constant(Person)operator(.)ident(number_of_people)operator(,) string - -ident(print) ident(pazzi)operator(.)ident(name)operator(,) stringoperator(,) operator(()ident(pazzi)operator(.)ident(alive?)operator(\)) operator(?) string operator(:) stringoperator(,) string -ident(lecter)operator(.)ident(kill)operator(()ident(pazzi)operator(\)) -ident(print) ident(pazzi)operator(.)ident(name)operator(,) stringoperator(,) operator(()ident(pazzi)operator(.)ident(alive?)operator(\)) operator(?) string operator(:) stringoperator(,) string - -ident(print) ident(starling)operator(.)ident(name) operator(,) stringoperator(,) ident(starling)operator(.)ident(year_of_birth)operator(,) string - - -comment(# @@PLEAC@@_13.1) -comment(# If you don't need any initialisation in the constructor,) -comment(# you don't need to write a constructor.) -reserved(class) class(MyClass) -reserved(end) - -reserved(class) class(MyClass) - reserved(def) method(initialize) - instance_variable(@start) operator(=) constant(Time)operator(.)ident(new) - instance_variable(@age) operator(=) integer(0) - reserved(end) -reserved(end) - -reserved(class) class(MyClass) - reserved(def) method(initialize)operator(()ident(inithash)operator(\)) - instance_variable(@start) operator(=) constant(Time)operator(.)ident(new) - instance_variable(@age) operator(=) integer(0) - reserved(for) ident(key)operator(,) ident(value) reserved(in) ident(inithash) - ident(instance_variable_set)operator(()stringdelimiter(")>operator(,) ident(value)operator(\)) - reserved(end) - reserved(end) -reserved(end) - -comment(# @@PLEAC@@_13.2) -comment(# Objects are destroyed by the garbage collector.) -comment(# The time of destroying is not predictable.) -comment(# The ruby garbage collector can handle circular references,) -comment(# so there is no need to write destructor for that.) - -comment(# There is no direct support for destructor.) -comment(# You can call a custom function, or more specific a proc object, when the) -comment(# garbage collector is about to destruct the object, but it is unpredictable) -comment(# when this occurs.) -comment(# Also if such a finalizer object has a reference to the orignal object,) -comment(# this may prevent the original object to get garbage collected.) -comment(# Because of this problem the finalize method below is) -comment(# a class method and not a instance method.) -comment(# So if you need to free resources for an object, like) -comment(# closing a socket or kill a spawned subprocess,) -comment(# you should do it explicitly.) - -reserved(class) class(MyClass) - reserved(def) method(initialize) - constant(ObjectSpace)operator(.)ident(define_finalizer)operator(()pre_constant(self)operator(,) - pre_constant(self)operator(.)ident(class)operator(.)ident(method)operator(()symbol(:finalize)operator(\))operator(.)ident(to_proc)operator(\)) - reserved(end) - reserved(def) constant(MyClass)operator(.)ident(finalize)operator(()ident(id)operator(\)) - ident(puts) stringcontent( dying at )inlinedelimiter(")> - reserved(end) -reserved(end) - -comment(# test code) -integer(3)operator(.)ident(times) operator({) - constant(MyClass)operator(.)ident(new) -operator(}) -constant(ObjectSpace)operator(.)ident(garbage_collect) - - -comment(# @@PLEAC@@_13.3) -comment(# You can write getter and setter methods in a natural way:) -reserved(class) class(Person) - reserved(def) method(name) - instance_variable(@name) - reserved(end) - reserved(def) method(name=)operator(()ident(name)operator(\)) - instance_variable(@name) operator(=) ident(name) - reserved(end) -reserved(end) - -comment(# But there is a better and shorter way) -reserved(class) class(Person) - ident(attr_reader) symbol(:age) - ident(attr_writer) symbol(:name) - comment(# attr_reader and attr_writer are actually methods in class Class) - comment(# which set getter and setter methods for you.) -reserved(end) - -comment(# There is also attr_accessor to create both setters and getters) -reserved(class) class(Person) - ident(attr_accessor) symbol(:age)operator(,) symbol(:name) -reserved(end) - - -comment(# @@PLEAC@@_13.4) -reserved(class) class(Person) - comment(# Class variables (also called static attributes\) are prefixed by @@) - class_variable(@@person_counter) operator(=) integer(0) - - reserved(def) constant(Person)operator(.)ident(population) - class_variable(@@person_counter) - reserved(end) - reserved(def) method(initialize) - class_variable(@@person_counter) operator(+=) integer(1) - constant(ObjectSpace)operator(.)ident(define_finalizer)operator(()pre_constant(self)operator(,) - pre_constant(self)operator(.)ident(class)operator(.)ident(method)operator(()symbol(:finalize)operator(\))operator(.)ident(to_proc)operator(\)) - reserved(end) - reserved(def) constant(Person)operator(.)ident(finalize)operator(()ident(id)operator(\)) - class_variable(@@person_counter) operator(-=) integer(1) - reserved(end) -reserved(end) -ident(people) operator(=) operator([)operator(]) -integer(10)operator(.)ident(times) operator({) - ident(people)operator(.)ident(push)operator(()constant(Person)operator(.)ident(new)operator(\)) -operator(}) -ident(printf)operator(()stringoperator(,) constant(Person)operator(.)ident(population)operator(\)) - - -constant(FixedArray)operator(.)ident(class_max_bounds) operator(=) integer(100) -ident(alpha) operator(=) constant(FixedArray)operator(.)ident(new) -ident(puts) stringdelimiter(")> - -ident(beta) operator(=) constant(FixedArray)operator(.)ident(new) -ident(beta)operator(.)ident(max_bounds) operator(=) integer(50) comment(# calls the instance method) -ident(beta)operator(.)ident(class)operator(.)ident(class_max_bounds) operator(=) integer(50) comment(# alternative, calls the class method) -ident(puts) stringdelimiter(")> - -reserved(class) class(FixedArray) - class_variable(@@bounds) operator(=) integer(7) - - reserved(def) method(max_bounds) - class_variable(@@max_bounds) - reserved(end) - comment(# instance method, which sets the class variable) - reserved(def) method(max_bounds=)operator(()ident(value)operator(\)) - class_variable(@@max_bounds) operator(=) ident(value) - reserved(end) - comment(# class method. This can only be called on a class,) - comment(# but not on the instances) - reserved(def) constant(FixedArray)operator(.)ident(class_max_bounds)operator(=)operator(()ident(value)operator(\)) - class_variable(@@max_bounds) operator(=) ident(value) - reserved(end) -reserved(end) - - -comment(# @@PLEAC@@_13.5) -constant(PersonStruct) operator(=) constant(Struct)operator(.)ident(new)operator(()stringoperator(,) symbol(:name)operator(,) symbol(:age)operator(,) symbol(:peers)operator(\)) -comment(# creates a class "Person::Struct", which is accessiable with the) -comment(# constant "PersonStruct") -ident(p) operator(=) constant(PersonStruct)operator(.)ident(new) -ident(p) operator(=) constant(Struct)operator(::)constant(Person)operator(.)ident(new) comment(# alternative using the classname) -ident(p)operator(.)ident(name) operator(=) string -ident(p)operator(.)ident(age) operator(=) integer(13) -ident(p)operator(.)ident(peers) operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(]) -ident(p)operator([)symbol(:peers)operator(]) operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(]) comment(# alternative access using symbol) -ident(p)operator([)stringoperator(]) operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(]) comment(# alternative access using name of field) -ident(p)operator([)integer(2)operator(]) operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(]) comment(# alternative access using index of field) -ident(puts) stringcontent(, )inlinecontent('s first friend is )inlinedelimiter(")> - -comment(# The fields of a struct have no special type, like other ruby variables) -comment(# you can put any objects in. Therefore the discussions how to specify) -comment(# the types of the fields do not apply to ruby.) - -constant(FamilyStruct) operator(=) constant(Struct)operator(.)ident(new)operator(()stringoperator(,) symbol(:head)operator(,) symbol(:address)operator(,) symbol(:members)operator(\)) -ident(folks) operator(=) constant(FamilyStruct)operator(.)ident(new) -ident(folks)operator(.)ident(head) operator(=) constant(PersonStruct)operator(.)ident(new) -ident(dad) operator(=) ident(folks)operator(.)ident(head) -ident(dad)operator(.)ident(name) operator(=) string -ident(dad)operator(.)ident(age) operator(=) integer(34) - -comment(# supply of own accessor method for the struct for error checking) -reserved(class) class(PersonStruct) - reserved(def) method(age=)operator(()ident(value)operator(\)) - reserved(if) operator(!)ident(value)operator(.)ident(kind_of?)operator(()constant(Integer)operator(\)) - ident(raise)operator(()constant(ArgumentError)operator(,) stringcontent( isn't an Integer)delimiter(")>operator(\)) - reserved(elsif) ident(value) operator(>) integer(150) - ident(raise)operator(()constant(ArgumentError)operator(,) stringcontent( is unreasonable)delimiter(")>operator(\)) - reserved(end) - instance_variable(@age) operator(=) ident(value) - reserved(end) -reserved(end) - - -comment(# @@PLEAC@@_13.6) -comment(# The ruby Object class defines a dup and a clone method.) -comment(# The dup method is recommended for prototype object creation.) -comment(# The default implementation makes a shallow copy,) -comment(# but each class can override it, for example to make a deep copy.) - -comment(# If you want to call 'new' directly on the instances,) -comment(# you can create a instance method "new", which returns a new duplicate.) -comment(# This method is distinct from the class method new.) -comment(#) -reserved(class) class(A) - reserved(def) method(new) - ident(dup) - reserved(end) -reserved(end) - -ident(ob1) operator(=) constant(A)operator(.)ident(new) -comment(# later on) -ident(ob2) operator(=) ident(ob1)operator(.)ident(new) - - -comment(# @@PLEAC@@_13.7) -ident(methname) operator(=) string -ident(obj)operator(.)ident(send)operator(()ident(methname)operator(,) integer(10)operator(\)) comment(# calls obj.flicker(10\)) - -comment(# call three methods on the object, by name) -operator([)stringoperator(,) stringoperator(,) stringoperator(])operator(.)ident(each) reserved(do) operator(|)ident(method_string)operator(|) - ident(obj)operator(.)ident(send)operator(()ident(method_string)operator(\)) -reserved(end) - -comment(# Another way is to create a Method object) -ident(method_obj) operator(=) ident(obj)operator(.)ident(method)operator(()stringoperator(\)) -comment(# And then call it) -ident(method_obj)operator(.)ident(call)operator(()integer(10)operator(\)) - - -comment(# @@PLEAC@@_13.8) -comment(# All classes in Ruby inherit from class Object) -comment(# and thus all objects share methods defined in this class) - -comment(# the class of the object) -ident(puts) ident(any_object)operator(.)ident(type) - -comment(# Ruby classes are actually objects of class Class and they) -comment(# respond to methods defined in Object class as well) - -comment(# the superclass of this class) -ident(puts) ident(any_object)operator(.)ident(class)operator(.)ident(superclass) - -comment(# ask an object whether it is an instance of particular class) -ident(n) operator(=) float(4.7) -ident(puts) ident(n)operator(.)ident(instance_of?)operator(()constant(Float)operator(\)) comment(# true) -ident(puts) ident(n)operator(.)ident(instance_of?)operator(()constant(Numeric)operator(\)) comment(# false) - -comment(# ask an object whether it is an instance of class, one of the) -comment(# superclasses of the object, or modules included in it) -ident(puts) ident(n)operator(.)ident(kind_of?)operator(()constant(Float)operator(\)) comment(# true (the class\)) -ident(puts) ident(n)operator(.)ident(kind_of?)operator(()constant(Numeric)operator(\)) comment(# true (an ancestor class\)) -ident(puts) ident(n)operator(.)ident(kind_of?)operator(()constant(Comparable)operator(\)) comment(# true (a mixin module\)) -ident(puts) ident(n)operator(.)ident(kind_of?)operator(()constant(String)operator(\)) comment(# false) - -comment(# ask an object whether it can respond to a particular method) -ident(puts) ident(n)operator(.)ident(respond_to?)operator(()stringoperator(\)) comment(# true) -ident(puts) ident(n)operator(.)ident(respond_to?)operator(()stringoperator(\)) comment(# false) - -comment(# all methods an object can respond to) -stringoperator(.)ident(methods)operator(.)ident(each) operator({) operator(|)ident(m)operator(|) ident(puts) ident(m) operator(}) - - -comment(# @@PLEAC@@_13.9) -comment(# Actually any class in Ruby is inheritable) -reserved(class) class(Person) - ident(attr_accessor) symbol(:age)operator(,) symbol(:name) - reserved(def) method(initialize) - instance_variable(@name) - instance_variable(@age) - reserved(end) -reserved(end) -comment(#-----------------------------) -ident(dude) operator(=) constant(Person)operator(.)ident(new) -ident(dude)operator(.)ident(name) operator(=) string -ident(dude)operator(.)ident(age) operator(=) integer(23) -ident(printf) stringoperator(,) ident(dude)operator(.)ident(name)operator(,) ident(dude)operator(.)ident(age) -comment(#-----------------------------) -comment(# Inheriting from Person) -reserved(class) class(Employee) operator(<) constant(Person) - ident(attr_accessor) symbol(:salary) -reserved(end) -comment(#-----------------------------) -ident(empl) operator(=) constant(Employee)operator(.)ident(new) -ident(empl)operator(.)ident(name) operator(=) string -ident(empl)operator(.)ident(age) operator(=) integer(23) -ident(empl)operator(.)ident(salary) operator(=) integer(200) -ident(printf) stringoperator(,) ident(empl)operator(.)ident(name)operator(,) ident(empl)operator(.)ident(age)operator(,) ident(empl)operator(.)ident(salary) -comment(#-----------------------------) -comment(# Any built-in class can be inherited the same way) -reserved(class) class(WeirdString) operator(<) constant(String) - reserved(def) method(initialize)operator(()ident(obj)operator(\)) - reserved(super) ident(obj) - reserved(end) - reserved(def) method(+)operator(()ident(anotherObj)operator(\)) comment(# + method in this class is overridden) - comment(# to return the sum of string lengths) - pre_constant(self)operator(.)ident(length) operator(+) ident(anotherObj)operator(.)ident(length) comment(# 'self' can be omitted) - reserved(end) -reserved(end) -comment(#-----------------------------) -ident(a) operator(=) constant(WeirdString)operator(.)ident(new)operator(()stringoperator(\)) -ident(b) operator(=) constant(WeirdString)operator(.)ident(new)operator(()stringoperator(\)) - -ident(puts) ident(a) operator(+) ident(b) comment(# the overridden +) -comment(#=> 8) -ident(puts) ident(a)operator(.)ident(length) comment(# method from the superclass, String) -comment(#=> 5) - - -comment(# @@PLEAC@@_13.11) -comment(# In ruby you can override the method_missing method) -comment(# to have a solution similar to perls AUTOLOAD.) -reserved(class) class(Person) - - reserved(def) method(initialize) - instance_variable(@ok_fields) operator(=) string - reserved(end) - - reserved(def) method(valid_attribute?)operator(()ident(name)operator(\)) - instance_variable(@ok_fields)operator(.)ident(include?)operator(()ident(name)operator(\)) - reserved(end) - - reserved(def) method(method_missing)operator(()ident(namesymbol)operator(,) operator(*)ident(params)operator(\)) - ident(name) operator(=) ident(namesymbol)operator(.)ident(to_s) - reserved(return) reserved(if) ident(name) operator(=)operator(~) regexp - reserved(if) ident(name)operator(.)ident(to_s)operator([)operator(-)integer(1)operator(]) operator(==) operator(()stringoperator([)integer(0)operator(])operator(\)) comment(# we have a setter) - ident(isSetter) operator(=) pre_constant(true) - ident(name)operator(.)ident(sub!)operator(()regexpoperator(,) stringoperator(\)) - reserved(end) - reserved(if) ident(valid_attribute?)operator(()ident(name)operator(\)) - reserved(if) ident(isSetter) - ident(instance_variable_set)operator(()stringdelimiter(")>operator(,) operator(*)ident(params)operator(\)) - reserved(else) - ident(instance_variable_get)operator(()stringdelimiter(")>operator(,) operator(*)ident(params)operator(\)) - reserved(end) - reserved(else) - comment(# if no annestor is responsible,) - comment(# the Object class will throw a NoMethodError exception) - reserved(super)operator(()ident(namesymbol)operator(,) operator(*)ident(params)operator(\)) - reserved(end) - reserved(end) - - reserved(def) method(new) - ident(kid) operator(=) constant(Person)operator(.)ident(new) - ident(kid)operator(.)ident(parent) operator(=) pre_constant(self) - ident(kid) - reserved(end) - -reserved(end) - -ident(dad) operator(=) constant(Person)operator(.)ident(new) -ident(dad)operator(.)ident(name) operator(=) string -ident(dad)operator(.)ident(age) operator(=) integer(23) -ident(kid) operator(=) ident(dad)operator(.)ident(new) -ident(kid)operator(.)ident(name) operator(=) string -ident(kid)operator(.)ident(age) operator(=) integer(2) -ident(puts) stringdelimiter(")> -ident(puts) ident(dad) -ident(puts) ident(kid) - -reserved(class) class(Employee) operator(<) constant(Person) - reserved(def) method(initialize) - reserved(super) - instance_variable(@ok_fields)operator(.)ident(push)operator(()stringoperator(,) stringoperator(\)) - reserved(end) - reserved(def) method(ok_fields) - instance_variable(@ok_fields) - reserved(end) -reserved(end) - - -comment(# @@PLEAC@@_13.13) -comment(# The ruby garbage collector pretends to cope with circular structures.) -comment(# You can test it with this code:) -reserved(class) class(RingNode) - ident(attr_accessor) symbol(:next) - ident(attr_accessor) symbol(:prev) - ident(attr_reader) symbol(:name) - - reserved(def) method(initialize)operator(()ident(aName)operator(\)) - instance_variable(@name) operator(=) ident(aName) - constant(ObjectSpace)operator(.)ident(define_finalizer)operator(()pre_constant(self)operator(,) - pre_constant(self)operator(.)ident(class)operator(.)ident(method)operator(()symbol(:finalize)operator(\))operator(.)ident(to_proc)operator(\)) - reserved(end) - - reserved(def) constant(RingNode)operator(.)ident(finalize)operator(()ident(id)operator(\)) - ident(puts) stringcontent( dying)delimiter(")> - reserved(end) - - reserved(def) constant(RingNode)operator(.)ident(show_all_objects) - constant(ObjectSpace)operator(.)ident(each_object) operator({)operator(|)ident(id)operator(|) - ident(puts) ident(id)operator(.)ident(name) reserved(if) ident(id)operator(.)ident(class) operator(==) constant(RingNode) - operator(}) - reserved(end) -reserved(end) - -reserved(def) method(create_test) - ident(a) operator(=) constant(RingNode)operator(.)ident(new)operator(()stringoperator(\)) - ident(b) operator(=) constant(RingNode)operator(.)ident(new)operator(()stringoperator(\)) - ident(c) operator(=) constant(RingNode)operator(.)ident(new)operator(()stringoperator(\)) - ident(a)operator(.)ident(next) operator(=) ident(b) - ident(b)operator(.)ident(next) operator(=) ident(c) - ident(c)operator(.)ident(next) operator(=) ident(a) - ident(a)operator(.)ident(prev) operator(=) ident(c) - ident(c)operator(.)ident(prev) operator(=) ident(b) - ident(b)operator(.)ident(prev) operator(=) ident(a) - - ident(a) operator(=) pre_constant(nil) - ident(b) operator(=) pre_constant(nil) - ident(c) operator(=) pre_constant(nil) -reserved(end) - -ident(create_test) -constant(RingNode)operator(.)ident(show_all_objects) -constant(ObjectSpace)operator(.)ident(garbage_collect) -ident(puts) string -constant(RingNode)operator(.)ident(show_all_objects) - - -comment(# @@PLEAC@@_13.14) -reserved(class) class(String) - reserved(def) method(<=>)operator(()ident(other)operator(\)) - pre_constant(self)operator(.)ident(casecmp) ident(other) - reserved(end) -reserved(end) - -comment(# There is no way to directly overload the '""' (stringify\)) -comment(# operator in Ruby. However, by convention, classes which) -comment(# can reasonably be converted to a String will define a) -comment(# 'to_s' method as in the TimeNumber class defined below.) -comment(# The 'puts' method will automatcally call an object's) -comment(# 'to_s' method as is demonstrated below.) -comment(# Furthermore, if a class defines a to_str method, an object of that) -comment(# class can be used most any place where the interpreter is looking) -comment(# for a String value.) - -comment(#---------------------------------------) -comment(# NOTE: Ruby has a builtin Time class which would usually be used) -comment(# to manipulate time objects, the following is supplied for) -comment(# educational purposes to demonstrate operator overloading.) -comment(#) -reserved(class) class(TimeNumber) - ident(attr_accessor) symbol(:hours)operator(,)symbol(:minutes)operator(,)symbol(:seconds) - reserved(def) method(initialize)operator(() ident(hours)operator(,) ident(minutes)operator(,) ident(seconds)operator(\)) - instance_variable(@hours) operator(=) ident(hours) - instance_variable(@minutes) operator(=) ident(minutes) - instance_variable(@seconds) operator(=) ident(seconds) - reserved(end) - - reserved(def) method(to_s) - reserved(return) ident(sprintf)operator(() stringoperator(,) instance_variable(@hours)operator(,) instance_variable(@minutes)operator(,) instance_variable(@seconds)operator(\)) - reserved(end) - - reserved(def) method(to_str) - ident(to_s) - reserved(end) - - reserved(def) method(+)operator(() ident(other)operator(\)) - ident(seconds) operator(=) instance_variable(@seconds) operator(+) ident(other)operator(.)ident(seconds) - ident(minutes) operator(=) instance_variable(@minutes) operator(+) ident(other)operator(.)ident(minutes) - ident(hours) operator(=) instance_variable(@hours) operator(+) ident(other)operator(.)ident(hours) - reserved(if) ident(seconds) operator(>)operator(=) integer(60) - ident(seconds) operator(%=) integer(60) - ident(minutes) operator(+=) integer(1) - reserved(end) - reserved(if) ident(minutes) operator(>)operator(=) integer(60) - ident(minutes) operator(%=) integer(60) - ident(hours) operator(+=) integer(1) - reserved(end) - reserved(return) constant(TimeNumber)operator(.)ident(new)operator(()ident(hours)operator(,) ident(minutes)operator(,) ident(seconds)operator(\)) - reserved(end) - - reserved(def) method(-)operator(()ident(other)operator(\)) - ident(raise) constant(NotImplementedError) - reserved(end) - - reserved(def) method(*)operator(()ident(other)operator(\)) - ident(raise) constant(NotImplementedError) - reserved(end) - - reserved(def) method(/)operator(() ident(other)operator(\)) - ident(raise) constant(NotImplementedError) - reserved(end) -reserved(end) - -ident(t1) operator(=) constant(TimeNumber)operator(.)ident(new)operator(()integer(0)operator(,) integer(58)operator(,) integer(59)operator(\)) -ident(sec) operator(=) constant(TimeNumber)operator(.)ident(new)operator(()integer(0)operator(,) integer(0)operator(,) integer(1)operator(\)) -ident(min) operator(=) constant(TimeNumber)operator(.)ident(new)operator(()integer(0)operator(,) integer(1)operator(,) integer(0)operator(\)) -ident(puts) ident(t1) operator(+) ident(sec) operator(+) ident(min) operator(+) ident(min) - -comment(#-----------------------------) -comment(# StrNum class example: Ruby's builtin String class already has the) -comment(# capabilities outlined in StrNum Perl example, however the '*' operator) -comment(# on Ruby's String class acts differently: It creates a string which) -comment(# is the original string repeated N times.) -comment(#) -comment(# Using Ruby's String class as is in this example:) -ident(x) operator(=) stringoperator(;) ident(y) operator(=) string -ident(z) operator(=) ident(x)operator(+)ident(y) -ident(r) operator(=) ident(z)operator(*)integer(3) comment(# r is "RedBlackRedBlackRedBlack") -ident(puts) stringcontent(, )inlinecontent(, )inlinecontent(, and )inlinedelimiter(")> -ident(print) stringcontent( is )delimiter(")>operator(,) ident(x) operator(<) ident(y) operator(?) string operator(:) stringoperator(,) stringchar(\\n)delimiter(")> -comment(# prints:) -comment(# values are Red, Black, RedBlack, and RedBlackRedBlackRedBlack) -comment(# Red is GE Black) - -comment(#-----------------------------) -reserved(class) class(FixNum) - constant(REGEX) operator(=) regexp - constant(DEFAULT_PLACES) operator(=) integer(0) - ident(attr_accessor) symbol(:value)operator(,) symbol(:places) - reserved(def) method(initialize)operator(()ident(value)operator(,) ident(places) operator(=) pre_constant(nil)operator(\)) - instance_variable(@value) operator(=) ident(value) - reserved(if) ident(places) - instance_variable(@places) operator(=) ident(places) - reserved(else) - ident(m) operator(=) constant(REGEX)operator(.)ident(match)operator(()ident(value)operator(.)ident(to_s)operator(\)) - reserved(if) ident(m) - instance_variable(@places) operator(=) ident(m)operator([)integer(0)operator(])operator(.)ident(length) operator(-) integer(1) - reserved(else) - instance_variable(@places) operator(=) constant(DEFAULT_PLACES) - reserved(end) - reserved(end) - reserved(end) - - reserved(def) method(+)operator(()ident(other)operator(\)) - constant(FixNum)operator(.)ident(new)operator(()instance_variable(@value) operator(+) ident(other)operator(.)ident(value)operator(,) ident(max)operator(()instance_variable(@places)operator(,) ident(other)operator(.)ident(places)operator(\))operator(\)) - reserved(end) - - reserved(def) method(*)operator(()ident(other)operator(\)) - constant(FixNum)operator(.)ident(new)operator(()instance_variable(@value) operator(*) ident(other)operator(.)ident(value)operator(,) ident(max)operator(()instance_variable(@places)operator(,) ident(other)operator(.)ident(places)operator(\))operator(\)) - reserved(end) - - reserved(def) method(/)operator(()ident(other)operator(\)) - ident(puts) stringdelimiter(")> - ident(result) operator(=) constant(FixNum)operator(.)ident(new)operator(()instance_variable(@value)operator(.)ident(to_f)operator(/)ident(other)operator(.)ident(value)operator(.)ident(to_f)operator(\)) - ident(result)operator(.)ident(places) operator(=) ident(max)operator(()ident(result)operator(.)ident(places)operator(,)ident(other)operator(.)ident(places)operator(\)) - ident(result) - reserved(end) - - reserved(def) method(to_s) - ident(sprintf)operator(()stringoperator(,) pre_constant(self)operator(.)ident(class)operator(.)ident(to_s) operator(,) instance_variable(@places)operator(,) instance_variable(@value)operator(\)) comment(#.) - reserved(end) - - reserved(def) method(to_str) - ident(to_s) - reserved(end) - - reserved(def) method(to_i) comment(#convert to int) - instance_variable(@value)operator(.)ident(to_i) - reserved(end) - - reserved(def) method(to_f) comment(#convert to float`) - instance_variable(@value)operator(.)ident(to_f) - reserved(end) - - ident(private) - reserved(def) method(max)operator(()ident(a)operator(,)ident(b)operator(\)) - ident(a) operator(>) ident(b) operator(?) ident(a) operator(:) ident(b) - reserved(end) -reserved(end) - -reserved(def) method(demo)operator(()operator(\)) - ident(x) operator(=) constant(FixNum)operator(.)ident(new)operator(()integer(40)operator(\)) - ident(y) operator(=) constant(FixNum)operator(.)ident(new)operator(()integer(12)operator(,) integer(0)operator(\)) - - ident(puts) stringcontent( and )inlinecontent( is )inlinedelimiter(")> - ident(puts) stringcontent( and )inlinecontent( is )inlinedelimiter(")> - - ident(z) operator(=) ident(x)operator(/)ident(y) - ident(puts) stringcontent( has )inlinecontent( places)delimiter(")> - reserved(unless) ident(z)operator(.)ident(places) - ident(z)operator(.)ident(places) operator(=) integer(2) - reserved(end) - - ident(puts) stringcontent( by )inlinecontent( is )inlinedelimiter(")> - ident(puts) stringdelimiter(")> -reserved(end) - -reserved(if) pre_constant(__FILE__) operator(==) global_variable($0) - ident(demo)operator(()operator(\)) -reserved(end) - - -comment(# @@PLEAC@@_14.1) -comment(# There are dbm, sdbm, gdbm modules) -comment(# and the bdb module for accessing the berkeley db) -comment(# sdbm seem to be available on the most systems,) -comment(# so we use it here) -comment(#) -ident(require) string -constant(SDBM)operator(.)ident(open)operator(()stringoperator(,) integer(0666)operator(\)) operator({) operator(|)ident(dbobj)operator(|) - comment(# raises exception if open error) - - comment(# the returned sdbm-dbobj has most of the methods of a hash) - ident(v) operator(=) ident(dbobj)operator([)stringoperator(]) - ident(dbobj)operator([)stringoperator(]) operator(=) string - reserved(if) ident(dbobj)operator(.)ident(has_key?)operator(()stringoperator(\)) - comment(# ...) - reserved(end) - ident(dbobj)operator(.)ident(delete)operator(()stringoperator(\)) -operator(}) -comment(# database is open only inside the block.) - -comment(# It is also possible to use a open .. close pair:) -ident(dbobj) operator(=) constant(SDBM)operator(.)ident(open)operator(()stringoperator(,) integer(0666)operator(\)) -comment(#.. do something with dbobj) -ident(dbobj)operator(.)ident(close) - -comment(#!/usr/bin/ruby -w) -comment(# userstats - generate statistics on who is logged in) -comment(# call with usernames as argument to display the totals) -comment(# for the given usernames, call with "ALL" to display all users) - -ident(require) string -ident(filename) operator(=) string -constant(SDBM)operator(.)ident(open)operator(()ident(filename)operator(,) integer(0666)operator(\)) operator({) operator(|)ident(dbobj)operator(|) - reserved(if) pre_constant(ARGV)operator(.)ident(length) operator(>) integer(0) - reserved(if) pre_constant(ARGV)operator([)integer(0)operator(]) operator(==) string - comment(# ARGV is constant, so we need the variable userlist) - ident(userlist) operator(=) ident(dbobj)operator(.)ident(keys)operator(()operator(\))operator(.)ident(sort)operator(()operator(\)) - reserved(else) - ident(userlist) operator(=) pre_constant(ARGV) - reserved(end) - ident(userlist)operator(.)ident(each) operator({) operator(|)ident(user)operator(|) - ident(print) stringchar(\\t)inlinechar(\\n)delimiter(")> - operator(}) - reserved(else) - ident(who) operator(=) shell - ident(who)operator(.)ident(split)operator(()stringoperator(\))operator(.)ident(each) operator({) operator(|)ident(line)operator(|) - ident(md) operator(=) regexpoperator(.)ident(match)operator(()ident(line)operator(\)) - ident(raise) stringdelimiter(")> reserved(unless) ident(md) - comment(# sdbm stores only strings, so "+=" doesn't work,) - comment(# we need to convert them expicitly back to integer.) - reserved(if) ident(dbobj)operator(.)ident(has_key?)operator(()ident(md)operator([)integer(0)operator(])operator(\)) - ident(dbobj)operator([)ident(md)operator([)integer(0)operator(])operator(]) operator(=) ident(dbobj)operator([)ident(md)operator([)integer(0)operator(])operator(])operator(.)ident(to_i) operator(+) integer(1) - reserved(else) - ident(dbobj)operator([)ident(md)operator([)integer(0)operator(])operator(]) operator(=) string - reserved(end) - operator(}) - reserved(end) -operator(}) - - -comment(# @@PLEAC@@_14.2) -comment(# using open and clear) -ident(dbobj) operator(=) constant(SDBM)operator(.)ident(open)operator(()stringoperator(,) integer(0666)operator(\)) -ident(dbobj)operator(.)ident(clear)operator(()operator(\)) -ident(dbobj)operator(.)ident(close)operator(()operator(\)) -comment(# deleting file and recreating it) -comment(# the filenames depend on the flavor of dbm you use,) -comment(# for example sdbm has two files named filename.pag and filename.dir,) -comment(# so you need to delete both files) -reserved(begin) - constant(File)operator(.)ident(delete)operator(()stringoperator(\)) - comment(# raises Exception if not exist) - ident(dbobj) operator(=) constant(SDBM)operator(.)ident(open)operator(()stringoperator(,) integer(0666)operator(\)) -reserved(rescue) - comment(# add error handling here) -reserved(end) - - -comment(# @@PLEAC@@_14.3) -comment(# sdbm2gdbm: converts sdbm database to a gdbm database) -ident(require) string -ident(require) string - -reserved(unless) pre_constant(ARGV)operator(.)ident(length) operator(==) integer(2) - ident(fail) string -reserved(end) -ident(infile) operator(=) pre_constant(ARGV)operator([)integer(0)operator(]) -ident(outfile) operator(=) pre_constant(ARGV)operator([)integer(1)operator(]) - -ident(sdb) operator(=) constant(SDBM)operator(.)ident(open)operator(()ident(infile)operator(\)) -ident(gdb) operator(=) constant(GDBM)operator(.)ident(open)operator(()ident(outfile)operator(,) integer(0666)operator(\)) -ident(sdb)operator(.)ident(each) operator({) operator(|)ident(key)operator(,) ident(val)operator(|) - ident(gdb)operator([)ident(key)operator(]) operator(=) ident(val) -operator(}) -ident(gdb)operator(.)ident(close) -ident(sdb)operator(.)ident(close) - - -comment(# @@PLEAC@@_14.4) -comment(#!/usr/bin/ruby -w) -comment(# dbmmerge: merges two dbm databases) -ident(require) string - -reserved(unless) pre_constant(ARGV)operator(.)ident(length) operator(==) integer(3) - ident(fail) string -reserved(end) -ident(infile1) operator(=) pre_constant(ARGV)operator([)integer(0)operator(]) -ident(infile2) operator(=) pre_constant(ARGV)operator([)integer(0)operator(]) -ident(outfile) operator(=) pre_constant(ARGV)operator([)integer(2)operator(]) - -ident(in1) operator(=) constant(SDBM)operator(.)ident(open)operator(()ident(infile1)operator(,) pre_constant(nil)operator(\)) -ident(in2) operator(=) constant(SDBM)operator(.)ident(open)operator(()ident(infile2)operator(,) pre_constant(nil)operator(\)) -ident(outdb) operator(=) constant(SDBM)operator(.)ident(open)operator(()ident(outfile)operator(,) integer(0666)operator(\)) - -operator([)ident(in1)operator(,) ident(in2)operator(])operator(.)ident(each) operator({) operator(|)ident(indb)operator(|) - ident(indb)operator(.)ident(each) operator({) operator(|)ident(key)operator(,) ident(val)operator(|) - reserved(if) ident(outdb)operator(.)ident(has_key?)operator(()ident(key)operator(\)) - comment(# decide which value to set.) - comment(# set outdb[key] if necessary) - reserved(else) - ident(outdb)operator([)ident(key)operator(]) operator(=) ident(val) - reserved(end) - operator(}) -operator(}) -ident(in1)operator(.)ident(close) -ident(in2)operator(.)ident(close) -ident(outdb)operator(.)ident(close) - - -comment(# @@PLEAC@@_14.7) -comment(# we write a tie method that extends the Array class.) -comment(# It reads the file into the memory, executes the code block) -comment(# in which you can manipulate the array as needed, and writes) -comment(# the array back to the file after the end of the block execution) -reserved(class) class(Array) - reserved(def) method(tie)operator(()ident(filename)operator(,) ident(flags)operator(\)) - constant(File)operator(.)ident(open)operator(()ident(filename)operator(,) ident(flags)operator(\)) operator({) operator(|)ident(f)operator(|) - ident(f)operator(.)ident(each_line) operator({) operator(|)ident(line)operator(|) - pre_constant(self)operator(.)ident(push)operator(()ident(line)operator(.)ident(chomp)operator(\)) - operator(}) - reserved(yield) - ident(f)operator(.)ident(rewind) - ident(each) operator({) operator(|)ident(line)operator(|) - reserved(if) ident(line) - ident(f)operator(.)ident(puts)operator(()ident(line)operator(\)) - reserved(else) - ident(f)operator(.)ident(puts) string - reserved(end) - operator(}) - operator(}) - reserved(end) -reserved(end) - -ident(array) operator(=) constant(Array)operator(.)ident(new) -ident(array)operator(.)ident(tie)operator(()stringoperator(,) constant(File)operator(::)constant(RDWR)operator(|)constant(File)operator(::)constant(CREAT)operator(\)) operator({) - ident(array)operator([)integer(4)operator(]) operator(=) string -operator(}) - -comment(# The tied array can be manipulated like a normal array,) -comment(# so there is no need for a special API, and the recno_demo program) -comment(# to demonstrate is API is useless) - - -comment(# tied array demo: show how to use array with a tied file) -ident(filename) operator(=) string -ident(lines) operator(=) constant(Array)operator(.)ident(new) -constant(File)operator(.)ident(unlink)operator(()ident(filename)operator(\)) reserved(if) constant(File)operator(.)ident(exists?)operator(()ident(filename)operator(\)) -ident(lines)operator(.)ident(tie)operator(()ident(filename)operator(,) constant(File)operator(::)constant(RDWR) operator(|) constant(File)operator(::)constant(CREAT)operator(\)) operator({) - comment(# first create a textfile to play with) - ident(lines)operator([)integer(0)operator(]) operator(=) string - ident(lines)operator([)integer(1)operator(]) operator(=) string - ident(lines)operator([)integer(2)operator(]) operator(=) string - ident(lines)operator([)integer(3)operator(]) operator(=) string - ident(lines)operator([)integer(4)operator(]) operator(=) string - - comment(# print the records in order.) - comment(# Opposed to perl, the tied array behaves exactly as a normal array) - ident(puts) string - reserved(for) ident(i) reserved(in) integer(0)operator(..)operator(()ident(lines)operator(.)ident(length)operator(-)integer(1)operator(\)) - ident(puts) stringcontent(: )inlinedelimiter(")> - reserved(end) - - comment(#use push and pop) - ident(a) operator(=) ident(lines)operator(.)ident(pop) - ident(lines)operator(.)ident(push)operator(()stringoperator(\)) - ident(puts)operator(()stringcontent(])delimiter(")>operator(\)) - - comment(#use shift and unshift) - ident(a) operator(=) ident(lines)operator(.)ident(shift) - ident(lines)operator(.)ident(unshift)operator(()stringoperator(\)) - ident(puts)operator(()stringcontent(])delimiter(")>operator(\)) - - comment(# add record after record 2) - ident(i) operator(=) integer(2) - ident(lines)operator(.)ident(insert)operator(()ident(i) operator(+) integer(1)operator(,) stringoperator(\)) - - comment(# add record before record one) - ident(i) operator(=) integer(1) - ident(lines)operator(.)ident(insert)operator(()ident(i)operator(,) stringoperator(\)) - - comment(# delete record 3) - ident(lines)operator(.)ident(delete_at)operator(()integer(3)operator(\)) - - comment(#now print the records in reverse order) - ident(puts) string - operator(()ident(lines)operator(.)ident(length) operator(-) integer(1)operator(\))operator(.)ident(downto)operator(()integer(0)operator(\))operator({) operator(|)ident(i)operator(|) - ident(puts) stringcontent(: )inlinedelimiter(")> - operator(}) - -operator(}) - - -comment(# @@PLEAC@@_14.8) -comment(# example to store complex data in a database) -comment(# uses marshall from the standard library) -ident(require) string -ident(db) operator(=) constant(SDBM)operator(.)ident(open)operator(()stringoperator(,) integer(0666)operator(\)) - -comment(# convert the Objects into strings and back by using the Marshal module.) -comment(# Most normal objects can be converted out of the box,) -comment(# but not special things like procedure objects,) -comment(# IO instance variables, singleton objects) - -ident(db)operator([)stringoperator(]) operator(=) constant(Marshal)operator(.)ident(dump)operator(()operator([)stringoperator(,) stringoperator(])operator(\)) -ident(db)operator([)stringoperator(]) operator(=) constant(Marshal)operator(.)ident(dump)operator(()operator([)stringoperator(,) -stringoperator(])operator(\)) - -ident(name1) operator(=) string -ident(name2) operator(=) string - -ident(tom1) operator(=) constant(Marshal)operator(.)ident(load)operator(()ident(db)operator([)ident(name1)operator(])operator(\)) -ident(tom2) operator(=) constant(Marshal)operator(.)ident(load)operator(()ident(db)operator([)ident(name2)operator(])operator(\)) - -ident(puts) stringcontent( )inlinedelimiter(")> - -reserved(if) ident(tom1)operator([)integer(0)operator(]) operator(==) ident(tom2)operator([)integer(0)operator(]) operator(&&) ident(tom1)operator([)integer(1)operator(]) operator(==) ident(tom2)operator([)integer(1)operator(]) - ident(puts) string -reserved(else) - ident(puts) string -reserved(end) - -comment(# To change parts of an entry, get the whole entry, change the parts,) -comment(# and save the whole entry back) -ident(entry) operator(=) constant(Marshal)operator(.)ident(load)operator(()ident(db)operator([)stringoperator(])operator(\)) -ident(entry)operator([)integer(0)operator(]) operator(=) string -ident(db)operator([)stringoperator(]) operator(=) constant(Marshal)operator(.)ident(dump)operator(()ident(entry)operator(\)) -ident(db)operator(.)ident(close) - - -comment(# @@PLEAC@@_14.9) -comment(# example to make data persistent) -comment(# uses Marshal from the standard lib) -comment(# Stores the data in a simple file,) -comment(# see 14.8 on how to store it in a dbm file) - -comment(# The BEGIN block is executed before the rest of the script) -comment(# we use global variables here because local variables) -comment(# will go out of scope and are not accessible from the main script) - -reserved(BEGIN) operator({) - global_variable($persistent_store) operator(=) string - reserved(begin) - constant(File)operator(.)ident(open)operator(()global_variable($persistent_store)operator(\)) reserved(do) operator(|)ident(f)operator(|) - global_variable($stringvariable1) operator(=) constant(Marshal)operator(.)ident(load)operator(()ident(f)operator(\)) - global_variable($arrayvariable2) operator(=) constant(Marshal)operator(.)ident(load)operator(()ident(f)operator(\)) - reserved(end) - reserved(rescue) - ident(puts) stringdelimiter(")> - comment(# Initialisation if this script runs the first time) - global_variable($stringvariable1) operator(=) string - global_variable($arrayvariable2) operator(=) operator([)operator(]) - reserved(end) -operator(}) - -reserved(END) operator({) - constant(File)operator(.)ident(open)operator(()global_variable($persistent_store)operator(,) stringoperator(\)) reserved(do) operator(|)ident(f)operator(|) - constant(Marshal)operator(.)ident(dump)operator(()global_variable($stringvariable1)operator(,) ident(f)operator(\)) - constant(Marshal)operator(.)ident(dump)operator(()global_variable($arrayvariable2)operator(,) ident(f)operator(\)) - reserved(end) -operator(}) - -comment(# simple test program) -ident(puts) global_variable($stringvariable1) -ident(puts) global_variable($arrayvariable2) -global_variable($stringvariable1) operator(=) string -global_variable($arrayvariable2)operator(.)ident(push)operator(()integer(5)operator(\)) -ident(puts) global_variable($stringvariable1) -ident(puts) global_variable($arrayvariable2) - - -comment(# @@PLEAC@@_14.10) -comment(#!/usr/bin/ruby -w) -comment(# Ruby has a dbi module with an architecture similar) -comment(# to the Perl dbi module: the dbi module provides an unified) -comment(# interface and uses specialized drivers for each dbms vendor) -comment(#) -reserved(begin) - constant(DBI)operator(.)ident(connect)operator(()stringoperator(,) stringoperator(,) stringoperator(\)) operator({) - operator(|)ident(dbh)operator(|) - - ident(dbh)operator(.)ident(do)operator(()constant(SQL1)operator(\)) - - ident(dbh)operator(.)ident(prepare)operator(()constant(SQL2)operator(\))operator({) operator(|)ident(sth)operator(|) - ident(sth)operator(.)ident(execute) - ident(sth)operator(.)ident(fetch) operator({)operator(|)ident(row)operator(|) - comment(# ...) - operator(}) - operator(}) comment(# end of block finishes the statement handle) - operator(}) comment(# end of block closes the database connection) -reserved(rescue) constant(DBI)operator(::)constant(DatabaseError) operator(=)operator(>) ident(e) - ident(puts) string - ident(puts) stringdelimiter(")> - ident(puts) stringdelimiter(")> -reserved(end) - -comment(#!/usr/bin/ruby -w) -comment(# dbusers - example for mysql which creates a table,) -comment(# fills it with values, retrieves the values back,) -comment(# and finally destroys the table.) - -ident(require) string - -comment(# replacement for the User::pwnt module) -reserved(def) method(getpwent) - ident(result) operator(=) operator([)operator(]) - constant(File)operator(.)ident(open)operator(()stringoperator(\)) operator({)operator(|)ident(file)operator(|) - ident(file)operator(.)ident(each_line) operator({)operator(|)ident(line)operator(|) - reserved(next) reserved(if) ident(line)operator(.)ident(match)operator(()regexpoperator(\)) - ident(cols) operator(=) ident(line)operator(.)ident(split)operator(()stringoperator(\)) - ident(result)operator(.)ident(push)operator(()operator([)ident(cols)operator([)integer(2)operator(])operator(,) ident(cols)operator([)integer(0)operator(])operator(])operator(\)) - operator(}) - operator(}) - ident(result) -reserved(end) - -reserved(begin) - constant(DBI)operator(.)ident(connect)operator(()stringoperator(,) stringoperator(,) stringoperator(\)) operator({) - operator(|)ident(conn)operator(|) - - ident(conn)operator(.)ident(do)operator(()stringoperator(\)) - - ident(users) operator(=) ident(getpwent) - - ident(conn)operator(.)ident(prepare)operator(()stringoperator(\)) operator({)operator(|)ident(sth)operator(|) - ident(users)operator(.)ident(each) operator({)operator(|)ident(entry)operator(|) - ident(sth)operator(.)ident(execute)operator(()ident(entry)operator([)integer(0)operator(])operator(,) ident(entry)operator([)integer(1)operator(])operator(\)) - operator(}) - operator(}) - - ident(conn)operator(.)ident(execute)operator(()stringoperator(\)) operator({)operator(|)ident(sth)operator(|) - ident(sth)operator(.)ident(fetch) operator({)operator(|)ident(row)operator(|) - ident(puts) ident(row)operator(.)ident(collect) operator({)operator(|)ident(col)operator(|) - reserved(if) ident(col)operator(.)ident(nil?) - string - reserved(else) - ident(col) - reserved(end) - operator(})operator(.)ident(join)operator(()stringoperator(\)) - operator(}) - operator(}) - - ident(conn)operator(.)ident(do)operator(()stringoperator(\)) - operator(}) -reserved(rescue) constant(DBI)operator(::)constant(DatabaseError) operator(=)operator(>) ident(e) - ident(puts) string - ident(puts) stringdelimiter(")> - ident(puts) stringdelimiter(")> -reserved(end) - - -comment(# @@PLEAC@@_15.1) -comment(# This test program demonstrates parsing program arguments.) -comment(# It uses the optparse library, which is included with ruby 1.8) -comment(# It handles classic unix style and gnu style options) -ident(require) string - -instance_variable(@debugmode) operator(=) pre_constant(false) -instance_variable(@verbose) operator(=) pre_constant(false) - -pre_constant(ARGV)operator(.)ident(options) reserved(do) operator(|)ident(opts)operator(|) - ident(opts)operator(.)ident(banner) operator(=) stringcontent( [OPTIONS] INPUTFILES)delimiter(")> - - ident(opts)operator(.)ident(on)operator(()stringoperator(,) stringoperator(,) stringoperator(\)) operator({) - ident(puts) ident(opts) - ident(exit) - operator(}) - comment(# The OptionParser#on method is called with a specification of short) - comment(# options, of long options, a data type spezification and user help) - comment(# messages for this option.) - comment(# The method analyses the given parameter and decides what it is,) - comment(# so you can leave out the long option if you don't need it) - ident(opts)operator(.)ident(on)operator(()stringoperator(,) stringoperator(,) constant(TrueClass)operator(,) stringoperator(\)) operator({) - operator(|)instance_variable(@verbose)operator(|) comment(# sets @verbose to true or false) - operator(}) - ident(opts)operator(.)ident(on)operator(()stringoperator(,) stringoperator(,) constant(TrueClass)operator(,) string operator(\))operator({) - operator(|)instance_variable(@debugmode)operator(|) comment(# sets @debugmode to true) - operator(}) - ident(opts)operator(.)ident(on)operator(()stringoperator(,) stringoperator(,) constant(Integer)operator(,) string operator(\))operator({) - operator(|)instance_variable(@count)operator(|) comment(# sets @count to given integer) - operator(}) - ident(opts)operator(.)ident(on)operator(()stringoperator(,) stringoperator(,) constant(String)operator(,) stringoperator(\))operator({) - operator(|)instance_variable(@outputfile)operator(|) comment(# sets @outputfile to given string) - operator(}) - ident(opts)operator(.)ident(parse!) -reserved(end) - -comment(# example to use the options in the main program) -ident(puts) string reserved(if) instance_variable(@verbose) -ident(puts) string reserved(if) instance_variable(@debugmode) -ident(puts) stringdelimiter(")> reserved(if) reserved(defined?) instance_variable(@outputfile) -ident(puts) stringdelimiter(")> reserved(if) reserved(defined?) instance_variable(@count) -pre_constant(ARGV)operator(.)ident(each) operator({) operator(|)ident(param)operator(|) - ident(puts) stringdelimiter(")> -operator(}) - - -comment(# @@PLEAC@@_15.4) -ident(buf) operator(=) string operator(*) integer(8) -global_variable($stdout)operator(.)ident(ioctl)operator(()integer(0x5413)operator(,) ident(buf)operator(\)) -ident(ws_row)operator(,) ident(ws_col)operator(,) ident(ws_xpixel)operator(,) ident(ws_ypixel) operator(=) ident(buf)operator(.)ident(unpack)operator(()stringoperator(\)) - -ident(raise) string reserved(unless) ident(ws_col) operator(>)operator(=) integer(20) -ident(max) operator(=) integer(0) -ident(values) operator(=) operator(()integer(1)operator(..)integer(5)operator(\))operator(.)ident(collect) operator({) ident(rand)operator(()integer(20)operator(\)) operator(}) comment(# generate an array[5] of rand values) -reserved(for) ident(i) reserved(in) ident(values) - ident(max) operator(=) ident(i) reserved(if) ident(max) operator(<) ident(i) -reserved(end) -ident(ratio) operator(=) constant(Float)operator(()ident(ws_col)operator(-)integer(12)operator(\))operator(/)ident(max) comment(# chars per unit) -reserved(for) ident(i) reserved(in) ident(values) - ident(printf) stringoperator(,) ident(i)operator(,) string operator(*) operator(()ident(ratio)operator(*)ident(i)operator(\)) -reserved(end) - -comment(# gives, for example:) -comment(# 15.0 *******************************) -comment(# 10.0 *********************) -comment(# 5.0 **********) -comment(# 14.0 *****************************) -comment(# 18.0 **************************************) - - -comment(# @@PLEAC@@_16.1) -ident(output) operator(=) shell comment(# collect output into one multiline string) -ident(output) operator(=) shelloperator(.)ident(split) comment(# collect output into array, one line per) -ident(element) - -ident(readme) operator(=) constant(IO)operator(.)ident(popen)operator(()stringoperator(\)) -ident(output) operator(=) string -reserved(while) ident(readme)operator(.)ident(gets) reserved(do) - ident(output) operator(+=) global_variable($_) -reserved(end) -ident(readme)operator(.)ident(close) - -shell comment(# BAD AND SCARY in Perl because it's managed by the shell) - comment(# I donna in Ruby ...) - -comment(# so the "clean and secure" version) -ident(readme)operator(,) ident(writeme) operator(=) constant(IO)operator(.)ident(pipe) -ident(pid) operator(=) ident(fork) operator({) - comment(# child) - global_variable($stdout) operator(=) ident(writeme) - ident(readme)operator(.)ident(close) - ident(exec)operator(()stringoperator(,) stringoperator(\)) -operator(}) -comment(# parent) -constant(Process)operator(.)ident(waitpid)operator(()ident(pid)operator(,) integer(0)operator(\)) -ident(writeme)operator(.)ident(close) -reserved(while) ident(readme)operator(.)ident(gets) reserved(do) - comment(# do something with $_) -reserved(end) - - -comment(# @@PLEAC@@_16.2) -ident(status) operator(=) ident(system)operator(()stringdelimiter(")>operator(\)) - -ident(status) operator(=) ident(system)operator(()stringoperator(,) ident(myfile)operator(\)) - -ident(system)operator(()stringoutfile)delimiter(")>operator(\)) -ident(system)operator(()stringoutfile 2>errfile)delimiter(")>operator(\)) - -comment(# stop if the command fails) -ident(raise) stringdelimiter(")> reserved(unless) ident(system)operator(()stringoperator(,) stringoperator(,) stringoperator(\)) - -comment(# get the value of the signal sent to the child) -comment(# even if it is a SIGINT or SIGQUIT) -ident(system)operator(()ident(arglist)operator(\)) -ident(raise) stringdelimiter(")> reserved(if) operator(()global_variable($?) operator(&) integer(127)operator(\)) operator(!=) integer(0) - -ident(pid) operator(=) ident(fork) operator({) - ident(trap)operator(()stringoperator(,) stringoperator(\)) - ident(exec)operator(()stringoperator(,) stringoperator(\)) -operator(}) -ident(trap) operator(()stringoperator(\)) operator({) - ident(puts) string -operator(}) -constant(Process)operator(.)ident(waitpid)operator(()ident(pid)operator(,) integer(0)operator(\)) - -comment(# Ruby doesn't permit to lie to the program called by a 'system'.) -comment(# (ie specify what return argv[0] in C, $0 in Perl/Ruby ...\)) -comment(# A (dirty\) way is to create a link (under Unix\), run this link and) -comment(# erase it. Somebody has a best idea ?) - - -comment(# @@PLEAC@@_16.3) -ident(exec)operator(()stringoperator(\)) - -ident(exec)operator(()stringoperator(,) stringoperator(\)) - -ident(exec)operator(()stringoperator(\)) - - -comment(# @@PLEAC@@_16.4) -comment(# read the output of a program) -constant(IO)operator(.)ident(popen)operator(()stringoperator(\)) operator({)operator(|)ident(readme)operator(|) - reserved(while) ident(readme)operator(.)ident(gets) reserved(do) - comment(# ...) - reserved(end) -operator(}) -comment(# or) -ident(readme) operator(=) constant(IO)operator(.)ident(popen)operator(()stringoperator(\)) -reserved(while) ident(readme)operator(.)ident(gets) reserved(do) - comment(# ...) -reserved(end) -ident(readme)operator(.)ident(close) - -comment(# "write" in a program) -constant(IO)operator(.)ident(popen)operator(()stringoperator(,)stringoperator(\)) operator({)operator(|)ident(pipe)operator(|) - ident(pipe)operator(.)ident(puts)operator(()stringoperator(\)) - ident(pipe)operator(.)ident(puts)operator(()stringoperator(\)) -operator(}) - -comment(# close wait for the end of the process) -ident(read) operator(=) constant(IO)operator(.)ident(popen)operator(()stringoperator(\)) comment(# child goes to sleep) -ident(read)operator(.)ident(close) comment(# and the parent goes to lala land) - -ident(writeme) operator(=) constant(IO)operator(.)ident(popen)operator(()stringoperator(,) stringoperator(\)) -ident(writeme)operator(.)ident(puts) string comment(# program will get hello\\n on STDIN) -ident(writeme)operator(.)ident(close) comment(# program will get EOF on STDIN) - -comment(# send in a pager (eg less\) all output) -global_variable($stdout) operator(=) constant(IO)operator(.)ident(popen)operator(()stringoperator(,)stringoperator(\)) -ident(print) string operator(*) integer(10000) - - -comment(# @@PLEAC@@_16.5) -comment(#-----------------------------) -reserved(def) method(head)operator(()ident(lines) operator(=) integer(20)operator(\)) - ident(pid) operator(=) ident(open)operator(()stringoperator(,)stringoperator(\)) - reserved(if) ident(pid) operator(==) pre_constant(nil) - reserved(return) - reserved(else) - reserved(while) ident(gets)operator(()operator(\)) reserved(do) - ident(pid)operator(.)ident(print) - ident(lines) operator(-=) integer(1) - reserved(break) reserved(if) ident(lines) operator(==) integer(0) - reserved(end) - reserved(end) - ident(exit) -reserved(end) - -ident(head)operator(()integer(100)operator(\)) -reserved(while) ident(gets)operator(()operator(\)) reserved(do) - ident(print) -reserved(end) -comment(#-----------------------------) -integer(1)operator(:) operator(>) constant(Welcome) ident(to) constant(Linux)operator(,) ident(version) float(2.0)operator(.)integer(33) ident(on) ident(a) ident(i686) - -integer(2)operator(:) operator(>) - -integer(3)operator(:) operator(>) string so I installed Linux.)delimiter(")> -comment(#-----------------------------) -operator(>) integer(1)operator(:) constant(Welcome) ident(to) constant(Linux)operator(,) constant(Kernel) ident(version) float(2.0)operator(.)integer(33) ident(on) ident(a) ident(i686) - -operator(>) integer(2)operator(:) - -operator(>) integer(3)operator(:) string 4: so I installed Linux.)delimiter(")> -comment(#-----------------------------) -comment(#!/usr/bin/ruby) -comment(# qnumcat - demo additive output filters) - -reserved(def) method(number)operator(()operator(\)) - ident(pid) operator(=) ident(open)operator(()stringoperator(,)stringoperator(\)) - reserved(if) ident(pid) operator(==) pre_constant(nil) - reserved(return) - reserved(else) - reserved(while) ident(gets)operator(()operator(\)) reserved(do) ident(pid)operator(.)ident(printf)operator(()stringoperator(,) global_variable($.)operator(,) global_variable($_)operator(\))operator(;) reserved(end) - reserved(end) - ident(exit) -reserved(end) - -reserved(def) method(quote)operator(()operator(\)) - ident(pid) operator(=) ident(open)operator(()stringoperator(,)stringoperator(\)) - reserved(if) ident(pid) operator(==) pre_constant(nil) - reserved(return) - reserved(else) - reserved(while) ident(gets)operator(()operator(\)) reserved(do) ident(pid)operator(.)ident(print) string )inlinedelimiter(")> reserved(end) - reserved(end) - ident(exit) -reserved(end) - -ident(number)operator(()operator(\)) -ident(quote)operator(()operator(\)) - -reserved(while) ident(gets)operator(()operator(\)) reserved(do) - ident(print) -reserved(end) -global_variable($stdout)operator(.)ident(close) -ident(exit) - - -comment(# @@PLEAC@@_16.6) -pre_constant(ARGV)operator(.)ident(map!) operator({) operator(|)ident(arg)operator(|) - ident(arg) operator(=)operator(~) regexp operator(?) stringdelimiter(")> operator(:) ident(arg) -operator(}) -reserved(for) ident(file) reserved(in) pre_constant(ARGV) - ident(fh) operator(=) ident(open)operator(()ident(file)operator(\)) - reserved(while) ident(fh)operator(.)ident(gets)operator(()operator(\)) reserved(do) - comment(# .......) - reserved(end) -reserved(end) -comment(#-----------------------------) -pre_constant(ARGV)operator(.)ident(map!) operator({) operator(|)ident(arg)operator(|) - ident(arg) operator(=)operator(~) regexp operator(?) stringdelimiter(")> operator(:) ident(arg) comment(#) -operator(}) -reserved(for) ident(file) reserved(in) pre_constant(ARGV) - ident(fh) operator(=) ident(open)operator(()ident(file)operator(\)) - reserved(while) ident(fh)operator(.)ident(gets)operator(()operator(\)) reserved(do) - comment(# .......) - reserved(end) -reserved(end) -comment(#-----------------------------) -ident(pwdinfo) operator(=) operator(()shell operator(=)operator(~) regexpoperator(\)) operator(?) string operator(:) stringoperator(;) -ident(pwd) operator(=) ident(open)operator(()ident(pwdinfo)operator(\))operator(;) -comment(#-----------------------------) -ident(puts) stringoperator(;) -ident(file) operator(=) ident(gets)operator(()operator(\))operator(.)ident(chomp)operator(()operator(\))operator(;) -ident(fh) operator(=) ident(open)operator(()ident(file)operator(\))operator(;) - - -comment(# @@PLEAC@@_16.7) -ident(output) operator(=) shell&1)delimiter(`)> comment(# with backticks) -comment(# or) -ident(ph) operator(=) ident(open)operator(()string&1)delimiter(")>operator(\)) comment(# with an open pipe) -reserved(while) ident(ph)operator(.)ident(gets)operator(()operator(\)) operator({) operator(}) comment(# plus a read) -comment(#-----------------------------) -ident(output) operator(=) shell/dev/null)delimiter(`)> comment(# with backticks) -comment(# or) -ident(ph) operator(=) ident(open)operator(()string/dev/null)delimiter(")>operator(\)) comment(# with an open pipe) -reserved(while) ident(ph)operator(.)ident(gets)operator(()operator(\)) operator({) operator(}) comment(# plus a read) -comment(#-----------------------------) -ident(output) operator(=) shell&1 1>/dev/null)delimiter(`)> comment(# with backticks) -comment(# or) -ident(ph) operator(=) ident(open)operator(()string&1 1>/dev/null)delimiter(")>operator(\)) comment(# with an open pipe) -reserved(while) ident(ph)operator(.)ident(gets)operator(()operator(\)) operator({) operator(}) comment(# plus a read) -comment(#-----------------------------) -ident(output) operator(=) shell&1 1>&2 2>&3 3>&-)delimiter(`)> comment(# with backticks) -comment(# or) -ident(ph) operator(=) ident(open)operator(()string&1 1>&2 2>&3 3>&-)delimiter(")>operator(\)) comment(# with an open pipe) -reserved(while) ident(ph)operator(.)ident(gets)operator(()operator(\)) operator({) operator(}) comment(# plus a read) -comment(#-----------------------------) -ident(system)operator(()string/tmp/program.stdout 2>/tmp/program.stderr)delimiter(")>operator(\)) -comment(#-----------------------------) -ident(output) operator(=) shell&1 1>&2 2>&3 3>&-)delimiter(`)> -comment(#-----------------------------) -ident(fd3) operator(=) ident(fd1) -ident(fd1) operator(=) ident(fd2) -ident(fd2) operator(=) ident(fd3) -ident(fd3) operator(=) reserved(undef) -comment(#-----------------------------) -method(system)operator(()stringtmpfile 2>&1)delimiter(")>operator(\)) -ident(system)operator(()string&1 1>tmpfile)delimiter(")>operator(\)) -comment(#-----------------------------) -comment(# system ("prog args 1>tmpfile 2>&1"\)) -ident(fd1) operator(=) string comment(# change stdout destination first) -ident(fd2) operator(=) ident(fd1) comment(# now point stderr there, too) -comment(#-----------------------------) -comment(# system("prog args 2>&1 1>tmpfile"\)) -ident(fd2) operator(=) ident(fd1) comment(# stderr same destination as stdout) -ident(fd1) operator(=) string comment(# but change stdout destination) -comment(#-----------------------------) -comment(# It is often better not to rely on the shell,) -comment(# because of portability, possible security problems) -comment(# and bigger resource usage. So, it is often better to use the open3 library.) -comment(# See below for an example.) -comment(# opening stdin, stdout, stderr) -ident(require) string -ident(stdin)operator(,) ident(stdout)operator(,) ident(stderr) operator(=) constant(Open3)operator(.)ident(popen)operator(()stringoperator(\)) - - -comment(# @@PLEAC@@_16.8) -comment(#-----------------------------) -comment(# Contrary to perl, we don't need to use a module in Ruby) -ident(fh) operator(=) constant(Kernel)operator(.)ident(open)operator(()string operator(+) ident(program)operator(,) stringoperator(\)) -ident(fh)operator(.)ident(puts) string -ident(output) operator(=) ident(fh)operator(.)ident(gets)operator(()operator(\)) -ident(fh)operator(.)ident(close)operator(()operator(\)) -comment(#-----------------------------) -constant(Kernel)operator(.)ident(open)operator(()stringoperator(\))operator(,)stringoperator(\)) comment(# RIGHT !) -comment(#-----------------------------) -comment(# Ruby has already object methods for I/O handles) -comment(#-----------------------------) -reserved(begin) - ident(fh) operator(=) constant(Kernel)operator(.)ident(open)operator(()string operator(+) ident(program_and_options)operator(,) stringoperator(\)) -reserved(rescue) - reserved(if) operator(()global_variable($@) operator(~=) regexpoperator(\)) - global_variable($stderr)operator(.)ident(puts) stringcontent( )char(\\n)content( )inlinecontent( )char(\\n)delimiter(")> - reserved(break) - reserved(end) - ident(raise) comment(# reraise unforseen exception) -reserved(end) - - -comment(# @@PLEAC@@_16.13) -comment(#% kill -l) -comment(#HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE) -comment(#ALRM TERM CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM) -comment(#PROF WINCH POLL PWR) -comment(#-----------------------------) -comment(#% ruby -e 'puts Signal.list.keys.join(" "\)') -comment(#PWR USR1 BUS USR2 TERM SEGV KILL POLL STOP SYS TRAP IOT HUP INT #) -comment(#WINCH XCPU TTIN CLD TSTP FPE IO TTOU PROF CHLD CONT PIPE ABRT) -comment(#VTALRM QUIT ILL XFSZ URG ALRM) -comment(#-----------------------------) -comment(# After that, the perl script create an hash equivalent to Signal.list,) -comment(# and an array. The array can be obtained by :) -ident(signame) operator(=) operator([)operator(]) -constant(Signal)operator(.)ident(list)operator(.)ident(each) operator({) operator(|)ident(name)operator(,) ident(i)operator(|) ident(signame)operator([)ident(i)operator(]) operator(=) ident(name) operator(}) - - -comment(# @@PLEAC@@_16.14) -constant(Process)operator(.)ident(kill)operator(()integer(9)operator(,) ident(pid)operator(\)) comment(# send $pid a signal 9) -constant(Process)operator(.)ident(kill)operator(()operator(-)integer(1)operator(,) constant(Process)operator(.)ident(getpgrp)operator(()operator(\))operator(\)) comment(# send whole job a signal 1) -constant(Process)operator(.)ident(kill)operator(()stringoperator(,) global_variable($$)operator(\)) comment(# send myself a SIGUSR1) -constant(Process)operator(.)ident(kill)operator(()stringoperator(,) ident(pid1)operator(,) ident(pid2)operator(,) ident(pid3)operator(\)) comment(# send a SIGHUP to processes in @pids) -comment(#-----------------------------) -reserved(begin) - constant(Process)operator(.)ident(kill)operator(()integer(0)operator(,) ident(minion)operator(\)) - ident(puts) stringcontent( is alive!)delimiter(")> -reserved(rescue) constant(Errno)operator(::)constant(EPERM) comment(# changed uid) - ident(puts) stringcontent( has escaped my control!)delimiter(")>operator(;) -reserved(rescue) constant(Errno)operator(::)constant(ESRCH) - ident(puts) stringcontent( is deceased.)delimiter(")>operator(;) comment(# or zombied) -reserved(rescue) - ident(puts) stringcontent( : )inlinedelimiter(")> -reserved(end) - - -comment(# @@PLEAC@@_16.15) -constant(Kernel)operator(.)ident(trap)operator(()stringoperator(,) ident(got_sig_quit)operator(\)) comment(# got_sig_quit = Proc.new { puts "Quit\\n" }) -ident(trap)operator(()stringoperator(,) stringoperator(\)) comment(# def got_sig_pipe ...) -ident(trap)operator(()stringoperator(\)) operator({) ident(ouch)operator(+)operator(+) operator(}) comment(# increment ouch for every SIGINT) -comment(#-----------------------------) -ident(trap)operator(()stringoperator(,) stringoperator(\)) comment(# ignore the signal INT) -comment(#-----------------------------) -ident(trap)operator(()stringoperator(,) stringoperator(\)) comment(# restore default STOP signal handling) - - -comment(# @@PLEAC@@_16.16) -comment(# the signal handler) -reserved(def) method(ding) - ident(trap)operator(()stringoperator(,) stringoperator(\)) - ident(puts) string -reserved(end) - -comment(# prompt for name, overriding SIGINT) -reserved(def) method(get_name) - ident(save) operator(=) ident(trap)operator(()stringoperator(,) stringoperator(\)) - - ident(puts) string - ident(name) operator(=) ident(gets)operator(()operator(\))operator(.)ident(chomp)operator(()operator(\)) - ident(trap)operator(()stringoperator(,) ident(save)operator(\)) - ident(name) -reserved(end) - - -comment(# @@PLEAC@@_16.21) -comment(# implemented thanks to http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/1760) -ident(require) string - -comment(# we'll do something vastly more useful than cookbook to demonstrate timeouts) -reserved(begin) - ident(timeout)operator(()integer(5)operator(\)) operator({) - ident(waitsec) operator(=) ident(rand)operator(()integer(10)operator(\)) - ident(puts) stringcontent( seconds is longer than 5 seconds...)delimiter(")> - ident(system)operator(()stringdelimiter(")>operator(\)) - operator(}) - ident(puts) string -reserved(rescue) constant(Timeout)operator(::)constant(Error) - ident(puts) string -reserved(end) - - -comment(# @@PLEAC@@_17.1) -comment(# A basic TCP client connection) -ident(require) string -reserved(begin) - ident(t) operator(=) constant(TCPSocket)operator(.)ident(new)operator(()stringoperator(,) stringoperator(\)) -reserved(rescue) - ident(puts) stringdelimiter(")> -reserved(else) - comment(# ... do something with the socket) - ident(t)operator(.)ident(print) string - ident(answer) operator(=) ident(t)operator(.)ident(gets)operator(()pre_constant(nil)operator(\)) - comment(# and terminate the connection when we're done) - ident(t)operator(.)ident(close) -reserved(end) - -comment(# Using the evil low level socket API) -ident(require) string -comment(# create a socket) -ident(s) operator(=) constant(Socket)operator(.)ident(new)operator(()constant(Socket)operator(::)constant(AF_INET)operator(,) constant(Socket)operator(::)constant(SOCK_STREAM)operator(,) integer(0)operator(\)) -comment(# build the address of the remote machine) -ident(sockaddr_server) operator(=) operator([)constant(Socket)operator(::)constant(AF_INET)operator(,) integer(80)operator(,) - constant(Socket)operator(.)ident(gethostbyname)operator(()stringoperator(\))operator([)integer(3)operator(])operator(,) - integer(0)operator(,) integer(0)operator(])operator(.)ident(pack)operator(()stringoperator(\)) -comment(# connect) -reserved(begin) - ident(s)operator(.)ident(connect)operator(()ident(sockaddr_server)operator(\)) -reserved(rescue) - ident(puts) stringdelimiter(")> -reserved(else) - comment(# ... do something with the socket) - ident(s)operator(.)ident(print) string - comment(# and terminate the connection when we're done) - ident(s)operator(.)ident(close) -reserved(end) - -comment(# TCP connection with management of error (DNS\)) -ident(require) string -reserved(begin) - ident(client) operator(=) constant(TCPSocket)operator(.)ident(new)operator(()stringoperator(,) stringoperator(\)) -reserved(rescue) - ident(puts) stringdelimiter(")> -reserved(end) - -comment(# TCP connection with a time out) -ident(require) string -ident(require) string -reserved(begin) - ident(timeout)operator(()integer(1)operator(\)) reserved(do) comment(#the server has one second to answer) - ident(client) operator(=) constant(TCPSocket)operator(.)ident(new)operator(()stringoperator(,) stringoperator(\)) - reserved(end) -reserved(rescue) - ident(puts) stringdelimiter(")> -reserved(end) - - -comment(# @@PLEAC@@_17.12) -ident(require) string - -reserved(class) class(Preforker) - ident(attr_reader) operator(()symbol(:child_count)operator(\)) - - reserved(def) method(initialize)operator(()ident(prefork)operator(,) ident(max_clients_per_child)operator(,) ident(port)operator(,) ident(client_handler)operator(\)) - instance_variable(@prefork) operator(=) ident(prefork) - instance_variable(@max_clients_per_child) operator(=) ident(max_clients_per_child) - instance_variable(@port) operator(=) ident(port) - instance_variable(@child_count) operator(=) integer(0) - - instance_variable(@reaper) operator(=) ident(proc) operator({) - ident(trap)operator(()stringoperator(,) instance_variable(@reaper)operator(\)) - ident(pid) operator(=) constant(Process)operator(.)ident(wait) - instance_variable(@child_count) operator(-=) integer(1) - operator(}) - - instance_variable(@huntsman) operator(=) ident(proc) operator({) - ident(trap)operator(()stringoperator(,) stringoperator(\)) - ident(trap)operator(()stringoperator(,) stringoperator(\)) - constant(Process)operator(.)ident(kill)operator(()stringoperator(,) integer(0)operator(\)) - ident(exit) - operator(}) - - instance_variable(@client_handler)operator(=)ident(client_handler) - reserved(end) - - reserved(def) method(child_handler) - ident(trap)operator(()stringoperator(,) stringoperator(\)) - instance_variable(@client_handler)operator(.)ident(setUp) - comment(# wish: sigprocmask UNblock SIGINT) - instance_variable(@max_clients_per_child)operator(.)ident(times) operator({) - ident(client) operator(=) instance_variable(@server)operator(.)ident(accept) reserved(or) reserved(break) - instance_variable(@client_handler)operator(.)ident(handle_request)operator(()ident(client)operator(\)) - ident(client)operator(.)ident(close) - operator(}) - instance_variable(@client_handler)operator(.)ident(tearDown) - reserved(end) - - reserved(def) method(make_new_child) - comment(# wish: sigprocmask block SIGINT) - instance_variable(@child_count) operator(+=) integer(1) - ident(pid) operator(=) ident(fork) reserved(do) - ident(child_handler) - reserved(end) - comment(# wish: sigprocmask UNblock SIGINT) - reserved(end) - - reserved(def) method(run) - instance_variable(@server) operator(=) constant(TCPserver)operator(.)ident(open)operator(()instance_variable(@port)operator(\)) - ident(trap)operator(()stringoperator(,) instance_variable(@reaper)operator(\)) - ident(trap)operator(()stringoperator(,) instance_variable(@huntsman)operator(\)) - ident(loop) operator({) - operator(()instance_variable(@prefork) operator(-) instance_variable(@child_count)operator(\))operator(.)ident(times) operator({) operator(|)ident(i)operator(|) - ident(make_new_child) - operator(}) - ident(sleep) operator(.)integer(1) - operator(}) - reserved(end) -reserved(end) - -comment(#-----------------------------) -comment(#!/usr/bin/ruby) - -ident(require) string - -reserved(class) class(ClientHandler) - reserved(def) method(setUp) - reserved(end) - - reserved(def) method(tearDown) - reserved(end) - - reserved(def) method(handle_request)operator(()ident(client)operator(\)) - comment(# do stuff) - reserved(end) -reserved(end) - -ident(server) operator(=) constant(Preforker)operator(.)ident(new)operator(()integer(1)operator(,) integer(100)operator(,) integer(3102)operator(,) constant(ClientHandler)operator(.)ident(new)operator(\)) -ident(server)operator(.)ident(run) - - -comment(# @@PLEAC@@_18.2) -ident(require) string - -reserved(begin) - ident(ftp) operator(=) constant(Net)operator(::)constant(FTP)operator(::)ident(new)operator(()stringoperator(\)) - ident(ftp)operator(.)ident(login)operator(()ident(username)operator(,)ident(password)operator(\)) - ident(ftp)operator(.)ident(chdir)operator(()ident(directory)operator(\)) - ident(ftp)operator(.)ident(get)operator(()ident(filename)operator(\)) - ident(ftp)operator(.)ident(put)operator(()ident(filename)operator(\)) -reserved(rescue) constant(Net)operator(::)constant(FTPError) - global_variable($stderr)operator(.)ident(print) string operator(+) global_variable($!) -reserved(ensure) - ident(ftp)operator(.)ident(close)operator(()operator(\)) reserved(if) ident(ftp) -reserved(end) - -comment(# A better solution for a local use could be :) -constant(Net)operator(::)constant(FTP)operator(::)ident(new)operator(()stringoperator(\)) reserved(do) operator(|)ident(ftp)operator(|) - ident(ftp)operator(.)ident(login)operator(()ident(username)operator(,)ident(password)operator(\)) - ident(ftp)operator(.)ident(chdir)operator(()ident(directory)operator(\)) - ident(ftp)operator(.)ident(get)operator(()ident(filename)operator(\)) - ident(ftp)operator(.)ident(put)operator(()ident(filename)operator(\)) -reserved(end) - -comment(# If you have only one file to get, there is a simple solution :) -ident(require) string -ident(open)operator(()stringoperator(\)) reserved(do) operator(|)ident(fh)operator(|) - comment(# read from filehandle fh) -reserved(end) -comment(#--------------------------------------------) -comment(# to wait a defined time for the connection,) -comment(# use the timeout module) -ident(require) string -reserved(begin) - ident(timeout)operator(()integer(30)operator(\))operator({) - ident(ftp) operator(=) constant(Net)operator(::)constant(FTP)operator(::)ident(new)operator(()stringoperator(\)) - ident(ftp)operator(.)ident(debug_mode) operator(=) pre_constant(true) - operator(}) -reserved(rescue) constant(Net)operator(::)constant(FTPError) - global_variable($stderr)operator(.)ident(puts) string -reserved(rescue) constant(Timeout)operator(::)constant(Error) - global_variable($stderr)operator(.)ident(puts) string -reserved(end) - -reserved(begin) - ident(ftp)operator(.)ident(login)operator(()operator(\)) -reserved(rescue) constant(Net)operator(::)constant(FTPError) - global_variable($stderr)operator(.)ident(print) string -reserved(end) - -reserved(begin) - ident(ftp)operator(.)ident(login)operator(()ident(username)operator(\)) -reserved(rescue) constant(Net)operator(::)constant(FTPError) - global_variable($stderr)operator(.)ident(print) string -reserved(end) - -reserved(begin) - ident(ftp)operator(.)ident(login)operator(()ident(username)operator(,) ident(password)operator(\)) -reserved(rescue) constant(Net)operator(::)constant(FTPError) - global_variable($stderr)operator(.)ident(print) string -reserved(end) - -reserved(begin) - ident(ftp)operator(.)ident(login)operator(()ident(username)operator(,) ident(password)operator(,) ident(account)operator(\)) -reserved(rescue) constant(Net)operator(::)constant(FTPError) - global_variable($stderr)operator(.)ident(print) string -reserved(end) -comment(#-----------------------------) -ident(ftp)operator(.)ident(put)operator(()ident(localfile)operator(,) ident(remotefile)operator(\)) -comment(#-----------------------------) -comment(# Sending data from STDIN is not directly supported) -comment(# by the ftp library module. A possible way to do it is to use the) -comment(# storlines method directly to send raw commands to the ftp server.) -comment(#-----------------------------) -ident(ftp)operator(.)ident(get)operator(()ident(remotefile)operator(,) ident(localfile)operator(\)) -comment(#-----------------------------) -ident(ftp)operator(.)ident(get)operator(()ident(remotefile)operator(\)) operator({) operator(|)ident(data)operator(|) ident(puts) ident(data) operator(}) -comment(#-----------------------------) -ident(ftp)operator(.)ident(chdir)operator(()stringoperator(\)) -ident(print) stringoperator(,) ident(ftp)operator(.)ident(pwd)operator(()operator(\))operator(,) string -comment(#-----------------------------) -ident(ftp)operator(.)ident(mkdir)operator(()stringoperator(\)) -comment(#-----------------------------) -ident(lines) operator(=) ident(ftp)operator(.)ident(ls)operator(()stringoperator(\)) -comment(# => ["drwxr-xr-x 2 matz users 4096 July 17 1998 1.0", ... ]) - -ident(latest) operator(=) ident(ftp)operator(.)ident(dir)operator(()stringoperator(\))operator(.)ident(sort)operator(.)ident(last) - -ident(ftp)operator(.)ident(nlst)operator(()stringoperator(\)) -comment(# => ["/pub/ruby/1.0", ... ]) -comment(#-----------------------------) -ident(ftp)operator(.)ident(quit)operator(()operator(\)) - - -comment(# @@PLEAC@@_18.6) -ident(require) string -ident(t) operator(=) constant(Net)operator(::)constant(Telnet)operator(::)ident(new)operator(() string operator(=)operator(>) integer(10)operator(,) - string operator(=)operator(>) regexpoperator(,) - string operator(=)operator(>) ident(host) operator(\)) -ident(t)operator(.)ident(login)operator(()ident(username)operator(,) ident(password)operator(\)) -ident(files) operator(=) ident(t)operator(.)ident(cmd)operator(()stringoperator(\)) -ident(t)operator(.)ident(print)operator(()stringoperator(\)) -ident(process_string) operator(=) ident(t)operator(.)ident(waitfor)operator(()regexpoperator(\)) -ident(t)operator(.)ident(close) -comment(#-----------------------------) -regexp] )char(\\z)delimiter(/)modifier(n)> -comment(#-----------------------------) -comment(# In case of an error, the telnet module throws an exception.) -comment(# For control of the behavior in case of an error,) -comment(# you just need to catch the exceptions and do your custom) -comment(# error handling.) -comment(#-----------------------------) -reserved(begin) - ident(telnet)operator(.)ident(login)operator(()ident(username)operator(,) ident(password)operator(\)) -reserved(rescue) constant(TimeoutError) - ident(fail) string -reserved(end) -comment(#-----------------------------) -ident(telnet)operator(.)ident(waitfor)operator(()stringoperator(\)) -comment(#-----------------------------) -ident(telnet)operator(.)ident(waitfor)operator(()constant(String) operator(=)operator(>) stringoperator(,) constant(Timeout) operator(=)operator(>) integer(30)operator(\)) - - -comment(# @@PLEAC@@_18.7) -ident(require) string - -ident(puts) stringcontent( is alive.)char(\\n)delimiter(")> reserved(if) constant(Ping)operator(.)ident(pingecho)operator(()ident(host)operator(\))operator(;) -comment(#-----------------------------) -comment(# the ping module only use TCP ping, not ICMP even if we are root) -reserved(if) constant(Ping)operator(.)ident(pingecho)operator(()stringoperator(\)) - ident(puts) stringoperator(;) -reserved(else) - ident(puts) stringoperator(;) -reserved(end) - - -comment(# @@PLEAC@@_19.1) -comment(#!/usr/local/bin/ruby -w) -comment(# hiweb - load CGI class to decode information given by web server) - -ident(require) string - -ident(cgi) operator(=) constant(CGI)operator(.)ident(new)operator(()stringoperator(\)) - -comment(# get a parameter from a form) -ident(value) operator(=) ident(cgi)operator(.)ident(params)operator([)stringoperator(])operator([)integer(0)operator(]) - -comment(# output a document) -ident(cgi)operator(.)ident(out) operator({) - ident(cgi)operator(.)ident(html) operator({) - ident(cgi)operator(.)ident(head) operator({) ident(cgi)operator(.)ident(title) operator({) string operator(}) operator(}) operator(+) - ident(cgi)operator(.)ident(body) operator({) ident(cgi)operator(.)ident(p) operator({) string operator(+) ident(cgi)operator(.)ident(tt) operator({) - constant(CGI)operator(.)ident(escapeHTML)operator(()ident(value)operator(\)) operator(}) operator(}) operator(}) - operator(}) -operator(}) - -ident(require) string -ident(cgi) operator(=) constant(CGI)operator(.)ident(new) -ident(who) operator(=) ident(cgi)operator(.)ident(param)operator([)stringoperator(])operator([)integer(0)operator(]) comment(# first param in list) -ident(phone) operator(=) ident(cgi)operator(.)ident(param)operator([)stringoperator(])operator([)integer(0)operator(]) -ident(picks) operator(=) ident(cgi)operator(.)ident(param)operator([)stringoperator(]) comment(# complete list) - -ident(print) ident(cgi)operator(.)ident(header)operator(() string operator(=)operator(>) stringoperator(,) - string operator(=)operator(>) constant(Time)operator(.)ident(now) operator(+) operator(()integer(3) operator(*) integer(24) operator(*) integer(60) operator(*) integer(60)operator(\)) operator(\)) - - -comment(# @@PLEAC@@_19.3) -comment(#!/usr/local/bin/ruby -w) -comment(# webwhoami - show web user's id) -ident(require) string -ident(print) string -ident(print) string operator(+) constant(Etc)operator(.)ident(getpwuid)operator(.)ident(name) operator(+) string - -comment(# % ruby -wc cgi-script # just check syntax) - -comment(# % ruby -w cgi-script # params from stdin) -comment(# (offline mode: enter name=value pairs on standard input\)) -comment(# name=joe) -comment(# number=10) -comment(# ^D) - -comment(# % ruby -w cgi-script name=joe number=10 # run with mock form input) -comment(# % ruby -d cgi-script name=joe number=10 # ditto, under the debugger) - -comment(# POST method script in csh) -comment(# % (setenv HTTP_METHOD POST; ruby -w cgi-script name=joe number=10\)) -comment(# POST method script in sh) -comment(# % HTTP_METHOD=POST perl -w cgi-script name=joe number=10) - - -comment(# @@PLEAC@@_19.4) -comment(# ruby has several security levels, the level "1" is similar to perls taint mode.) -comment(# It can be switched on by providing the -T command line parameter) -comment(# or by setting $SAFE to 1. Setting $SAFE to 2,3 or 4 restricts possible) -comment(# harmful operations further.) - -comment(#!/usr/bin/ruby -T) -global_variable($SAFE) operator(=) integer(1) -constant(File)operator(.)ident(open)operator(()pre_constant(ARGV)operator([)integer(0)operator(])operator(,) stringoperator(\)) -comment(# ruby warns with:) -comment(# taint1.rb:2:in `initialize': Insecure operation - initialize (SecurityError\)) - -global_variable($SAFE) operator(=) integer(1) -ident(file) operator(=) pre_constant(ARGV)operator([)integer(0)operator(]) -reserved(unless) regexpoperator(.)ident(match)operator(()ident(file)operator(\)) - ident(raise) stringcontent( has invalid characters)delimiter(")> -reserved(end) -ident(file) operator(=) global_variable($1) -comment(# In ruby, even the back reference from a regular expression stays tainted.) -comment(# you need to explicitly untaint the variable:) -ident(file)operator(.)ident(untaint) -constant(File)operator(.)ident(open)operator(()ident(file)operator(,) stringoperator(\)) - -comment(# Race condition exists like in perl:) -reserved(unless) constant(File)operator(.)ident(exists)operator(()ident(filename)operator(\)) comment(# Wrong because of race condition) - constant(File)operator(.)ident(open)operator(()ident(filename)operator(,) stringoperator(\)) -reserved(end) - - - -comment(# @@PLEAC@@_19.10) -ident(preference_value) operator(=) ident(cgi)operator(.)ident(cookies)operator([)stringoperator(])operator([)integer(0)operator(]) - -ident(packed_cookie) operator(=) constant(CGI)operator(::)constant(Cookie)operator(.)ident(new)operator(()string operator(=)operator(>) stringoperator(,) - string operator(=)operator(>) stringoperator(,) - string operator(=)operator(>) constant(Time)operator(.)ident(local)operator(()constant(Time)operator(.)ident(now)operator(.)ident(year) operator(+) integer(2)operator(,) - constant(Time)operator(.)ident(now)operator(.)ident(mon)operator(,) constant(Time)operator(.)ident(now)operator(.)ident(day)operator(,) constant(Time)operator(.)ident(now)operator(.)ident(hour)operator(,) constant(Time)operator(.)ident(now)operator(.)ident(min)operator(,) constant(Time)operator(.)ident(now)operator(.)ident(sec)operator(\)) operator(\)) - -ident(cgi)operator(.)ident(header)operator(()string operator(=)operator(>) operator([)ident(packed_cookie)operator(])operator(\)) - -comment(#!/usr/local/bin/ruby -w) -comment(# ic_cookies - sample CGI script that uses a cookie) -ident(require) string - -ident(cgi) operator(=) constant(CGI)operator(.)ident(new)operator(()stringoperator(\)) - -ident(cookname) operator(=) string -ident(favorite) operator(=) ident(cgi)operator(.)ident(params)operator([)stringoperator(])operator([)integer(0)operator(]) -ident(tasty) operator(=) ident(cgi)operator(.)ident(cookies)operator([)ident(cookname)operator(])operator([)integer(0)operator(]) operator(||) string - -reserved(unless) ident(favorite) - ident(cgi)operator(.)ident(out) operator({) - ident(cgi)operator(.)ident(html) operator({) - ident(cgi)operator(.)ident(head) operator({) ident(cgi)operator(.)ident(title) operator({) string operator(}) operator(}) operator(+) - ident(cgi)operator(.)ident(body) operator({) - ident(cgi)operator(.)ident(h1) operator({) string operator(}) operator(+) - ident(cgi)operator(.)ident(hr) operator(+) - ident(cgi)operator(.)ident(form) operator({) - ident(cgi)operator(.)ident(p) operator({) string operator(+) - ident(cgi)operator(.)ident(text_field)operator(()stringoperator(,) ident(tasty) operator(\)) operator(}) - operator(}) operator(+) - ident(cgi)operator(.)ident(hr) - operator(}) - operator(}) - operator(}) -reserved(else) - ident(cookie) operator(=) constant(CGI)operator(::)constant(Cookie)operator(.)ident(new)operator(() string operator(=)operator(>) ident(cookname)operator(,) - string operator(=)operator(>) ident(favorite)operator(,) - string operator(=)operator(>) constant(Time)operator(.)ident(local)operator(()constant(Time)operator(.)ident(now)operator(.)ident(year) operator(+) integer(2)operator(,) -constant(Time)operator(.)ident(now)operator(.)ident(mon)operator(,) constant(Time)operator(.)ident(now)operator(.)ident(day)operator(,) constant(Time)operator(.)ident(now)operator(.)ident(hour)operator(,) constant(Time)operator(.)ident(now)operator(.)ident(min)operator(,) constant(Time)operator(.)ident(now)operator(.)ident(sec)operator(\)) operator(\)) - ident(cgi)operator(.)ident(out)operator(()string operator(=)operator(>) operator([)ident(cookie)operator(])operator(\)) operator({) - ident(cgi)operator(.)ident(html) operator({) - ident(cgi)operator(.)ident(head) operator({) ident(cgi)operator(.)ident(title) operator({) string operator(}) operator(}) operator(+) - ident(cgi)operator(.)ident(body) operator({) - ident(cgi)operator(.)ident(h1) operator({) string operator(}) operator(+) - ident(cgi)operator(.)ident(p) operator({) stringcontent('.)delimiter(")> operator(}) - operator(}) - operator(}) - operator(}) -reserved(end) - - -comment(# @@PLEAC@@_20.9) -reserved(def) method(templatefile)operator(()ident(filename)operator(,) ident(fillings)operator(\)) - ident(aFile) operator(=) constant(File)operator(.)ident(new)operator(()ident(filename)operator(,) stringoperator(\)) - ident(text) operator(=) ident(aFile)operator(.)ident(read)operator(()operator(\)) - ident(aFile)operator(.)ident(close)operator(()operator(\)) - ident(pattern) operator(=) constant(Regexp)operator(.)ident(new)operator(()stringoperator(\)) - ident(text)operator(.)ident(gsub!)operator(()ident(pattern)operator(\)) operator({) - ident(fillings)operator([)global_variable($1)operator(]) operator(||) string - operator(}) - ident(text) -reserved(end) - -ident(fields) operator(=) operator({) - string operator(=)operator(>) ident(whats_his_name)operator(,) - string operator(=)operator(>) ident(login_count)operator(,) - string operator(=)operator(>) ident(minutes_used) -operator(}) -ident(puts) ident(templatefile)operator(()stringoperator(,) ident(fields)operator(\)) - -comment(# @@INCOMPLETE@@) -comment(# An example using databases is missing) -- cgit v1.2.1