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 7662017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:41:40+00:00 2026-05-31T13:41:40+00:00

I came across a ternary in some code and I am having trouble understanding

  • 0

I came across a ternary in some code and I am having trouble understanding the conditional:

str.split(/',\s*'/).map do |match|
  match[0] == ?, ?
    match : "some string"
end.join

I do understand that I am splitting a string at certain points and converting the total result to an array, and dealing with each element of the array in turn. Beyond that I have no idea what’s going on.

  • 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-31T13:41:42+00:00Added an answer on May 31, 2026 at 1:41 pm

    A (slightly) less confusing way to write this is:

    str.split(/',\s*'/).map do |match|
      if match[0] == ?,
        match
      else
        "some string"
      end
    end.join
    

    I think multiline ternary statements are horrible, especially since if blocks can return in Ruby.

    Probably the most confusing thing here is the ?, which is a character literal. In Ruby 1.8 this means the ASCII value of the character (in this case 44), in Ruby 1.9 this is just a string (in this case ",").

    The reason for using a character literal instead of just "," is that the return value of calling [] on a string changed in Ruby 1.9. In 1.8 it returned the ASCII value of the character at that position, in 1.9 it returns a single-character string. Using ?, here avoids having to worry about the differences in String#[] between Ruby 1.8 & 1.9.

    Ultimately the conditional is just checking if the first character in match is ,, and if so it keeps the value the same, else it sets it to "some string".

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Came across this conditional in some uncommented Objective-C code: if (w & (w -
I was reviewing some code and I came across this: public static doSomething(String myString,
I came across some java code for doing a prefix search on a JList.
I came across this in some example code: - (IBAction) startPlayLowNote:(id)sender { UInt32 noteNum
I came across this strange looking declaration in some code early this morning (before
I came across code written by somebody which caught some exception and tried logging
I came across a controller in an older set of code (Rails 1.2.3) that
I came across this class while reading a C# book and have some questions.
I came across this snippet of code on MSDN: entityBuilder.Metadata = @res://*/AdventureWorksModel.csdl| res://*/AdventureWorksModel.ssdl| res://*/AdventureWorksModel.msl;
I came across this code in ShareKit, and I don't understand what the writer

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.