Skip to content

Instantly share code, notes, and snippets.

@jswanner jswanner/wc.rb
Created May 24, 2018

Embed
What would you like to do?
#! /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
You can’t perform that action at this time.