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
votes
0answers
18 views

better multiple fields validation on ruby/rails [on hold]

I have a form for an item, that has sets of 2 fields based on a type. field and field + _dolar e.g: cogs_paid and ...
3
votes
0answers
36 views

Repeater for blocks of Ruby

I've written a module for repeating blocks of code, generally to cover issues related to eventual consistency and testing screen elements which may take some time to fully load. It looks like this: <...
3
votes
0answers
107 views

Script to auto-generate Markdown tables of contents

I would greatly appreciate feedback on the enclosed script, which auto-generates a Markdown table of contents for Github-flavoured Markdown, with respect to: Consistency with Ruby idiom & style ...
3
votes
2answers
67 views

Keep ABC size down for conditional string concatenation in Ruby

This method takes a comment object and displays it correctly depending on what columns are available. EVENT_TEXT and REASON_TEXT ...
1
vote
0answers
7 views

Command-line implementation of Mastermind (Ruby)

I just finished this command-line implementation of the Mastermind board game that allows two players to play against each other. I'm fairly new to Ruby, but I think I did a decent job with this. ...
3
votes
1answer
47 views

Calculate the percentage of sum of hash values

Please review my code and help on How can I make it better calculation in method and class definition with SOLID ruby principles?? The application has a static JSON file which contains: ...
2
votes
1answer
43 views

Remove duplicates from an unsorted Linked List in Ruby

Currently I'm going over the cracking the coding interview. I'm in the Linked List 2.1 question which is as follow: Remove Duplicates, write code to remove duplicates from an unsorted Linked List....
1
vote
1answer
27 views

Rails initializer to be more clean [closed]

I need a little bit of refactor my initialize method because I think it will make initializer more flexible and readable. ...
3
votes
0answers
41 views

Atbash cipher in Ruby

I implemented my own spin on the Atbash cipher in Ruby: ...
2
votes
1answer
48 views

Dynamically combine ActiveRecord Relation results [closed]

This isn't the actual use-case I have but for simplicity, let's say I have an ActiveRecord Animal model in a Rails app. The Animals have a name (string) and ...
2
votes
2answers
47 views

Calculating p-value for Welch's t-test

I am attempting to translate some of my Perl code to Ruby, from http://rosettacode.org/wiki/Welch%27s_t-test I am a very very beginner with Ruby, but I have verified that this is producing the ...
0
votes
0answers
13 views

ActiveRecord Query for many to many association

I have two Models in my Rails 6 / PostgreSQL application which have a many to many relation; Car and Driver. The goal is to get ...
4
votes
1answer
135 views

Model Association (Rails 5 API)

I have a Character model that has a CharClass and ClassPerks child. Currently I am ...
1
vote
1answer
32 views

Rails controller method to show an optionally scaled image using MiniMagick

I've got below implementation for my controller and show action which implements MiniMagick gem. I've just wondering is there any better way to write it down? If ...
3
votes
1answer
68 views

Adding an item to a hierarchical data structure

What I need to do is create a new object, modify it using a recursive function, then merge it with another object, and do this all in such a way that the values of the new object take precedence over ...
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, ...
1
vote
1answer
205 views

Listing employees, categorized by license expiration date

I am working on a personal project and I have hit a wall. I know I am writing bad code and I really want to refactor the code below. The application has three tables on the same page. Each table ...
4
votes
0answers
51 views

Identifying the missing number in an array

This is from LeetCode: Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. Example 1: Input: [3,0,1]...
1
vote
1answer
843 views

Rails passing in params to association on create method

I am currently using a belongs_to relationship in my Rails application to link a map and a group an object that way I can assign those maps to the user based on the ...
9
votes
1answer
82 views

Summing the size/count of associations for multiple records

I'm working on a plain Ruby class within a Rails application. For the total_leg_count method---is that the best way to go about achieving what I want? I feel like ...
3
votes
1answer
64 views

System Dependency Manager - Ruby

I recently got a prompt when looking for a job. To spare any details, the company decided not to get back to me, so because I did not get a code-review/feedback from them, I was wondering if someone ...
6
votes
2answers
410 views

Find associated object if exists, otherwise generate it

I have a simplified model that looks like this: ...
5
votes
1answer
266 views

Querying multiple related records in Rails

I have an Account, User and Group model. All User and <...
1
vote
1answer
42 views

Ensure that a Sinatra hash always has a valid value

I'm trying to ensure that a Sinatra hash always has a valid value. Is there a more-concise way to write this? ...
1
vote
1answer
95 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 ...
2
votes
2answers
143 views

Ruby implementation for Hacker Rank connected cells in a grid

https://www.hackerrank.com/challenges/connected-cell-in-a-grid/problem problem statement: Consider a matrix where each cell contains either a 1 or a 0. Any cell containing a 1 is called a ...
1
vote
1answer
99 views

Short Ruby program that opens a random video file from a directory

I am currently learning to code in ruby. I have created a small program that iterates through a directory of video files and opens one at random. The program is fully functioning (with the odd issue). ...
4
votes
1answer
682 views

Parent-child relationship

I am implementing a parent-child relationship, represented by hashes that look like this: ...
2
votes
1answer
301 views

Redis lock implementation

Considering I'm not implementing distributed lock mechanism, is this code correct and clear? ...
2
votes
1answer
74 views

Add method to Ruby's Enumerable module that produces every nth element

The task is to enhance the Enumerable module with a method nth(). The method shall create a new list, which contains every nth-...
5
votes
1answer
1k views

Rails & RSpec - Testing API with request specs and shared examples

I have written the following shared examples which are used in multiple request specs to test a namespaced RESTful JSON API. Application details: Rails 4.2, RSpec 3.5, Devise for authentication and ...
4
votes
2answers
112 views

Ruby price calculator for groceries

I am good at logic and most of the time I write working code. But I want to learn writing code which follows best practices and is very efficient. I tried to implement some of them in my code but it ...
5
votes
1answer
67 views

Update join table using list of checkboxes in Rails

I have Gig and Singer Active Record models (standard--no customization just yet) with a many-to-many relationship through a ...
0
votes
1answer
26 views

Enumerating AWS instances in Rails, with error handling

I have created a method, which is raising the same exception in case of any error: ...
0
votes
2answers
40 views

Ruby if block refactor from new synthax needed

I have this, I guess, new Ruby syntax in my method but Rubocop are warning me that the second last line is too long. Could you please help me to refactor this method? ...
8
votes
2answers
457 views

Make Project Euler 27 solution idiomatic Ruby

I learned to program in Java and C# and in my free time I am using Ruby because it is fun. Unfortunately I have the feeling that I write Ruby code like I am making Java or C# code. I have learned to ...
2
votes
1answer
31 views

Statistical function which computes the average work time in different months

It's a task from a Ruby-course, in which I'm currently enrolled: Ruby Course - Page Precisely it's one of the assignments for the first week. Following idea: Your are given a list in which finished ...
2
votes
1answer
60 views

Table class to fill array structure with values

The code creates a table and adds rows and columns. Does it make sense to write the code like this? Would you design it differently? Is there a better OOP approach to this? ...
1
vote
1answer
22 views

Rails 5.2 callbacks to trigger score recalculation for relationships

I have a model that is in a HABTM relationship with 2 other models: Activity.rb ...
1
vote
1answer
35 views

Re-creating user discounts nightly

I'm calling these two methods on a Worker that is executed every 24 hours at midnight in the background. The code seems to be a little bit difficult to read and I'm not sure if i'm iterating through ...
3
votes
0answers
27 views

Rails route structure for managing tags

I'm hoping someone could vet my route structure as I've really been struggling with getting something that feels right. Right now I have this nested resource structure: ...
1
vote
2answers
158 views

github page/ DHH Score Challenge in ruby

I wrote the following code which when executed prints the score of https://github.com/dhh. ...
5
votes
2answers
1k views

Rails controller action methods corresponding to static web pages

One of my rails controllers is horribly overcrowded with a bunch of methods that link to static web pages. Controller ...
6
votes
3answers
2k views

GCD of two numbers in Ruby

I am trying to implement a algorithm for the GCD of two numbers in a functional approach. Can this be improved further, also with regards to performance? ...
12
votes
0answers
106 views

Ruby chess engine gem

This is my first Ruby gem that claims to provide all the rules of the chess game. My goal was to keep this library as simple as possible via OOP principles, and I'd be glad to hear any feedback from ...
1
vote
1answer
101 views

Rspec test for Rails method to return alphabetically sorted list of tags

I have one model class as listed here: class Post < ActiveRecord::Base has_many :comments def tags comments.map(&:tag).uniq.sort end end and ...
1
vote
1answer
57 views

Getting the count of how many times a route has been hit

I have this log file (example below) and I'm trying to find the number of times a particular route is called. Example log file: ...
3
votes
1answer
627 views

“Can a list of meetings all be scheduled in a conference room?”

I was given this question at an interview for a position I just got rejected by, so looking to improve and see what better answers there are! ...
1
vote
1answer
35 views

Ruby on Rails - Creating many models associated with another one

So I have the following models: ...
0
votes
1answer
39 views

Modular multiplicative inverse in Ruby

I implemented an algorithm to find the modular multiplicative inverse of an integer. The code works, but it is too slow and I don't know why. I compared it with an algorithm I found in Rosetta Code, ...