Questions tagged [ruby]
Ruby is a multi-platform, open-source, dynamic, object-oriented, interpreted language created by Yukihiro Matsumoto (Matz) in 1993.
1,620
questions
0
votes
1
answer
334
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
1
answer
1k
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
1
answer
127
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
1
answer
233
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
1
answer
1k
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
1
answer
67
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
1
answer
175
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
2
answers
64
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
2
answers
234
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
0
answers
217
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
2
answers
80
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
0
answers
56
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
1
answer
498
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
2
answers
2k
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
1
answer
3k
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
2
answers
756
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
2
answers
181
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
2
answers
262
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
1
answer
406
views
Executing a command using Net::SSH
According to the local cop (rubocop), my method has too many lines.
...
0
votes
1
answer
436
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
2
answers
6k
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
1
answer
1k
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
2
answers
52
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
2
answers
574
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
1
answer
382
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
2
answers
6k
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
1
answer
123
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
3
answers
75
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
2
answers
75
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
1
answer
958
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
1
answer
136
views
Rails model for publishable posts
I'm trying to dry up this Rails post model:
...
3
votes
3
answers
3k
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
3
answers
326
views
1
vote
1
answer
272
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
2
answers
124
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
2
answers
159
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
1
answer
2k
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
2
answers
1k
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
3
answers
1k
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
1
answer
73
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
1
answer
145
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
1
answer
42
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
1
answer
91
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:
...
16
votes
2
answers
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
2
answers
5k
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
1
answer
90
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
vote
1
answer
214
views
Ruby Hangman with JSON to save and load
This was an assignment from "The Odin Project". This is my first program using JSON. I've been told I need work on structuring my classes, any advice will be appreciated!
https://github.com/jmooree30/...
2
votes
1
answer
106
views
Rails 5 helpers to fetch tasks with different filters
I moved a lot of the loading/logic from my view to my controller, and it's working great but after I did this I now have several very similar helper methods in my controller.
What is a more efficient ...
0
votes
2
answers
39
views
ActiveRecord query for coaches a client has bookmarked
I'm trying to figure out the best way to set up an ActiveRecord query.
Models
There are three relevant models involved:
...
2
votes
2
answers
196
views
Given an arbitrarily large file containing integers, return the largest N numbers, highest first
I've written a top_n program in Ruby, which does pretty much exactly what the title says, as part of a coding exercise. I'm trying to learn about how to sort ...