To define a function in Ruby that takes a string and displays the "size" value for a given object in an array, you can use the following code:
main.rb197 chars11 lines
This function display_size
takes two parameters: data
(the array of objects) and string
(the string to match against the "n" key in the objects).
Within the function, we iterate over each object in the data
array using each
. We check if the "n" value of the current object matches the string
parameter. If it does, we then access the "size" value and display the "s" value using puts
.
In the example given, the result of calling display_size(data, "m")
will output 7
.
gistlibby LogSnag