Next 0.2 Simple Ruby program Prev

Simple Ruby program

Print all lines from standard input containing "Ruby":

while gets       # assigns line to $_
    if /Ruby/     # matches against $_
        print      # prints $_
    end
end

A more perl-ish variant:

print if /Ruby/ while gets

A more smalltalk-ish variant:

STDIN.each { |line|  print line  if line =~ /Ruby/ }

Command line one liners:

% ruby -ne 'print if /Ruby/'
% ruby -pe '$_ = "" unless /Ruby/'
% ruby -e 'print STDIN.grep /Ruby/'

Next Ruby for Perl programmers Prev