All Questions
7,678 questions
-1
votes
1
answer
94
views
I need help formatting this code as a Ruby method [closed]
So, I have passing code for what I am trying to achieve. Now I need to format it in a method format that takes two parameters. I have more experience with JavaScript functions at this point and am ...
0
votes
1
answer
72
views
Implement a method that takes takes 2 parameters and returns a hash with the amount of times a substring occurred in the string
I am currently learning Ruby through the odin project curriculum. This is the project description:
Implement a method #substrings that takes a word as the first argument and then an array of valid ...
0
votes
1
answer
52
views
How to map values in the array of arrays based on the second value in each array
I have the following data in the array of arrays:
[['apples', 2020], ['pears', 2020], ['apples', 2021], ['melons', 2020], ['peaches', 2021], ['pears', 2019]]
What I want as a result is this:
{ 2019 =&...
2
votes
4
answers
133
views
ruby 3.1.2 how to reference an object property as a method parameter
This may be trivial. I want to reference an object property from within an object method parameter list, if that makes sense.
Trivial example:
'tags'.ljust([calculated_length, obj.length].max)
where, ...
0
votes
1
answer
534
views
Undefined method `name' for an instance of Array (NoMethodError)
I am trying to add an element into an instance of array by using push(<<) to insert variables new_name and new_origin into ingredients array but I am receiving this error: undefined method `name'...
-2
votes
1
answer
100
views
syntax error, unexpected ')', expecting '=' (SyntaxError) [closed]
Letters1 = Array["A","B"]
password1 = nil
while password1 != "A"
letter1 = Letters1[rand(0...2)]
letter2 = Letters1[rand(0...2)]
password1 = (letter1)
File.write(&...
0
votes
1
answer
88
views
How can I incrementally increase a displayed array element using a rails button?
I have two arrays @necklines and @skirts, made up of all Neckline models, and Skirt models respectively.
I am attempting to show one neckline and one skirt at a time, and having two buttons to either ...
0
votes
2
answers
74
views
Generate array based on two others
I have two arrays:
[["106373", "106374", "106375", "106376"], ["106377", "106378", "220183", "220184"], ["220185&...
0
votes
6
answers
121
views
Locate index of mismatch in an array
I have an array that should contain the same value throughout and if there are mismatches, I want to pull the index of those mismatches.
Example:
[1,1,1,1,3,1,1,1,2,1,1,1]
Output:
[4, 8]
What's the ...
1
vote
1
answer
86
views
Iterating over a Nested array in ruby
test_scores = [
[97, 76, 79, 93], [79, 84, 76, 79], [88, 67, 64, 76], [94, 55, 67, 81]
]
puts test_scores.any? do |scores|
scores.all? { |score| score > 80 }
end
When I run the above code on ...
0
votes
2
answers
55
views
Ruby on Rails seems to corrupt a constant in a method
my Constant seems be be modified but how can it be.
the environment is rails 7.0.7.2
Ruby 3.1.2p20 via rbenv
Consider
module ArrayTestMethods
PROFILE_PARAMS = [["id","id"],["...
-1
votes
3
answers
163
views
Ruby complex strings array to array of float
I'm trying to convert this (rather complex) string array of float strings into an array of float with no luck:
[ 5.85142857 6.10684807 6.15328798 6.31582766 6.96598639
7.61614512 7....
2
votes
1
answer
101
views
Ruby #union on Array reacts very strangely
I like to use the Array#union or #| method to return an array of objects where duplicates are removed. I have a custom implementation of eql? on my class.
There is a really strange behaviour if I do ...
1
vote
2
answers
580
views
Matching of randomly ordered array when test nested hash with RSpec
In my RSpec tests I often have the challenge to compare deep nested hashes like this
{ foo: ["test", { bar: [1,2,3] }] }
The values [1,2,3] are read from a DB where the order is not ...
0
votes
5
answers
171
views
How to check if a value exists in the first member of a two dimensional array
I'm trying to check if any of the first value of a two-dimensional array has the value i'm testing. The array is like this
[[2,5],[3,1],[4,1]]
I want to check only the first member of the array (here ...