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
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
558 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
201 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
261 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
216 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
721 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
100 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
183 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
982 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 ...
3
votes
1answer
62 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
158 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
83 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
203 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
64 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
50 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
266 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
1k 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
507 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
175 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
171 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
229 views

Executing a command using Net::SSH

According to the local cop (rubocop), my method has too many lines. ...
0
votes
1answer
359 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
4k 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 ...
5
votes
1answer
721 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
45 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
382 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
312 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
3k 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
119 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
68 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
70 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 ...
0
votes
1answer
426 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
99 views

Rails model for publishable posts

I'm trying to dry up this Rails post model: ...
3
votes
3answers
2k 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
238 views
1
vote
1answer
164 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
113 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
118 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
1k 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
858 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, ...
3
votes
3answers
691 views

“Which Century” programming exercise

I recently wrote this up and am wondering if there is a better way to achieve the results I want. Essentially, we're taking a given year and adding 'th, nd, rd, st' depending on the digit. Any ...
0
votes
1answer
65 views

How do I avoid multiple chain calls?

Imagine code that reads from a file, cleans the data and finally writes the cleaned data to a new file. Let's briefly define these methods as Ruby stubs: ...
3
votes
1answer
105 views

Vehicle inheritance exercise

I'm learning about using inheritance and super in Ruby. This is a continuation of one of the exercises that is in a book I am reading. I set up my Vehicle class to be the superclass. From there, <...
0
votes
1answer
36 views

Ruby code forces the answer using a loop and a branch to guess my favorite ice cream flavor

I'm writing very simple programs. "Crawl before you walk..." they say. How could I clean this up? And/or simplify? While I am very new, technical lanquage is necessary because it helps me learn the ...
2
votes
1answer
61 views

Web scraper extracts zip files and publishes them to redis

I wrote this program for a job interview a while back. I would like general feedback on style, readability, maintainability, or any obvious flaws: ...
14
votes
2answers
3k views

Deaf grandma doesn't allow me to leave until I say bye three times

So, I'm working through Chris Pine's "learn to program" and I have been banging my head against the wall regarding the "deaf grandma" lesson. I think I got it. I'm looking for advice regarding ...
0
votes
2answers
3k views

Simple calculator in Ruby

I wrote a calculator in Ruby. These are the rules: Write the first number, in a new line the arithmetic operator, and in another line the last number. Use + for addition, - for subtraction, * ...
1
vote
1answer
81 views

A simple program using branching to guess the answer

As a rookie to programming, i'm really excited about finishing my first program. But I know I only learn by making mistakes and through constructive criticism. I'm open to any feedback regarding my ...

1 2 3
4
5
32