Truncate string with Ruby/Rails


Ruby:-

1.9.3p125 :079 >   list = “Hi, this is Ruby on rails developer”
=> “Hi, this is Ruby on rails developer”
1.9.3p125 :080 > list.truncate(10)
=> “Hi, thi…”
1.9.3p125 :081 > list.truncate(20)
=> “Hi, this is Ruby …”
1.9.3p125 :083 > list[0..10]
=> “Hi, this is”
1.9.3p125 :084 > list[0..20]
=> “Hi, this is Ruby on r”

1.9.3p125 :108 >   list.slice(0 , 10)
=> “Hi, this i”

Rails:-

– @organization_applied_name.each do |each_org_name|
%li
= link_to “#{truncate(each_org_name, :length => 25)}”, “#”
= link_to “#{each_org_name[0..10]}”, “#”

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s