Skip to content

Instantly share code, notes, and snippets.

@jswanner
Created May 24, 2018 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jswanner/93d64a8c3463905466be554153539b54 to your computer and use it in GitHub Desktop.
Save jswanner/93d64a8c3463905466be554153539b54 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
require 'bundler/inline'
gemfile do
gem 'rspec', '3.7', require: false
end
class Program
def initialize(input)
@input = input
end
def call
"%8d%8d%8d" % [lines, words, characters]
end
def characters
@input.size
end
def lines
@input.split(/\n/).size
end
def words
@input.split(/\s+/).size
end
end
if $PROGRAM_NAME == __FILE__
puts Program.new(STDIN.read).call
exit
end
require 'rspec'
RSpec.describe Program do
subject(:program) { described_class.new(input) }
let(:input) { "line 1\nline 2\nline 3\n" }
it { expect(program.call).to eq(' 3 6 21') }
it { expect(program.characters).to eq(21) }
it { expect(program.lines).to eq(3) }
it { expect(program.words).to eq(6) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment