Ruby solution for 3-value datasets

Therefore, I have little knowledge Ruby, but I need to work with a simple script. I will try to explain my dilemma in detail, but let me know if you still need clarification.

My script includes sets of 3 numbers each. Say, for example, we have three information for each person: Age, Size, and Score. So, I need to be able to assess whether a person exists with a certain age and size. If so, then I would like to derive an assessment of this person.

The only way I know to track this information is to create 3 separate arrays, each containing one piece information. I can check if age is included in one array, if so, I can find its index (each value in each category will be unique, so there are no ages , " sizes ," or " ratings "). Then I can check if the value at the same index in the size array matches the specified size. If so, I can infer the estimate found in the third array at the same index. I would rather not do it this way and instead preserve each person’s age , size and value together.

So, I tried arrays in an array like this:

testarray = [
  [27, 8, 92],
  [52, 12, 84]
]

The problem with this, however, is that I'm not sure how to access the values ​​inside these subarrays. So I know that I can use something like testarray.include? (27) to check if 27 is present in the main array, but how to check if 27 and 8 are the first two values ​​in the testarray subarray, and if so, then print the third value subarray, 92.

Then I tried an array of hashes, as shown below:

testarrayb = [
{ :age => 27, :size => 8, :score => 92 },
{ :age => 52, :size => 12, :score => 84 }
]

, testarrayb[0][:score] 92, , , , , , , ? - testarrayb[:age].include?(value), , . , .

, - . , , . !

+5
6

, . Struct. .

Entry = Struct.new(:age, :size, :score) do
  # Holds the data (example)
  def self.entries; @entries ||= [] end

  # Example filtering method
  def self.score_by_age_and_size(age, size)
    entry = entries.find { |e| e.age == age && e.size == size }
    entry.score if entry
  end
end

# Add some entries
Entry.entries << Entry.new(27, 8, 92)
Entry.entries << Entry.new(52, 13, 90)

# Get score
Entry.score_by_age_and_size(27, 8) # => 92
Entry.score_by_age_and_size(27, 34) # => nil
+3

, , .

# return an array of hashes that satisfy the conditions
array_of_hashes = testarrayb.select { |h| h[:age] == 27 && h[:size] == 8 }

# assign the score
score = array_of_hashes[0][:score]

EDIT: , ,

# use an instance variable to reference the initial array defined outside this method
@testarrayb = [{:age=>27, :size=>8, :score=>92}, {:age=>52, :size=>12, :score=>84}]

def find_person(age, size)
  array_of_hashes = @testarrayb.select { |h| h[:age] == age && h[:size] == size }

  # since values in each column are unique we can assume array size of 0 or 1
  # return false if not found otherwise return the score
  !array_of_hashes.empty? && array_of_hashes[0][:score]
end

:

find_person 27, 8
# => 92 

find_person 27, 7
# => false
+2

?

testhash = {27 => {8 => 92}, 52 => {12 => 84}}
p testhash             # {27=>{8=>92}, 52=>{12=>84}}
p testhash[27][8]      # 92
p testhash[27][42]     # nil
testhash[27][42] = 99
p testhash             # {27=>{8=>92, 42=>99}, 52=>{12=>84}}
p testhash[27][42]     # 99

/ , .

+1

, ( " , ", " , , " ), , . Ruby ( ) , :

# `scores` hash is structured { [age, size] => score, ... }
scores = {[27, 8] => 92, [52, 12] => 84}

, :

scores.include?([27, 8])  # => true
scores.include?([27, 7])  # => false
scores.include?([28, 8])  # => false
scores.include?([52, 12]) # => true

( , / ):

scores[[52, 12]] = 97
scores                    # => {[27, 8]=>92, [52, 12]=>97}

, , nil :

score = scores[[27, 8]]   # `score` is now `92`
if !score.nil?
  # Do something with `score`
end

Array , - Hash, :

scores = {{:age => 27, :size => 8} => 92, {:age => 52, :size => 12} => 84}
scores[{:size => 8, :age => 27}] # => 92
+1

- :

[
  { age: 27, size: 8,  score: 92 },
  { age: 52, size: 12, score: 84 }
].map { |e| e[:age] == 27 && e[:size] == 8 ? e[:score] : nil }.compact

map array ( e[:score], nil, . compact, nil array.

, , , , ( ), . , ?

0
def person_score (*input)
    testarray = [
    [27, 8, 92],
    [52, 12, 84]
    ]
        testarray.each_with_object("") do|i,mem| 
            h = Hash[*([:age,:size,:score].zip(i).flatten)]
            (h.values.first(2) == input) ? (return h[:score]) : ( mem.replace("age and size not matched"))
        end
end

p person_score(27,12)  #=> "age and size not matched" 
p person_score(27,8)   #=> 92
p person_score(52,44)  #=> "age and size not matched"
p person_score(52,12)  #=> 84
0

All Articles