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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:02:17+00:00 2026-05-16T00:02:17+00:00

So I need to run a loop in Ruby to pass some strings into

  • 0

So I need to run a loop in Ruby to pass some strings into SQLite. Basically I have a table that looks like this:

    pID          Data
1649,1650,1651|Some data
1643,3|some more data
23,4,5,6,7|More data

Now in my SQLite queries, I will sometimes need to pass all the pIDs for a given line through as one whole string, which I can easily do with the ‘i’ variable in the loop below:

pID = db.execute( "select distinct pID from pmesh")
pID.each do |i|
end

Where ‘i’ will equal each pID line and each string looks like this:

1649,1650,1651
1643,3
23,4,5,6,7

But sometimes I will need each string to look like this for me to pass them through:

"1649","1650","1651"
"1643","3"
"23,"4","5","6","7"    

Is there any easy loop that I can do that will put quotations and proper commas to make the string appear as I want them to? I know I can do “i = i.to_s() and a = i.split(“,”)”, but I really don’t know much else loop-wise after that.

Thanks,
Bobby

  • 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-16T00:02:17+00:00Added an answer on May 16, 2026 at 12:02 am

    Use Enumerable.collect

    Here’s an example:

    x = [1,2,4,5,6]
    => [1,2,4,5,6]
    x.collect { |i| "#{i}" }
    = > ["1","2","4","5","6"]
    

    or, if you want to join them into one string:

    x.collect { |i| "#{i}" }.join(', ')
    

    or, if your input is a csv string:

       x.split(",").collect { |i| "#{i}" }.join(', ')
    

    Edit:

    Here is an example of what I think you’re trying to do.

     require 'sqlite3'
     db = SQLite3::Database.new("test.db")
     db.execute("create table bobby (pID varchar2(50), Data varchar(100))")
     db.execute("select * from bobby")
     db.execute("insert into bobby values ( ? , ?)", "1649,1650,1651", "Some Data1")
     db.execute("insert into bobby values ( ? , ?)", "1643,3", "Some Data2")
     db.execute("insert into bobby values ( ? , ?)", "23,4,5,6,7", "Some Data3")
     db.execute("select * from bobby").each do |row|
        p
     end
    # outputs [["1649,1650,1651", "Some Data1"], ["1643,3", "Some Data2"], ["23,4,5,6,7", "Some Data3"]]
    
    db.results_as_hash = true #allows hashing row via column name
    
    puts "Each ID as array of strings"
     db.execute("select * from bobby").each do |row|
        p row['pID'].split(",").collect { |i| "#{i}" }
     end
    
    puts "Each ID as single string"
     db.execute("select * from bobby").each do |row|
        p row['pID'].split(",").collect { |i| "#{i}" }.join(", ")
     end
    

    Here is the output:

    C:\Users\Jim\Desktop>ruby sqlLiteExample.rb
    Each ID as array of strings
    ["1649", "1650", "1651"]
    ["1643", "3"]
    ["23", "4", "5", "6", "7"]
    Each ID as single string
    "1649, 1650, 1651"
    "1643, 3"
    "23, 4, 5, 6, 7"
    

    edit2:
    if you want your values to be a string of , for instance, “”1643”, “3””, then you’ll have to do the following:

    x.map{|i| '"' + "#{i}" + '"' }.join(',')
    # outputs "\"124\",\"5525\",\"23525\""
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reclarification on the original question. I need a loop that will run in ruby
I have a nice dataset loop working, but I need to run another loop
I need to run the application that I have made in Xcode on my
Need to run a for loop in jquery. Condition is : i have the
I have few tables on the page and I need to run a loop
I have a fairly long parallel foreach loop I need to run. Over 100
I'm have a Form1 and when I run a loop I need to open
I have the following code that I need to run over a matrix with
I need to run a loop in SQL. Google this is a little hard
I need to run a PHP loop for a total of 100, 000 times

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.