Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.

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
142 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
113 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
116 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
942 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
787 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
600 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
65 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
100 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
36 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
58 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
81 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
92 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
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 ...
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
143 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
198 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
71 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
99 views

Creating a nested treeview Rails

I have a dataset that looks something like this: ...
2
votes
1answer
1k 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
42 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
431 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
203 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
157 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
146 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
671 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
570 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
127 views
3
votes
0answers
118 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
54 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
456 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
712 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
289 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
156 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
507 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
93 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 ...
3
votes
0answers
206 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
2k 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
332 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
312 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
640 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 ...