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
55 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
144 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
59 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
188 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
62 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
48 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
135 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
719 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
274 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
163 views

Calling appropriate method depending on ARGV using a switch case

Apparently (according to rubocop), my method is too long and has high cyclomatic complexity. How do I fix this? ...
2
votes
2answers
135 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
145 views

Executing a command using Net::SSH

According to the local cop (rubocop), my method has too many lines. ...
0
votes
1answer
276 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 ...
4
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
555 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
278 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
247 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 ...
8
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
103 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
62 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
204 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
88 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
199 views
1
vote
1answer
127 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
111 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
114 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
858 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
678 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
503 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
63 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
94 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
34 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
57 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
2k 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
80 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
1answer
86 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/...
1
vote
1answer
92 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
2answers
37 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
2answers
134 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 ...
0
votes
2answers
34 views

Counting number of messages for a mailbox over several periods

The idea is to return a number of messages received today, this week, this month. ...
3
votes
2answers
166 views

String calculator kata in Ruby

I've completed the string calculator kata in Ruby (spec from here - http://osherove.com/tdd-kata-1/). All tests currently pass. Could it be refactored further to improve readability? And could I make ...
1
vote
1answer
68 views

Largest divisible subset optimization

So this is my solution to the following leetcode problem: https://leetcode.com/problems/largest-divisible-subset/description/ How could I optimize this code? I think the run time is \$\mathcal{O}(n^2)...
0
votes
1answer
84 views

Creating a nested treeview Rails

I have a dataset that looks something like this: ...
2
votes
1answer
903 views

Rspec unit test for updating a property of a model

I know how to write simple RSpec tests and unit tests. I just want to know if this is good enough or if there are any improvements I can make. I intend to test the the ...