I was looking for a ruby method in my Ruby on Rails project which can truncate a string, say after 50 characters.
I found 'truncate' method, which is a rails api method for truncating string in views.
So if have string
str = "Hi, this is Gourav Tiwari, how are you??" # total 40 characters
the truncate method can be used in the ciew like this:
truncate(str, :length => 25, :ommision => " - - -")
and the result would be :
"Hi, this is Gourav Tiwari, - - -"
In ruby I can accomplish same by slice and concatenation:
myproject>ruby script\console
>> str = "Hi, this is Gourav Tiwari, how are you??"
=> "Hi, this is Gourav Tiwari, how are you??"
>> str.slice(0..25)
=> "Hi, this is Gourav Tiwari,"
>> str.slice(0..25) + " - - -"
=> "Hi, this is Gourav Tiwari, - - -"
Very simple!
I found 'truncate' method, which is a rails api method for truncating string in views.
So if have string
str = "Hi, this is Gourav Tiwari, how are you??" # total 40 characters
the truncate method can be used in the ciew like this:
truncate(str, :length => 25, :ommision => " - - -")
and the result would be :
"Hi, this is Gourav Tiwari, - - -"
In ruby I can accomplish same by slice and concatenation:
myproject>ruby script\console
>> str = "Hi, this is Gourav Tiwari, how are you??"
=> "Hi, this is Gourav Tiwari, how are you??"
>> str.slice(0..25)
=> "Hi, this is Gourav Tiwari,"
>> str.slice(0..25) + " - - -"
=> "Hi, this is Gourav Tiwari, - - -"
Very simple!