From 848ccfb74e55df32c5731b40cc62f54e0e14bc51 Mon Sep 17 00:00:00 2001 From: Krzysztof Karski Date: Thu, 4 May 2017 17:50:31 +0200 Subject: [PATCH] Proper quine without cheating. --- lib/job_interview/quine.rb | 55 +++++++++++++++++++++++++++++++++----- spec/quine_spec.rb | 14 ++++------ 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/lib/job_interview/quine.rb b/lib/job_interview/quine.rb index 00ccad4..9793add 100644 --- a/lib/job_interview/quine.rb +++ b/lib/job_interview/quine.rb @@ -1,10 +1,53 @@ module JobInterview module Quine - - def quine(file = nil) - file ||= caller.first.split(':').first - return File.read(file) + QUOT = 39.chr + SOURCE = [ + 'module JobInterview', + ' module Quine', + ' QUOT = 39.chr', + ' SOURCE = [', + ' ', + ' ].freeze', + '', + ' def quine', + ' str = String.new', + '', + ' 0.upto(3) do |i|', + ' str << "#{SOURCE[i]}\n"', + ' end', + '', + ' 0.upto(SOURCE.length - 1) do |i|', + ' str << "#{SOURCE[4]}#{QUOT}#{SOURCE[i]}#{QUOT},\n"', + ' end', + '', + ' 5.upto(SOURCE.length - 1) do |i|', + ' str << "#{SOURCE[i]}\n"', + ' end', + '', + ' str', + ' end', + '', + ' end', + 'end', + ].freeze + + def quine + str = String.new + + 0.upto(3) do |i| + str << "#{SOURCE[i]}\n" + end + + 0.upto(SOURCE.length - 1) do |i| + str << "#{SOURCE[4]}#{QUOT}#{SOURCE[i]}#{QUOT},\n" + end + + 5.upto(SOURCE.length - 1) do |i| + str << "#{SOURCE[i]}\n" + end + + str end - + end -end \ No newline at end of file +end diff --git a/spec/quine_spec.rb b/spec/quine_spec.rb index dd9b8a3..14bb5af 100644 --- a/spec/quine_spec.rb +++ b/spec/quine_spec.rb @@ -1,19 +1,15 @@ -require "spec_helper.rb" +require 'spec_helper.rb' + module QuineSpec - describe "When called" do + describe 'When called' do before(:each) do @answer = JobInterview::Answer.new end - it "should return the source of the calling file" do - @answer.quine(__FILE__).should == File.read(__FILE__) - end - - it "should find the calling file if not given" do - @answer.quine.should == File.read(__FILE__) + it 'should return its own source code' do + @answer.quine.should == File.read(File.join(Dir.pwd, 'lib', 'job_interview', 'quine.rb')) end end - end