Advertisement
hikanh

Untitled

Oct 23rd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. class String
  2. def starts_with?(str)
  3. return false if self.length < str.length
  4. return true if self[0, str.length] == str
  5. return false
  6. end
  7.  
  8. def ends_with?(str)
  9. return self.reverse.starts_with?(str.reverse)
  10. end
  11. end
  12.  
  13. Example:
  14. "hello".starts_with?("hell") # => Returns true
  15. "hello world!".ends_with?("world") # => Returns false (because it has an exclamation point, for which I'm not checking)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement