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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:15:41+00:00 2026-05-29T07:15:41+00:00

i want to following select tag <select> <option value=10> 10% </option> <option value=20> 20%

  • 0

i want to following select tag

<select>
<option value=10> 10% </option>
<option value=20> 20% </option>
<option value=30> 30% </option>
</select>

using

 ((0..10).to_a.collect {|r| ["#{r*10} %", r*10] })

will give structure like

[["0 %", 0], ["10 %", 10], ["20 %", 20], ["30 %", 30]]

i wonder if this structure can be converted to something like this

[#<ratio id: 10, name: "10%">, #<id: 20, name:"20%">]

how to convert first structure to second strcuture

so i could use

options_from_collection_for_select(@arr,"id","name")

to populate the select_tag
or

is there any better way to do the same?

comments please.

  • 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-29T07:15:43+00:00Added an answer on May 29, 2026 at 7:15 am

    You can try to use OpenStruct for this.

    An OpenStruct is a data structure, similar to a Hash, that allows the
    definition of arbitrary attributes with their accompanying values.
    This is accomplished by using Ruby’s metaprogramming to define methods
    on the class itself.

    require 'ostruct'
    (0..10).map { |i| OpenStruct.new(:id => i*10, :name => "#{i*10}%") }
    # => [#<OpenStruct id=0, name="0%">, #<OpenStruct id=10, name="10%">, ...
    

    But if you need a real class, not anonymous class like OpenStruct, then just create this class instances inside the collect/map method:

    class Ratio
        attr_reader :id, :name
        def initialize(id, name)
            @id = id; @name = name
        end
    end
    (0..10).map { |i| ratio = i*10; Ratio.new(ratio, "#{ratio}%") }
    # => [#<Ratio: @id=0, @name="0%">, #<Ratio: @id=10, @name="10%">, ...
    

    Maybe I misunderstood you a bit and you want to create this structure not from range, but from array [["0 %", 0], ["10 %", 10], ["20 %", 20], ["30 %", 30]] then the code is similar:

    require 'ostruct'
    arr = [["0 %", 0], ["10 %", 10], ["20 %", 20], ["30 %", 30]]
    p arr.map { |name, id| OpenStruct.new(:id => id, :name => name) }
    # => [#<OpenStruct id=0, name="0 %">, #<OpenStruct id=10, name="10 %">, #<OpenStruct id=20, name="20 %">, #<OpenStruct id=30, name="30 %">]
    

    UPDATE
    I found one corner case in usage of OpenStruct (looks like this is the case for Ruby 1.8.7 only): OpenStruct#id will return object_id for id instead of field value:

    ruby-1.8.7-p352 > o = OpenStruct.new :id => 10
     => #<OpenStruct id=10> 
    ruby-1.8.7-p352 > o.id
    (irb):4: warning: Object#id will be deprecated; use Object#object_id
     => 70021843187380 
    

    There are two workarounds:

    # 1. redefine OpenStruct#id method: 
    OpenStruct.__send__(:define_method, :id) { @table[:id] }
    
    # 2. dont use 'id' as a value and use any other value:
    require 'ostruct'
    (0..10).map { |i| OpenStruct.new(:value => i*10, :name => "#{i*10}%") } # 'value' instead of 'id'
    
    ...
    options_from_collection_for_select(@arr, 'value', 'name')
    

    I recommend you to use second approach.

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

Sidebar

Related Questions

I am using a g:select tag like this: <td><g:select name=newCity id=${'newCity_' +cityData.uid} from=${cityData.name} value=${cityData.someValue}
I have the following code: <select> <option value=blablabla>Test</option> <!-- ... --> </select> But I
Have the following select giving me the data I want, but I want to
i have the following select box and want to sort it by display:none inlne
I want to do the following query: SELECT getDistance(lat1, lng1, lat2, lng2) as `distance`,
I have the following query: SELECT carBrand, carYear, carModel FROM Cars; What I want
I want to build the following pseudo query Select a From APDU a where
I want to translate following simple sql query into Linq to NHibernate: SELECT NewsId
I want to create the following T-SQL statement: SELECT SUM (sa.Amount) as 'SumAmount', SUM(sa.Cost)
I want to convert the following SQL Query to a SubSonic Query. SELECT [dbo].[tbl_Agency].[ParentCompanyID]

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.