Skip to content

Instantly share code, notes, and snippets.

@jhammann
Last active December 16, 2015 01:18
Show Gist options
  • Save jhammann/5353556 to your computer and use it in GitHub Desktop.
Save jhammann/5353556 to your computer and use it in GitHub Desktop.
A helper that checks the length of a string and which adds three dots if that string exceeds a given length.
module TextLengthHelper
def text_length(string, max_length)
return string if string.length <= max_length
string[0..(max_length - 1)] + "..."
end
end
require_relative 'text_length_helper'
describe TextLengthHelper do
include TextLengthHelper
full_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
short_text = "Lorem ipsum dolor sit amet, consectetur adipiscing..."
it "should stay the same when a text is shorter than 100" do
text_length(full_text, 100).should eq full_text
end
it "should return a text with dots given a string longer than 50" do
text_length(full_text, 50).should eq short_text
end
it "should not have dots when given an empty string" do
text_length("", 50).should eq ""
end
end
@DanielZwijnenburg
Copy link

nog wat compacter :)

def text_length(string, max_length)
  return string if string.length <= max_length
  string[0..(max_length - 1)] + "..."
end

@jhammann
Copy link
Author

Tnx, Ik pas hem aan!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment