Using rails 4, and having trouble finding documentation on this. I would like to capitalize the first letter of each word in a string but keep already capitalized letters.
I would like the following outputs:
how far is McDonald's from here? => How Far Is McDonald's From Here?
MDMA is also known as molly => MDMA Is Also Known As Molly
i drive a BMW => I Drive A BMW
I thought .titleize would do it, but that will turn BMW into Bmw. Thank you for any help.
You can try the following:
a.split.map{|x| x.slice(0, 1).capitalize + x.slice(1..-1)}.join(' ')
# or
a.split.map{|x| x[0].upcase + x[1..-1]}.join(' ')
#=> ["MDMA Is Also Known As Molly",
"How Far Is McDonald's From Here?",
"I Drive A BMW"]
Demonstration
You can do a custom method like this:
string = "your string IS here"
output = []
string.split(' ').each do |word|
if word =~ /[A-Z]/
output << word
else
output << word.capitalize
end
end
output.join(' ')
Of course, this will not change a word like "tEST" or "tEst" because it has at least one capital letter in it.
to capitalize only the first letter while preserving existing capitalization:
your_string.then { |s| s[0].upcase + s[1..-1] }
Related
Is it possible to set a conditional statement (IF statement) comparing a variable against a variable that iterates through the values inside an array? I was looking for something like:
array_of_small_words = ["and","or","be","the","of","to","in"]
if word == array_of_small_words.each
# do thing
else
# do another thing
end
Basically, I want to capitalize each word but don't want to do it for "small words". I know I could do the the opposite and iterate through the array first and then compare each iteration with the word but I was hoping there would be a more efficient way.
sentence = ["this","is","a","sample","of","a","title"]
array_of_small_words = ["and","or","be","the","of","to","in"]
sentence.each do |word|
array_of_small_words.each do |small_words|
if word == small_words
# don't capitalize word
else
# capitalize word
end
end
end
I'm not really sure if this is possible or if there is a better way of doing this?
Thank you!
sentence = ["this","is","a","sample","of","a","title"]
array_of_small_words = ["and","or","be","the","of","to","in"]
sentence.map do |word|
array_of_small_words.include?(word) ? word : word.upcase
end
#⇒ ["THIS", "IS", "A", "SAMPLE", "of", "A", "TITLE"]
What you're looking for is if array_of_small_words.include?(word).
This should be faster than #mudasobwa's repeated use of include? if packaged in a method and used frequency. It would not be faster, however, if mudsie used a set lookup (a minor change, of which he is well-aware), as I mentioned in a comment. If efficiency is important, I'd prefer mudsie's way with the set mod over my answer. In a way I was just playing around below.
I've assumed he small words are and, or, be, the, of, to, in and notwithstanding.
SMALL_WORDS = %w| and or be the of to in notwithstanding |
#=> ["and", "or", "be", "the", "of", "to", "in", "notwithstanding"]
(SMALL_WORDS_HASH = SMALL_WORDS.map { |w| [w.upcase, w] }.to_h).
default_proc = proc { |h,k| h[k]=k }
Test:
SMALL_WORDS_HASH
#=> {"AND"=>"and", "OR"=>"or", "BE"=>"be", "THE"=>"the", "OF"=>"of",
# "TO"=>"to", "IN"=>"in", "NOTWITHSTANDING"=>"notwithstanding"}
SMALL_WORDS_HASH["TO"]
#=> "of"
SMALL_WORDS_HASH["HIPPO"]
#=> "HIPPO"
def convert(arr)
arr.join(' ').upcase.gsub(/\w+/, SMALL_WORDS_HASH)
end
convert ["this","is","a","sample","of","a","title"]
#=> "THIS IS A SAMPLE of A TITLE"
I am having trouble writing this so that it will take a sentence as an argument and perform the translation on each word without affecting the punctuation.
I'd also like to continue using the partition method.
It would be nice if I could have it keep a quote together as well, such as:
"I said this", I said.
would be:
"I aidsay histay", I said.
def convert_sentence_pig_latin(sentence)
p split_sentence = sentence.split(/\W/)
pig_latin_sentence = []
split_sentence.each do |word|
if word.match(/^[^aeiou]+/x)
pig_latin_sentence << word.partition(/^[^aeiou]+/x)[2] + word.partition(/^[^aeiou]+/x)[1] + "ay"
else
pig_latin_sentence << word
end
end
rejoined_pig_sentence = pig_latin_sentence.join(" ").downcase + "."
p rejoined_pig_sentence.capitalize
end
convert_sentence_pig_latin("Mary had a little lamb.")
Your main problem is that [^aeiou] matches every character outside that range, including spaces, commas, quotation marks, etc.
If I were you, I'd use a positive match for consonants, ie. [b-df-hj-np-tv-z] I would also put that regex in a variable, so you're not having to repeat it three times.
Also, in case you're interested, there's a way to make your convert_sentence_pig_latin method a single gsub and it will do the whole sentence in one pass.
Update
...because you asked...
sentence.gsub( /\b([b-df-hj-np-tv-z])(\w+)/i ) { "#{$2}#{$1}ay" }
# iterate over and replace regexp matches using gsub
def convert_sentence_pig_latin2(sentence)
r = /^[^aeiou]+/i
sentence.gsub(/"([^"]*)"/m) {|x| x.gsub(/\w+/) {|y| y =~ r ? "#{y.partition(r)[2]}#{y.partition(r)[1]}ay" : y}}
end
puts convert_sentence_pig_latin2('"I said this", I said.')
# define instance method: String#to_pl
class String
R = Regexp.new '^[^aeiou]+', true # => /^[^aeiou]+/i
def to_pl
self.gsub(/"([^"]*)"/m) {|x| x.gsub(/\w+/) {|y| y =~ R ? "#{y.partition(R)[2]}#{y.partition(R)[1]}ay" : y}}
end
end
puts '"I said this", I said.'.to_pl
sources:
http://www.ruby-doc.org/core-2.1.0/Regexp.html
http://ruby-doc.org/core-2.0/String.html#method-i-gsub
I have the following method in my Ruby model:
Old:
def to_s
numbers = self.title.scan(/\d+/) if self.title.scan(/\d+/)
return numbers.join.insert(0, "#{self.title.chop} ") if numbers
"#{self.title.titlecase}"
end
New:
def to_s
numbers = self.title.scan(/\d+/)
return numbers.join.insert(0, "#{self.title.sub(/\d+/, '')} ") if numbers.any?
self.title.titlecase
end
A title can be like so: Level1 or TrackStar
So TrackStar should become Track Star and Level1 should be come Level 1, which is why I am doing the scan for numbers to begin with
I am trying to display it like Level 1. The above works, I was just curious to know if there was a more eloquent solution
Try this:
def to_s
self.title.split(/(?=[0-9])/, 2).join(" ")
end
The second argument to split is to make sure a title like "Level10" doesn't get transformed into "Level 1 0".
Edit - to add spaces between words as well, I'd use gsub:
def to_s
self.title.gsub(/([a-z])([A-Z])/, '\1 \2').split(/(?=\d)/, 2).join(" ")
end
Be sure to use single-quotes in the second argument to gsub.
How about this:
'Level1'.split(/(\d+)/).join(' ')
#=> "Level 1"
Pluralizing a single word is simple:
pluralize(#total_users, "user")
But what if I want to print "There is/are N user/users":
There are 0 users
There is 1 user
There are 2 users
, i.e., how to pluralize a sentence?
You can add a custom inflection for it. By default, Rails will add an inflections.rb to config/initializers. There you can add:
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular "is", "are"
end
You will then be able to use pluralize(#total_users, "is") to return is/are using the same rules as user/users.
EDIT: You clarified the question on how to pluralize a sentence. This is much more difficult to do generically, but if you want to do it, you'll have to dive into NLP.
As the comment suggests, you could do something with I18n if you just want to do it with a few sentences, you could build something like this:
def pluralize_sentence(count, i18n_id, plural_i18n_id = nil)
if count == 1
I18n.t(i18n_id, :count => count)
else
I18n.t(plural_i18n_id || (i18n_id + "_plural"), :count => count)
end
end
pluralize_sentence(#total_users, "user_count")
And in config/locales/en.yml:
en:
user_count: "There is %{count} user."
user_count_plural: "There are %{count} users."
This is probably best covered by the Rails i18n pluralization features. Adapted from http://guides.rubyonrails.org/i18n.html#pluralization
I18n.backend.store_translations :en, :user_msg => {
:one => 'There is 1 user',
:other => 'There are %{count} users'
}
I18n.translate :user_msg, :count => 2
# => 'There are 2 users'
I think the first part of Martin Gordon's answer is pretty good.
Alternatively, it's kind of messy but you can always just write the logic yourself:
"There #{#users.size == 1 ? 'is' : 'are'} #{#users.size} user#{'s' unless #users.size == 1}."
UPDATE to code: I no longer use the inflections route as stated in #Martin Gordon's answer. For some reason it would cause other non-related functions to error. I did extensive tests to confirm, though could not track down a reason why. So, below is now what I use and it works.
There are many ways to do this. This is how I did it using Rails 6.0.3.4 and Ruby 2.7.1.
I wanted to pluralize this sentence:
Singular: There is 1 private group
Plural: There are 2 private groups
What I did is I went to application_helper.rb and added this code:
def pluralize_private_statement(list, word)
num_in_list = list.count
is_or_are = num_in_list == 1 ? 'is' : 'are'
return "There " + is_or_are + " " + num_in_list.to_s + " private " + word.pluralize(num_in_list)
end
Now, all I have to use in my view is:
<%= pluralize_private_statement(private_groups, "group") %>
# private_groups = 2
# -> There are 2 private groups
What the code in application_helper.rb does is first create a variable for the number of items in the list passed and store it in num_in_list. Then it creates a second varible checking if the num_in_list is equal to 1 and if so returns 'is' otherwise it returns 'are'. Then, it returns the sentence that is constructed with the information obtained.
The first part of the sentence is a simple string, then the is_or_are variable which holds either 'is' or 'are' as explained above. Then it adds a space with the number of list items, converted from an integer to a string, followed by the 'private' word. Then it adds the pluralization of the word passed to the initial function; but, only returns the singular/plural word without a number attached as pluralize(#total_users, "is") would do.
Here is how you could use it for your specific question.
First, add this to your application_helper.rb file:
def pluralize_sentence(list, word)
num_in_list = list.count
is_or_are = num_in_list == 1 ? 'is' : 'are'
return "There " + is_or_are + " " + num_in_list.to_s + " " + word.pluralize(num_in_list)
end
Lastly, you can use this code wherever you wish to have the pluralized sentence:
<%= pluralize_sentence(#total_users, "user") %>
Happy Coding!
What's the best way in Ruby (with Rails, if relevant) to capitalize the first letter of a string?
Note that String#capitalize is not what I want since, in addition to capitalizing the first letter of the string, this function makes all other characters lowercase (which I don't want -- I'd like to leave them untouched):
>> "a A".capitalize
=> "A a"
In Rails you have the String#titleize method:
"testing string titleize method".titleize #=> "Testing String Titleize Method"
You can use "sub" to get what you want (note: I haven't tested this with multibyte strings)
"a A".sub(/^(\w)/) {|s| s.capitalize}
(and you can of course monkeypatch String to add this as a method if you like)
Upper case the first char, and save it back into the string
s = "a A"
s[0] = s[0,1].upcase
p s # => "A A"
Or,
class String
def ucfirst!
self[0] = self[0,1].upcase
self
end
end
If you don't want to modify the original string, you can do it this way:
class String
def ucfirst
str = self.clone
str[0] = str[0,1].upcase
str
end
end
I propose the following solution, works through whitespace
' ucfirstThis'.sub(/\w/, &:capitalize)
# => "UcfirstThis"
Since rails 5:
"a A".upcase_first
=> "A A"
http://api.rubyonrails.org/v5.1/classes/ActiveSupport/Inflector.html#method-i-upcase_first
If you are looking for a real similar function to PHPs ucfirst() try
"a A".gsub(/(\w+)/) {|s| s.capitalize}
will result in "A A".
"a neW APPROACH".gsub(/(\w+)/) {|s| s.capitalize}
will result in "A New Approach".
You can extend String class with:
class String
def ucfirst
self.gsub(/(\w+)/) { |s| s.capitalize }
end
def ucfirst!
self.gsub!(/(\w+)/) { |s| s.capitalize }
end
end
Have a look at this.
capitalizing-first-letter-of-each-word
There's not an inbuilt function. You need to split the letters and rejoin or try Rails' String#titleize and see if it does what you want.
That one liner does not depend on ActiveSupport. Not sure it's totally bulletproof though:
"my great uncle and grand-ma".gsub(/(\A\w|\s\w)/) { |m| m.upcase }
# My Great Uncle And Grand-ma