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
0answers
37 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? ...
2
votes
0answers
29 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 ...
1
vote
1answer
16 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 ...
0
votes
1answer
18 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: ...
1
vote
1answer
31 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
21 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
90 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
969 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? ...
11
votes
0answers
68 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 ...
2
votes
2answers
57 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
1answer
46 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
43 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
577 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
30 views

Ruby on Rails - Creating many models associated with another one

So I have the following models: ...
0
votes
1answer
35 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, ...
3
votes
1answer
20 views

Ruby command to edit a field in a SAM file

Inspired by a bioinformatics question on Stack Overflow, I wanted to brush up on my Ruby knowledge, since I’ve been meaning to replace my command-line use of Perl by Ruby. Briefly, the problem ...
1
vote
0answers
25 views

Ruby exercise in writing and reading a file [closed]

I've been asked to improve the modular decomposition of the main by moving as many lines of code out of main as possible, but i have no idea how i should do this ...
1
vote
3answers
2k views

Rails validating with condition block

Rails AR. validate one field, with 4 validators & 2 condition block ...
2
votes
2answers
60 views

Persist relation to a class (and not a class instance) in Ruby on Rails with Mongoid

Assume my users can subscribe to several plan_types. I want to define those plans as classes, and be able to keep, for each user, a reference to a subscription_plan and also a presubscription_plan. ...
3
votes
1answer
120 views

Model Association (Rails 5 API)

I have a Character model that has a CharClass and ClassPerks child. Currently I am ...
3
votes
0answers
40 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 ...
8
votes
2answers
5k views

Handling nil: Trying to avoid #try

Yesterday, I introduced a bug into our codebase by calling #titleize on a string that could possibly come in as nil: ...
1
vote
1answer
55 views

Trie structure using Hash in Ruby

This is a follow-up to Boggle board solver in Ruby ...
2
votes
0answers
38 views

Validating Amazon CloudFormation templates using Rspec

I have created an open source project that uses Rspec to validate the formatting of AWS::CloudFormation::Init YAML blocks in CloudFormation templates.1 My project is here. Some general concerns: Is ...
1
vote
0answers
30 views

Atbash cipher in Ruby

(Here's a REPL if you want to test it online: https://repl.it/repls/RecentMonstrousOffice) I tried to implement the Atbash cipher in Ruby and this is my final code: ...
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, ...
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 ...
3
votes
2answers
128 views

Rails controller for a vocabulary quiz

I'm new to refactoring and I have just refactored an old Rails project that had bloated controller methods. I spun off the bloated methods into its own separate controller following this logic, since ...
3
votes
1answer
48 views

(Ruby version) Rotate linked list to the right by k places

I saw this question (javascript - rotate linked list to the right by k places) the other day and I tried implementing it in Ruby in a totally different way. Here's the code (I also created a REPL if ...
1
vote
1answer
654 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 ...
6
votes
4answers
573 views

State machine for handling Telnet sequences for an FTP server

I've asked Code Climate to generate metrics for the ftpd Ruby gem. It correctly identified the God class; I know what to do about that. But one of the smaller classes has me stumped. This is telnet....
1
vote
2answers
288 views

Saving a subscription plan with a payment

Is this acceptable for readability or would it be better to refactor it into smaller methods? Tips? Comments? Examples? ...
2
votes
3answers
2k views

Validating the presence of properties in a document

How can I reduce cyclomatic complexity of validation and refactor it better way? ...
3
votes
1answer
73 views

Formatting a long HTML document in a model method

I was asked to make a view which is a mix of HTML & ERB logic available in multiple views. At first this seemed pretty simple. However, some of these views required the output to be plaintext ...
5
votes
2answers
65 views

Vigenère cipher in Ruby

What I'm trying to do: implement the Vigenère cipher in Ruby. I already have a working version, but I want to make sure it is efficient and well-designed. ...
5
votes
1answer
313 views
5
votes
1answer
263 views

Querying multiple related records in Rails

I have an Account, User and Group model. All User and <...
1
vote
2answers
61 views

Initiating an action based on product type

I am starting my studies with Ruby and OO and I have received a test to do about OO. I am looking for new ways to improve the scenario following object-oriented concepts. Is there a better way to ...
1
vote
1answer
36 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
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 ...
1
vote
1answer
90 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). ...
2
votes
1answer
636 views

Parent-child relationship

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

Redis lock implementation

Considering I'm not implementing distributed lock mechanism, is this code correct and clear? ...
7
votes
1answer
440 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 ...
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 ...
1
vote
0answers
56 views

Rails dynamically changing parts of view but using the same method in controller

I have this update method which decides whether to update or recreate(same as update but resets the approval_status and create_date) of the building_approval object. I have the on_progress? method on ...
3
votes
1answer
127 views

Mars Rover challenge in Ruby

I have written a simplified version of the Mars Rover challenge in Ruby. Please review it for best coding practices. A rover’s position and location is represented by a combination of x and y co-...
4
votes
1answer
92 views

Download a Git repo without .git folder

Coming from Python, JavaScript and PHP, I'd like to learn to write Ruby in the way it's "supposed to". Well-written Python code is called "Pythonic", so I'd like to know how idiomatic my Ruby code is. ...