Only show first letters of string of each word
"Rock Paper Shotgun".split.map(&:first).join.upcase
edit:
irb:
[4] pry(main)> "rock Paper Shotgun".split.map(&:first).join.upcase
=> "RPS"
Truncate string to the first n words
n = 3
str = "your long long input string or whatever"
str.split[0...n].join(' ')
=> "your long long"
str.split[0...n] # note that there are three dots, which excludes n
=> ["your", "long", "long"]
HTML title attribute in Rails displaying only first word
I think you need to add quoting to the HTML in the view. If you view source on the generated HTML, I'm willing to bet it looks like this:
<div title=This is a super cool item! >
The browser will interpret that as the div
having a title
with the value This
, and then attributes named is
, a
, super
, cool
, and item!
.
If you change your view to this:
<div title="<%= item.description %>" >
Then your generated HTML should look like this:
<div title="This is a super cool item!" >
How to display the first 10 characters only of a message stored in database?
Try this:
truncate(message.body, :length => 10)
Ruby: How to get the first character of a string
You can use Ruby's open classes to make your code much more readable. For instance, this:
class String
def initial
self[0,1]
end
end
will allow you to use the initial
method on any string. So if you have the following variables:
last_name = "Smith"
first_name = "John"
Then you can get the initials very cleanly and readably:
puts first_name.initial # prints J
puts last_name.initial # prints S
The other method mentioned here doesn't work on Ruby 1.8 (not that you should be using 1.8 anymore anyway!--but when this answer was posted it was still quite common):
puts 'Smith'[0] # prints 83
Of course, if you're not doing it on a regular basis, then defining the method might be overkill, and you could just do it directly:
puts last_name[0,1]
Get first N characters from string without cutting the whole words
s = "Coca-Cola is the most popular and biggest-selling soft drink in history, as well as the best-known brand in the world."
s = s.split(" ").each_with_object("") {|x,ob| break ob unless (ob.length + " ".length + x.length <= 70);ob << (" " + x)}.strip
#=> "Coca-Cola is the most popular and biggest-selling soft drink in"
Get substring after the first = symbol in Ruby
Not exactly .after
, but quite close to:
string.partition('=').last
http://www.ruby-doc.org/core-1.9.3/String.html#method-i-partition
Show only some words from post as preview
Know the builtin tools.
Ruby - How to select some characters from string
Try foo[0...100]
, any range will do. Ranges can also go negative. It is well explained in the documentation of Ruby.
Related Topics
Is Time.Zone.Now.To_Date Equivalent to Date.Today
Ruby Get Time in Given Timezone
How to Get a List of Files That Have Been 'Required' in Ruby
Change Error Field Name in Rails
Why Do I Get "No Implicit Conversion of String into Integer (Typeerror)"
Concurrent Requests with Mri Ruby
What Does the Operator ||= Stand for in Ruby
CSV - Unquoted Fields Do Not Allow \R or \N (Line 2)
Is Alias_Method_Chain Synonymous with Alias_Method
How to Install 'Cocoapods' Gem from Rubygems.Org (Bad Response Backend Read Error)
How to Get the Current Route in Rails
How to Run Phantomjs on Heroku
Devise Skip_Confirmation! Fails to Avoid to Send the Confirmation Instructions
Rvm Install: Ruby Installation Error
Trouble Resizing the Default Image with Paperclip
How to Use Ruby Metaprogramming to Add Callbacks to a Rails Model