Questions tagged [ruby]

Ruby is a multi-platform, open-source, dynamic, object-oriented, interpreted language created by Yukihiro Matsumoto (Matz) in 1993.

Filter by
Sorted by
Tagged with
1 vote
0 answers
32 views

Open a file, with a fallback path if it doesn't exist at one location

I am parsing a file that exists in either app/components/ui or in app/components. If the one in the first dir (...
  • 246
0 votes
0 answers
31 views

Testing out whether controller response body contains correct data in Rspec Controller specs

Running Rails 7.0.3, Ruby 3.1.2 and rspec-rails 6.0.0rc1 I'm writing out some specs to test for the inclusion of certain data getting sent by my controllers. A ...
  • 185
2 votes
0 answers
50 views

Parse a document and returning uniqueness and total number occurrences

I am parsing a document and returning (1) uniqueness, and (2) total number occurrence. I believe my code can still be subjected to review. I would like a review of my code as I have even made it too ...
3 votes
2 answers
217 views

battleship game with two ships per player

I'm creating a battleship game in Ruby. Let me explain how it works: The game is played on 5x5 grids (one per player). Players have 2 ships each to place on their grid. A small ship (3x1 side) and a ...
  • 31
2 votes
0 answers
63 views

Ruby OOP Mastermind Game Code Refactor

I have finished The mastermind game. Basically, it's a game that has 2 sides, codebreaker or codemaker. If you are a codebreaker, you are guessing the colour code that the computer has chosen. If you ...
4 votes
2 answers
96 views

Ruby CLI TicTacToe

I have done Tictactoe game in Ruby. It is my first Object Oriented project in Ruby. I would like to separate all program into a few classes such as Player Board and Game. I want to use more OOP best ...
0 votes
1 answer
69 views

Code for appending to a certain index of an array

I have a collection which, after the code has ran, looks something like the following: ...
1 vote
1 answer
57 views

Rails controller to handle offers and payments for rental housing

I've been adding stuff a bit "blindly" to my controller and currently everything is working as it should, but the controller is gotten very messy. I'm using the gem ...
  • 11
0 votes
1 answer
61 views

RSpec: Compound Expectations

It's an exercise from a RSpec-course, which I'm currently doing. Task: Create a compound expectation, which asserts the string sportscar starts with the substring "sports" and ends with the ...
  • 3,758
0 votes
1 answer
74 views

User registration API

Would love some feedback on this simple API implementation with Ruby on Rails. Have I used proper naming conventions, readable/manageable code, optimal approaches, etc? As I'm still learning Ruby on ...
  • 103
1 vote
0 answers
47 views

Unpack a flattened hash into Hash/JSON

The following JSON ...
4 votes
2 answers
96 views

Jekyll plugin which bundles files into a zip archive

I am not a Ruby developer but cold-reading existing Jekyll plugins I implemented one to bundle files in a zip archive and open sourced it on GitHub. What do I need to fix to make it look more Ruby-...
  • 183
3 votes
1 answer
71 views

Migration in a bioinformatics application that changes a column name and explains the rationale

This very short piece of code is a migration in which a column name is changed to be more descriptive. It is part of a larger bioinformatics application written in RoR that parses the output of ...
1 vote
2 answers
132 views

Refactoring Ruby recharge REST API wrapper by using class variables

I am trying to create an API wrapper for recharge (A Shopify subscription service), I am using the HTTParty gem ...
0 votes
1 answer
88 views

Library Program in Ruby with Class

I have been learning Ruby for 2 weeks. Today my bootcamp teacher taught Ruby Class and gave me homework. I coded simple library program. But I think the code is smelling bad. How can do better the ...
2 votes
2 answers
124 views

Number guessing game in Ruby

I am a newbie in Ruby. I want to learn Ruby and I want to learn clear code in Ruby. I coded a number guessing game but my code is smelling bad. How I can do better code? ...
-2 votes
2 answers
44 views

How can I make BalanceService PORO I wrote better of any code smell? [closed]

I wrote this method which I think is not effective as it doesn't respond to the missing method when I use the PORO Class. BalanceService ...
1 vote
2 answers
57 views

Querying and rendering an HTML table of ISP performance statistics

I have multiples measurements and I want to render it into tables like this Measurements Operator browsings FTP DL FTP UL Location Event Date Operator avg min max avg min max avg min max Verizon ...
  • 107
0 votes
1 answer
130 views

Ruby: Chaining methods vs methods with arguments?

I started with the following module: ...
  • 135
3 votes
1 answer
120 views

Reshape hash of hashes into array of modified hashes without using nested each?

My goal is to write more idiomatic ruby when working with nested data structures. More specifically, I need to break my habit of looping through everything with ...
  • 135
2 votes
1 answer
69 views

Singleton in Ruby without using Singleton module or class variable

When I need a singleton class in my Ruby code (for example, single logger for multiple classes) I usually use code like this: ...
1 vote
1 answer
65 views

Group some given users according to their department

I have wrote following solution for one answer but I want to refactor to make it more simple and readable. I am receiving params ...
3 votes
2 answers
128 views

Finding minimum steps required to traverse from source to terminal

Hello everyone this is my first post here so please do critique my post if there's anything. I have made a program intended to pass a programming challenge on open.kattis.com, here. It should do as ...
  • 33
1 vote
3 answers
114 views

Advent of Code 2020, Day 10 in Ruby

I'm an amateur programmer making a career change into web development. I've found Advent of Code challenges are great way to sharpen my skills. I've cleaned up this solution for Day 10, 2020 as much ...
0 votes
2 answers
173 views

Insertion Sort Implemented in Ruby

I'm a new programmer and I'm periodically going into the Intro To Algorithms CLRS textbook and trying to translate pseudocode into Ruby for skill practice. This is my implementation/translation of ...
0 votes
3 answers
70 views

Ruby: possible Poker holds for a hand of length n

Hello everyone and thanks in advance for taking the time to read my post!! In the process of learning Ruby, I'm writing a video poker machine that analyzes hands represented by arrays of Cards, like ...
user avatar
1 vote
0 answers
81 views

Addition/subtraction of Ruby arrays

Let's say I have this array. items = [ { amount: 100, add: true }, { amount: 50, add: false }, { amount: 100, add: true } ] ...
  • 193
2 votes
1 answer
115 views

How can I make this CSV Importer code better of any code smell?

I wrote this method a few years ago in a Ruby on Rails project, which I think I am not proud of. How can I make this code better to show in-depth professionalism in Ruby? lib/merchant/web_csv_importer....
4 votes
1 answer
77 views

Generating product variants in Ruby

Based on the sample variants, I need to get all combinations of all variants. In the example I have 3x3x2=18 variants. ...
  • 193
1 vote
1 answer
60 views

Ruby Symbol Table implementation using Binary Search

I'm currently going over Robert Sedgewick's Algorithms book. I implemented a Symbol Table using two parallel array one for keys and one for values. The keys array is ordered and the Symbol Table uses ...
1 vote
1 answer
66 views

What changes can I make to simply this function that groups parameter key value pairs

I am writing a function that can be used in a ruby script that will match the parameter and values together so the script can use them in an intelligent way. The idea is if you were to run a script ...
2 votes
1 answer
158 views

Idiomatic Ruby if/else in a Sinatra route

Apologies if this question doesn't fit the format here, seemed more appropriate here than SO due to the more nebulous nature of the question. Think mini tinyURL clone: POST to service to save the url +...
  • 45
1 vote
1 answer
54 views

Ruby: Database Authentication Module

I have created a module that validates the credentials against different databases. ...
3 votes
3 answers
205 views

Better way to count the number of occurrences of a maximum number in the array in Ruby

I am trying to write a program to get the count of the maximum number in an array of integers. The below code is what I have written. Is there any better way to write this code in Ruby? ...
  • 211
1 vote
2 answers
200 views

Ruby Hangman Game

I'm still new to coding and self-taught. If anyone could take a look at my code for my Ruby Hangman game and give any tips or advice whatsoever, it would be greatly appreciated. I'm trying to improve! ...
  • 41
-2 votes
1 answer
38 views

Getting account status from a client [closed]

I have the following Ruby method. def get_status(creds) client = create_client(creds) status = client.account_status client.close_session status end ...
  • 117
2 votes
1 answer
160 views

Priority Queue implementation for top k frequent elements leetcode in Ruby

While working on the following leetcode problem: https://leetcode.com/problems/top-k-frequent-elements/ I used a Priority Queu implementation in Ruby. I have learned how to implement a Priority Queu ...
3 votes
2 answers
184 views

Reverse the words, move the vowels and downcase everything

I'm brand new to Ruby. The function takes in a string of any number of words, and reverses the order of the words. Also, for each word, it takes the vowels and moves it to the end of the word. It also ...
  • 41
2 votes
2 answers
186 views

Ruby Mastermind game project with AI

I'm self-learning and pretty new to coding and created a Mastermind game in Ruby. Any general feedback or advice whatsoever would be greatly appreciated. The game is completely functional right now, ...
  • 41
1 vote
1 answer
43 views

Should methods be inside classes? in Ruby

I have coded more in Python but I am learning Ruby now. So I am trying to write a solution to create a family_tree and add and get relationships. The code below is ...
  • 211
1 vote
2 answers
364 views

Return nil after loop and also in unless block in Ruby

I have a working code as below. Is there any better way to return nil after the each loop and ...
  • 211
-1 votes
1 answer
72 views

Correct way to define variables and methods in Ruby [closed]

I am trying to write a Person class in Ruby which has some methods and properties. The below is how I have implemented it now. ...
  • 211
1 vote
0 answers
73 views

Random Word Chain Finder in Ruby

Wrote this program out of curiosity after initially solving the Word Chains puzzle. Explanation of puzzle in link below. http://rubyquiz.com/quiz44.html NWordChains#find_word_chains will take in a ...
4 votes
3 answers
101 views

App to seed DB, generate CSVs, and export individually or in complex zip files Rails 5

I have an app whose sole purpose is to seed data files and add the data to different CSVs which are zipped and exported by the user. My application controller is filled with lines that all look like ...
  • 41
1 vote
0 answers
55 views

Custom include? method for a substring

Task: Write a function which returns true if a given substring is found within a given string. If the substring isn't found, then return false. My implementation: ...
  • 3,758
1 vote
1 answer
486 views

Ruby Tic-Tac-Toe two-player

As a preface, I'm very new to coding (self-learning) so if you notice any bad habits please let me know, it would be GREATLY appreciated. I'm self-learning Ruby so I don't really have a frame of ...
  • 41
1 vote
2 answers
96 views

Parser for file made up of sections

I'm parsing a file made up of various sections. I have a current_section variable that tracks which portion of the file I'm currently processing and the following <...
1 vote
1 answer
30 views

Write your implementation of Ruby's String#count method

The task is to write a simplified version of Ruby's String#count method oneself. Simplified because it doesn't have to have the negation and sequence parts of the original. Here my solution: ...
  • 3,758
4 votes
1 answer
161 views

Solution to the n-queens puzzle in Ruby

I started studying programming in January of this year and found a free online course that teaches Ruby. Below is my solution to the Eight Queens problem. I wanted my solution to be able to return any ...
3 votes
1 answer
66 views

Finding the lowest perfect square that can be added to a number to make another perfect square

The code below returns the smallest perfect square that can be added to a number n to result in a perfect square. It works perfectly fine but I need it to run ...

1
2 3 4 5
33