From c477e90c452a3a689a23bd99a11f7cf2338f63f4 Mon Sep 17 00:00:00 2001 From: chhavikirtani2000 <55941961+chhavikirtani2000@users.noreply.github.com> Date: Sat, 28 Mar 2020 03:20:44 +0530 Subject: [PATCH] implemented start_with along with tests --- lib/parfait/word.rb | 19 +++++++++++++++++-- test/parfait/test_word2.rb | 5 +++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/parfait/word.rb b/lib/parfait/word.rb index f9ea7e25..e8e4804f 100644 --- a/lib/parfait/word.rb +++ b/lib/parfait/word.rb @@ -13,7 +13,8 @@ module Parfait # Object length is measured in non-type cells though class Word < Data8 - attr_reader :char_length , :next_word + attr_reader :char_length , :str, :prefix ,:next_word + def self.type_length 3 # 0 type , 1 char_length , next_word @@ -27,6 +28,8 @@ module Parfait def initialize( len ) super() @char_length = 0 + #@str=str1 + #@prefix=prefix1 raise "Must init with int, not #{len.class}" unless len.kind_of? ::Integer raise "Must init with positive, not #{len}" if len < 0 fill_to( len , 32 ) unless len == 0 #32 being ascii space @@ -189,7 +192,19 @@ module Parfait def padded_length Object.padded( 4 * get_type().instance_length + @char_length ) end - + def start_with(str1,prefix1) + @str=str1 + @prefix=prefix1 + s = @str.size() + temp="" + for i in 0..s-1 + temp=temp+@str[i] + if temp==@prefix + return true + end + end + return false + end private def check_length raise "Length out of bounds #{char_length}" if @char_length > 1000 diff --git a/test/parfait/test_word2.rb b/test/parfait/test_word2.rb index fc8e2e23..de533dac 100644 --- a/test/parfait/test_word2.rb +++ b/test/parfait/test_word2.rb @@ -6,6 +6,11 @@ module Parfait super @word = Parfait::Word.new(5) end + def test_start_with + assert_equal true , @word.start_with("hello","hell") + assert_equal true , @word.start_with("Adbfgsj","Adbf") + assert_equal false , @word.start_with("Vanila","van") + end def test_len assert_equal 5 , @word.length end