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
3
votes
2answers
167 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
85 views

Creating a nested treeview Rails

I have a dataset that looks something like this: ...
2
votes
1answer
922 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 ...
1
vote
1answer
41 views

Refactored code for updating user password, email and other info

I have to update user email and password if they are present in the params, I have other user information also which can be updated. this is the method I have written, but it doesn't seem like a good ...
3
votes
2answers
428 views

Making Anagrams

Task: given two strings, that may or may not be of the same length, determine the number of characters you must delete to make the two strings anagrams of each other. My solution: ...
1
vote
2answers
202 views

Resetting a password

I am trying to reset a password when password_reset_token is received in the controller. But it's becoming a long if ...
3
votes
1answer
138 views

Ruby & OOP: FizzBuzz Game

I would like a more elegant solution for this problem, I think the FizzBuzzArrayBuilder class is not used properly. Also can those ...
2
votes
0answers
139 views

LeetCode Minesweeper solution in Ruby

This is my solution to this LeetCode problem. Essentially you want to get the next board state for a Minesweeper game. All test cases passed and the submission was accepted on leetcode. The ...
0
votes
2answers
53 views

A class that uses a few other classes to generate an email

I seem to find my self writing a lot of classes like the below where there seems to be some kind of send method that just calls a bunch of other methods. Is this ...
2
votes
0answers
635 views

Rails CRUD feature specs (RSpec and Capybara)

The following feature spec tests CRUD operations for the users resource in a Rails 4.2.8 application. It includes tests for filtering, sorting and pagination ...
0
votes
1answer
51 views

rails method for transaction type

I am trying to make a rake task but after completion my code i realized i need some refactorization. How could I do refactorization of the following code? ...
2
votes
1answer
530 views

Rails5/Devise/Omniauth (Multiple Providers)

I spent the better part of the last two days getting this to work, but it finally is working, but it feels extremely redundant. I am following a belongs_to method ...
3
votes
1answer
121 views
3
votes
0answers
107 views

Codingame: Great Escape bot in Ruby - Follow-up

This is a follow-up to Codingame: Great Escape bot in Ruby. As mentioned there, my first reasonably sized Ruby project is a bot for CodinGame's Great Escape contest (here is a sample gameplay video). ...
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 ...
0
votes
1answer
52 views

Ruby class to encapsulate Retrying (for instance, with email)

One of my clients sends quite a few emails a day ... I would think over 500. They are mostly reports to clients or employees. The volume has become sufficient that intermittent issues with gmail (like ...
-2
votes
2answers
3k views

Case statement with array includes? [closed]

What is the best practice to implement case checks using arrays in Ruby? ...
4
votes
2answers
71 views

User and picture models

I have the following models: User Picture Variant User has_many pictures, picture has_many variants. Variant has value price ...
3
votes
1answer
426 views

Controller tests (Rails 4.2 and RSpec 3.5)

The following code is a controller spec used in a Rails 4.2 application with RSpec 3.5. I would greatly appreciate your suggestions on how to improve it (e.g., efficiency, readability, maintainability,...
3
votes
2answers
609 views

Group array by sums

I have the following code that takes an array, and groups it by the sums of its elements: ...
5
votes
2answers
275 views

Codingame: Great Escape bot in Ruby

(EDIT: I incorporated a lot of the feedback from the answers, and posted a follow-up question here: Codingame: Great Escape bot in Ruby - Follow-up) I started working on some code challenges at ...
-1
votes
1answer
153 views

Merge sort for an integer array in Ruby

I've implemented merge sort an integer array. Should I change them to more meaningful names? Overall, any further suggestions on this code? I would like to improve my coding style and to shorten the ...
3
votes
2answers
462 views

Convert decimal to binary and find the maximum number of consecutive 1's in the binary

Task: given a base- 10 integer, n, convert it to binary (base-2). Then find and print the base- 10 integer denoting the maximum number of consecutive 1's in n's binary representation. What can you ...
1
vote
2answers
50 views

Weekly job to create or update the lessons in all locations

I'm working in a project with the following structure. A location can have many timetables with many lessons. I need to perform a job in all locations every sunday to create or update the lessons. <...
7
votes
0answers
88 views

Stable variation of built-in sort

Ruby's built-in sorts are unstable. The stable_sort gem monkey-patches Ruby's built-in library to have stable sorts which include nearly all the functionality of the built-in sorts, but with the ...
2
votes
0answers
204 views

Ruby on Rails portfolio website

I've been learning Ruby on Rails for the last few weeks. I've decided to do a portfolio website using Ruby on Rails to showcase all of my projects. Can you look through my code to see what you think? ...
5
votes
3answers
1k views

CodeWars kata 'Sum Of Pairs'

I need help in optimizing my code for this challenge. I think my algorithm is fine and I pass all sample tests but get took longer than 12000ms to complete error for final one. I guess there are a ...
1
vote
1answer
259 views

Extract list of selected contractors by matching serialized array in Rails

I'm working on an application where I have a Company model setup as follows: ...
3
votes
1answer
304 views

Ruby wrapper for Instagram API

I've created my first gem as Instagram API wrapper https://github.com/agilie/instagram_api_gem. I would be very pleased if you give me some feedback on code organization, methods elegancy and so on. ...
4
votes
7answers
1k views

Display related records, if they exist, in Rails view

Is this much logic in view is justified to check for the presence of record? ...
1
vote
0answers
536 views

Unit-testing authorization policies in Rails with RSpec

The following specs test the authorization setup of the Post resource in a Rails 4.2 application. The Pundit gem is used for authorization but I believe the specs ...
6
votes
0answers
239 views

Solve alphametics puzzles in non-brute-force, iterative manner

The goal: Trying to optimize solution for the alphametics problem on exercism. From the question text: Alphametics is a puzzle where letters in words are replaced with numbers. For example ...
1
vote
3answers
568 views

Count has_many relationships

I have 3 models: Import, Product, Values. An import has_many products. A product has_many values. I want to find the product with the highest values count and the sum of all values for all those ...
0
votes
1answer
75 views

Array even and odd indexes sorting and printing its values

I'm looking for a more elegant solutions of this task. Task: Given a string, print its even-indexed and odd-indexed characters as space-separated strings on a single line. Input Format: The first ...
4
votes
1answer
1k views

Validate if Sudoku is correct

Given a two-dimensional array, Sudoku class can create NxN Sudoku where N = size^2. Assume that the user gives only valid NxN matrixes for the constructor. Method <...
1
vote
0answers
163 views

Rails - updating and deleting exactly 2 children of the parent model

Requirements A Phrase can have many Translations, which are in turn a Phrase; thus, the ...
2
votes
1answer
301 views

URL whitelist class

I'm trying to make a class for safe redirection of URLs. Redirecting URLs is something you can have in a get parameter, for example: ...
3
votes
1answer
189 views

Redis lookup with a default value

I have some code to perform a redis lookup, but if the key to look up either doesn't exist or there's an issue connecting to Redis at all, it will use a default value. The logic currently looks like ...
3
votes
2answers
65 views

Rails method to match users according to language fluency level

This code works fine, but I'm still learning, and feel like there is a more Railsy way to write this. Is there a good rails ActiveRecord method I should learn more about? Please let me know if you ...
3
votes
1answer
852 views

Finding the middle permutation

This method finds all permutations of a string and stores them in a sorted array. It then returns the element in the middle of the array. ...
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
201 views

Sudoku solver using backtracking, in Ruby

I wrote the below Sudoku solver, using Backtracking. Would love to hear your comments on the approach, and how I can improve readability/performance: ...
2
votes
2answers
93 views

Extracting an IP address from a hosts file by hostname

I am currently working with Vagrant for the first time, and therefore also working with Ruby for the first time. I have a specific hostname and IP address I would like set for my VM, both of which are ...
0
votes
1answer
46 views

Checking whether any of some large collection of attributes has changed [closed]

I have to check a large number of attributes and do something if any of them have changed. However, right now there are way too many or statements. Is there a better way to do this? ...
4
votes
3answers
965 views

Ruby banking system program

As a beginner I'd like to get any kind of feedback. The more the better. Any optimization and style mistakes? ...
1
vote
1answer
90 views

Print lyrics of 99 Bottles of Beer

I read 99 Bottles of OOP, and one of the offhand comments was that doing the 99 bottles problem with composition was another route that one could take (the book used inheritance). Here is my attempt. ...
1
vote
2answers
855 views

Setting unique code to model before creating (ruby/rails)

I have a Table model that needs a unique code. I have a method creating the code (and making sure it's unique), but I have the same line ...
4
votes
2answers
251 views

Tower of Hanoi solver in Ruby

Solved the classic Tower of Hanoi problem in Ruby, using recursion. Would love your feedback on this. ...