- 1,224
- Posts
- 11
- Years
- Omnipresence
- Seen Aug 8, 2023
Do you consider speed important, and how much? For instance, I made a word wrapping method, while a regexp method can do almost the same thing (it doesn't seem to wrap properly for me though). My method benchmarks at about 1/1000 of a second, whereas the regexp speed is so minuscule the benchmark shows it as 0. I might just need to reform the regexp function to do what I want (for which I will have to look at more things because it is confusing to me), but I was wondering if there was really a point in this case.
Edit: fixed the regexp method to work correctly, so now the example is null but the question remains
Here's the methods if you want them
RegExp
Other one
Edit: fixed the regexp method to work correctly, so now the example is null but the question remains
Here's the methods if you want them
Spoiler:
RegExp
Code:
#word wrapping
class String
def wordwrap(width)
return self.scan(/\S.{0,#{width-2}}\S(?=\s|$)|\S+/)
end
end
Other one
Code:
def wordwrap (text,width)
textarray=[]
wordsarray=[]
#break text into words
wordsarray=text.scan(/\w+/)
count=0
count2=0
done=false
loop do
textarray[count]=""
for i in count2...wordsarray.length
count2=i
if (textarray[count].length+wordsarray[i].length+1)>=width
done=true if [email protected]
break
else
textarray[count]=textarray[count]+wordsarray[i]+" "
done=true if i==wordsarray.length-1
break if i==wordsarray.length-1
end
end
count+=1
break if done==true
end
return textarray
end
Last edited: