The Stack Overflow podcast is back! Listen to an interview with our new CEO.

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
1answer
309 views

RSpec test for handling known IP addresses

I want to replace the array of ips with a method (like known_ips or just ips) but I don't know how. I'll use this ...
2
votes
1answer
54 views

Command-line search for web-scraped TV Guide listings

This is a project that I am looking to clean up. The code is fully functioning and it has already been turned in, so I figured I can let Code Review help me. The git repository can be found here: ...
1
vote
2answers
54 views

Solution to 99 lisp problems, p09 in ruby [closed]

I am fairly new to ruby, and I am worried that my solution to problem 9 of the 99 lisp problems is extremly convoluted and inefficient. The problem is: My Code ...
6
votes
3answers
140 views

A method that calculates win streaks

Right now, I have the following: A win_streak column on the User table. This method in ...
2
votes
4answers
74 views

Cut each N symbol from array

What is the most efficient and productive way to cut each N symbol from a string? For instance, n = 5; str = '1234A1234B1234C'; result: "123412341234" This is my approach: ...
5
votes
1answer
116 views

Program to count common words in documents

I finished my first program in Ruby and I would like to share it with you so I can get some suggestions or recommendations. I'd really like to hear them because I am learning and I want to have a ...
3
votes
1answer
67 views

Failed job application test: program which alters 2d list based on commands

I recently was given this question for an interview application and am wondering where I went wrong. (The whole answer is contained in a skeleton which they gave me which I haven't included here so it'...
1
vote
0answers
81 views

Ruby Sudoku Solver without classes

I'm writing a Sudoku solver in Ruby to complete a coding challenge with the following restrictions. Failing to follow the following restrictions will result in an invalid submission: Do ...
0
votes
1answer
52 views

Buying in cash or coins

Does anyone have any ideas on how I can refactor these methods to get rid of the duplication? The first, third and fourth lines of both methods do the same thing. I'm having trouble for some reason ...
1
vote
1answer
426 views

Dice roller in Ruby (mainly for D&D)

I have been working with Ruby for about a week or two now, and I wanted to create a dice-rolling application that intakes any string (e.g. 1d4 + 1 or 3d20 + 4) and outputs the correct dice rolling. <...
2
votes
1answer
173 views

Ruby Connected Components in a Graph

I've written the following code to find the connected components of a graph in ruby, but it is quite slow since I don't use any sensible data structure for the disjoint sets, and hence find_set is ...
0
votes
2answers
219 views

Remove duplication in Ruby map with conditional entries

I have a nasty (IMO) piece of code for a datatable configuration, how do i best remove the duplication here? I've tried the obvious things I know (collapsing into one statement and trying inline ...
0
votes
1answer
136 views

Ruby BST code for insertion implementation

After some help in understanding difference why direct assignment with setter methods don't work, here is my code for a simple BST in ruby. Would appreciate your reviews and suggestions to refactor ...
2
votes
1answer
595 views

Bank ATM machine in Ruby

I've made a simple implementation of a bank ATM machine in pure Ruby. I'd like to hear some recommendations to make my code more idiomatic. atm.rb This is the startup file. I didn't use a class and ...
1
vote
1answer
92 views

Langton's Ant cellular automaton in Ruby

I wrote a Langton's Ant cellular automaton simulator in Ruby using object oriented principles. This code works. It requires the drawille library. I would like to ...
4
votes
1answer
152 views

Recursive Ruby merge sort

I have implemented a merge sort in Ruby. (Yes, I know, there's Array#sort) I would like to get feedback on code quality and optimization. ...
3
votes
1answer
795 views

Shopping cart in Rails for karaoke items

This is my first cart implementation in Rails so I wanted to get some feedback on the correct method to implement it. First of all, I am storing the data directly in the session because the items in ...
1
vote
1answer
56 views

Ensure multiple steps work/saving multiple models via a service

I just thought of something new re: saving multiple objects in Rails (though Rails is not necessary). Objectives: Create an author and a tomato, but both of those must be valid. If invalid creation, ...
0
votes
1answer
151 views

Ruby Linked List implementation

I've made a Ruby linked list data structure class and a lookup method. I know that lookup will only fetch the first occurrence ...
1
vote
2answers
63 views

Ensure that a directory reflects a specified Git commit

I have a method that looks like the method below, and I can't help but shake the feeling that it can be improved. It doesn't really "read" well, and it seems like ...
0
votes
2answers
63 views

Ensuring a non-zero radius when constructing a circle

Is there a little "better" way of writing the line: raise RuntimeError if not radius.is_a? Numeric or radius == 0 I want to only allow numbers bigger than 0. <...
1
vote
0answers
195 views

A rails controller needs to validate certain values exist

I need to pass some values from a CMS to a rails view. I should check these values are defined in the CMS. These values are meant to be use in all pages, hence the use of ...
2
votes
2answers
63 views

Copying and filtering Ruby arrays

I'm doing a Ruby exercise to create new arrays with a given filter. Copy the values less than 4 in the array stored in the source variable into the array in ...
1
vote
0answers
49 views

ruby schema matching code

I tried to find the correspondences between columns in two datasets, which is called schema matching. I implemented the algorithm like a c-style program so that I could implement it easily but I have ...
2
votes
1answer
165 views

Simple rate limiting class in Ruby

The idea of this class is I would be able to limit the use of an API/method by sending it a key which would be an IP address or username and it would tell me if they are under the rate limit. ...
4
votes
2answers
1k views

Compare two CSV files In Ruby

I wrote my first program in Ruby that compares two CSV files, but I'm sure there are more efficient ways to do it. I tried using the Ruby CSV library at first, but it was unproductive. Please let me ...
1
vote
1answer
898 views

Let's play Rock, Paper, Scissors with Ruby

I'm just looking for any tips to improve the code, particularly easier ways to change the main logic of the code so that it's shorter. ...
2
votes
2answers
354 views

Check if num X is divisible by num Y

I wrote some code for a course: Create a program that takes two numbers from the user then find out if the first is divisible by the second. If not divisible, let the user know what the remainder ...
-4
votes
2answers
172 views

Calling appropriate method depending on ARGV using a switch case [closed]

Apparently (according to rubocop), my method is too long and has high cyclomatic complexity. How do I fix this? ...
2
votes
2answers
144 views

Masking user input passwords

I have a method that accepts user input. Additionally, it masks passwords then the input is a password. It also bypasses the prompt of if an env variable supplies the needed input. Apparently, my ...
2
votes
1answer
161 views

Executing a command using Net::SSH

According to the local cop (rubocop), my method has too many lines. ...
0
votes
1answer
314 views

Find triangle types

This challenge I am working on involves checking the sides of a triangle and returning the type of triangle as a symbol. For example, :equilateral if all sides ...
5
votes
2answers
3k views

Merge hashes that have the same description field, summing the other fields

I have an array of hashes of form: { description: 'string', b: float, c: float } I need to get an array of merged hashes so that if hashes have same value at key ...
4
votes
1answer
618 views

ROT13 cipher challenge (HackerRank)

I just started learning Ruby and thought that this relatively simple HackerRank challenge would be a good opportunity to try to write clean, tested and documented code without getting bogged down in ...
3
votes
2answers
43 views

Saves files and attachments regardless of errors in the model

I have a long form with dozens of fields, and users can upload photos and attachments in the form. It's super annoying when there is a simple field missing, the record doesn't save, and the user has ...
3
votes
2answers
316 views

Finding the nth Fibonacci number using recursive technique

EDIT So I've been working on this according to the challenge @PeterTaylor gave me. I'm not very good at math, so I've been doing a lot of reading and found this link on the Binet form. Fibonacci ...
5
votes
1answer
290 views

Appointment Program In Ruby

For a homework assignment for an online class (pretty much learning everything from the internet anyway) we were asked to build an appointment program. This is just a start, however I feel I am ...
7
votes
2answers
2k views

Finding the middle day of a month, given any day within that month

Given a date, need to find the middle day of that day's month, or if there are an even number of days (with no exact middle day), the day should be the closest day to the middle of the month on the ...
0
votes
1answer
114 views

How to refactor this function to solve cyclomatic issue? [closed]

I'm trying to fix this error on codeclimate Cyclomatic complexity for resource_icon is too high. [7/6] What is the best way to refactor this function? ...
2
votes
3answers
64 views

Leading substrings exercise

I have a couple of answers that I wrote up for this exercise. Both of my methods work. I'm hoping to get some feedback on each and how I could improve them, if needed. The idea is to take a string, ...
1
vote
2answers
67 views

A simple Q and A quiz that reinforces Ruby learning

I wrote a simple quiz which I am hoping to scale-up as I continue to learn about programming. The code now stores questions and answers in separate arrays. Please review this code to improve its ...
1
vote
1answer
256 views

Add values to multiple keys in one line

I Have orders table, every order has different payment method and made by different user, has different total of money. and i want to make a detailed report, i export date as a hash to the views, as ...
1
vote
1answer
90 views

Rails model for publishable posts

I'm trying to dry up this Rails post model: ...
3
votes
3answers
1k views

Blackjack Game in Ruby

I built a simple Blackjack game in Ruby on Rails, and am looking for feedback, because I believe I may have overcomplicated things. This was a homework assignment, where I was told to make 1 be an Ace ...
1
vote
3answers
222 views
1
vote
1answer
136 views

Formatting floating point number as mixed fractional in Ruby

I have to format numbers that are either whole numbers or mixed fractional numbers with quarters (1/4, 1/2, 3/4) or thirds (1/3, 2/3). The numbers are given as floats, for example ...
3
votes
2answers
112 views

Determining if variables (integers) are greater or less than 0

Hobbyist here. I think my case/when logic needs to be cleaned up a bit. I'd also appreciate critique on the other parts...but I'm more interested in cleaning up the case/when statements... ...
3
votes
2answers
116 views

Alphabetically sorted numbers

This exercise involves taking an array of numbers and sorting them alphabetically. For example, [1,2,3,4] would return as [4,1,3,2] I mapped the array first, returning an array of words, then sorted ...
2
votes
1answer
921 views

Phrase matching between two Strings in Ruby

I've written a phrase-matching method to return the longest matching phrase from two Strings in Ruby. My method looks like this and works as expected: ...
1
vote
2answers
777 views

String of numbers to Integers (without using .to_i)

My exercise was to take a string of numbers, such as "1234" and return them as an integer 1234. However, I wasn't allowed to use #to_i, ...