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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:51:02+00:00 2026-06-15T00:51:02+00:00

I have the following text to put into some Ruby objects so that I

  • 0

I have the following text to put into some Ruby objects so that I can write them into a DB to be used in a rails app. The data is some output from a wave forecast model, which shows ocean swells at a particular point in the ocean. The first column is the day and hour, then combined swell (not interested in this), followed by individual swells which can vary from 1 to 6 swell present in any one hour.

 +-------+-----------+-----------------+-----------------+-----------------+
 | day & |  Hst  n x |    Hs   Tp  dir |    Hs   Tp  dir |    Hs   Tp  dir |
 |  hour |  (m)  - - |    (m)  (s) (d) |    (m)  (s) (d) |    (m)  (s) (d) |
 +-------+-----------+-----------------+-----------------+-----------------+
 | 15  3 | 0.94  3   |   0.74  4.4  69 |   0.43 10.6 186 |   0.39  4.8 351 |
 | 15  4 | 0.90  3   |   0.71  4.2  68 |   0.43 10.7 186 |   0.34  4.7 347 |
 | 15  5 | 0.85  3   |   0.65  4.1  72 |   0.42 10.7 186 |   0.35  4.4 351 |
 | 15  6 | 0.81  3   |   0.61  4.2  72 |   0.42 10.7 186 |   0.32  4.5 350 |
 | 15  7 | 0.77  2   |                 |   0.41 10.8 186 |                 |
 | 15  8 | 0.73  2   |                 |   0.40 10.8 186 |                 |
 | 15  9 | 0.70  3   |   0.51  3.8  74 |   0.40 10.7 187 |   0.26  4.1 350 |
 | 15 10 | 0.67  3   |   0.49  3.8  73 |   0.39 10.7 187 |   0.24  4.2 349 |
 | 15 11 | 0.65  3   |   0.47  3.7  73 |   0.38 10.7 186 |   0.23  4.1 352 |
 | 15 12 | 0.63  2   |                 |   0.37 10.7 187 |                 |
 | 15 13 | 0.63  2   |                 |   0.35 10.6 187 |                 |

I’m interested in the date, number of swells, and info about each swell. What I’m after is an object that contains the day/hour as the key, and also contains the individual data for each swell. The number of swells will vary for each hour. If I loaded line:

| 15 11 | 0.65  3   |   0.47  3.7  73 |   0.38 10.7 186 |   0.23  4.1 352 |

I’d like to get info out of the object with calls like:

@forecast.date              #=> 15:11
@forecast.numswells         #=> 3 for the total swells present on that date 
@forecast.swell.1.height    #=> 0.47
@forecast.swell.1.direction #=> 73
@forecast.swell.3           #=> a swell object with all info in it for swell 3

I think what I need is an object which has a variable length store of other objects. Is that possible? Any pointers as to what I should be reading up 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-06-15T00:51:03+00:00Added an answer on June 15, 2026 at 12:51 am

    Some suggestions:

    • You should better use swells in plural than swell for a set of things.
    • For things of varying length, Array is the best fit. When referring to a particular element of an Array, you should use the [] method instead of methods like 1, 2, etc. (Probably that is not possible in the first place). In that case, note that you should start from 0, not 1.
    • You can easily get the number of swells by applying the length method on the swells. You should not have a specific method numswells for that unless it will be used particularly frequently.

    I would do something like this:

    data =
    "| 15 11 | 0.65  3   |   0.47  3.7  73 |   0.38 10.7 186 |   0.23  4.1 352 |"
    
    class Forecast
      attr_reader :date, :swells
      def initialize string
        _, date, _, swells = string.split("|", 4)
        @date = date.scan(/\d+/).join(":")
        @swells = swells.scan(/[^\|]+/).select{|s| s =~ /\S/}.map{|s| Swell.new(s)}
      end
    end
    
    class Swell
      attr_reader :height, :tp, :direction
      def initialize string
        @height, @tp, @direction = string.split(/\s+/).drop(1).map(&:to_f)
      end
    end
    
    @forecast = Forecast.new(data)
    p @forecast.date
    p @forecast.swells.length
    p @forecast.swells[0].height
    p @forecast.swells[0].direction
    p @forecast.swells[2]
    
    #=> "15:11"
    #=> 3
    #=> 0.47
    #=> 73.0
    #=> #<Swell:0x000000016401d0 @height=0.23, @tp=4.1, @direction=352.0>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I put some colored text to my rich text box my using the following
Assume that I have some array of data (a vector to be specific). Can
I have the following text inside a javascript string variable: here is some text
Hi I have a text file which contains some numerical data. Of that text
I have the following problem: i have some data loaded in my application, that
I have the following text; country=france name=jean country=germany name=michael country=england name=jack I want it
I have the following text contained within a field in my DB: [quote:5a7b87febe=mr smith]This
I have the following text in a string in the resources file: <a href=mailto:mymail@mail.com>&lt;img
I have the following text in xml file: <Config Builder=LP Wizard> <Libraries> <Library Name=XCAMSource/>
I have the following text, I need to extract the exception name and the

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.