Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3429584
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:05:58+00:00 2026-05-18T07:05:58+00:00

One of the reasons I like writing in Ruby so much is because it

  • 0

One of the reasons I like writing in Ruby so much is because it is very capable of achieving a lot via one liners.

I like:

@sentence = @sentence.split(' ').map!{|x| x = x[0..0].upcase << x[1..-1] }.join(' ')

It capitalizes the first letter of each word, which is not the most stunning, but quite efficient.

What’s the most elegant one liner you have seen or written with Ruby?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:05:59+00:00Added an answer on May 18, 2026 at 7:05 am

    You posted the oneliner:

    @sentence = @sentence.split(' ').map!{|x| x = x[0..0].upcase << x[1..-1] }.join(' ')
    

    But you misunderstand a couple things about Ruby.

    First off, saying x= in a map call doesn’t affect anything outside of the map call. The new value of that array element is the value returned by the block.

    Secondly, you don’t need to mutate the first string with << — a concatenation that creates a new string (using +) is a much better option.

    Thirdly, map! makes changes directly to the array returned by split. What you want to do is leave that array untouched (even though it doesn’t change program semantics), and return a new array with the mapped results, by using map.

    So you should have written:

    @sentence = @sentence.split(' ').map{|x| x[0..0].upcase + x[1..-1] }.join(' ')
    

    Now the statement x[0..0].upcase + x[1..-1] is still very obtuse. Maybe you should replace it with the capitalize method, which already changes the first letter of the string to uppercase. (Note that it also downcases all of the others, which may or may not be what you want.)

    @sentence = @sentence.split(' ').map{|x| x.capitalize }.join(' ')
    

    I’d probably use gsub! to change parts of the string in-place, but that’s probably a little obtuse:

    @sentence.gsub!(/\S+/){|x| x.capitalize}
    

    One more thing. If the only purpose of a block is to call a single method on the object passed to the block, you can do the following:

    @sentence = @sentence.split(' ').map(&:capitalize).join(' ')
    

    or to modify my gsub! version:

    @sentence.gsub!(/\S+/,&:capitalize)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I like Eclipse for a number of reasons. One of them is, when I'm
For various reasons, we are writing a new business objects/data storage library. One of
One of the reasons that I tend to dread writing Javascript for anything other
For a few different reasons one of my projects is hosted on a shared
There are many security reasons why one would want to drop an HTTP connection
In one's server, file_get_contents is disabled for security reasons. I need to retrieve xml
I've heard in a few places that one of the main reasons why distributed
I am writing a set of django apps and would like to use Hg
This is possibly a candidate for a one-line answer. I would like know it
I am writing ruby on rails application, which will have 2 different users types

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.