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
3answers
753 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
198 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
328 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
233 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
66 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
983 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
61 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
229 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
96 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
1k 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
93 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
1k 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
323 views

Tower of Hanoi solver in Ruby

Solved the classic Tower of Hanoi problem in Ruby, using recursion. Would love your feedback on this. ...
5
votes
1answer
212 views

Print combinations of balanced parentheses

Solved the below problem. Would love to hear your feedback, especially on performance improvements (granted, the solution should remain recursive). Especially concerned about the ...
0
votes
1answer
100 views

List User Online Status

I'm using Rails with MongoDB for database. In my apps I have 1 action for displaying list of users. User has name, image, ...
3
votes
0answers
65 views

Space efficient revision history storage with diffs

I've written a tool to store a files history more efficiently by only generating diffs and storing them whereas whole file would otherwise have to be stored. I would like to to hear about ways to ...
1
vote
1answer
127 views

Ruby on Rails Messaging board

I would definitely love some feedback on this message board app I made. I been doing front end development for the most part and I just want to keep my backend chops up. Let me know anywhere that can ...
2
votes
2answers
82 views

Machine gun with multiple firing modes

I have a class 'M249' it is initiated with 'ammo' The class has one responsibility(I hope) to expend ammo. Does there exist a way to increment without accessor or writer? I am shaky on when I should ...
1
vote
2answers
71 views

Testing a chance-based class

So, chance, huh? Randomness. I'm looking into how I can test a class whose sole responsibility is to return weighted random results, that is, where there is a set probability of returning true. The ...
1
vote
0answers
38 views

Calculate time to the offset of another time zone

I have a time stamp with UTC +0, i.e. time_stamp = Sat, 08 Apr 2017 16:01:16 UTC +00:00 I'm converting the time/date part of the time stamp to another time zone'...
1
vote
3answers
305 views

Given a function that returns a random number 0 or 1, write a function that returns a number between 0 and 199

We are given a function f2(), that can return either 0 or 1 with equal probability. Now ...
1
vote
2answers
107 views

Display random facts about cities

This is one of the first scripts I've written. I set it aside for a month or so and I'm not sure how to go about refactoring it. I'd love to have it be more OOD, but I have much more studying before ...
2
votes
2answers
55 views

Ruby class that validates a string

I have this class that needs to do three things: Determine if a string is 8 characters or longer Determine if a string has a number in it Determine if a string has a capital letter in it NB: I don'...
3
votes
1answer
316 views

Check if a Binary Search Tree is valid

I've written the following code in Ruby: ...
7
votes
2answers
1k views

Counting the elements of one array that are less than or equal to some other numbers

I have a method which compares two array О(n2), maxes with each element in nums against <...
3
votes
1answer
68 views

Email finding bot

I'm working on a lead generation bot that helps you find the emails of people you want to reach out to. The bot grabs your spreadsheet from gdrive, logs into several email finding tools, and collect ...
0
votes
2answers
2k views

Cyclomatic complexity for method is too high

How do i refactor my code to fix the lint error i am getting? ...
2
votes
0answers
40 views

Classes to help transform “How hard is it to …?” questions into replies like “It's really hard to …”

I've written a Twitter bot, @answering_yelp, which responds to another Twitter bot, @hard_to_yelp. They scrape yelp reviews for sentences which start "How hard is it to..." and my bot answer with some ...
1
vote
1answer
989 views

Ruby - Summing groups of odd and even integers in an array until no groups remain

The problem my code solves is listed below. I know my code can be improved, it takes a while to run when bigger arrays are entered as input. "Problem - Given an array of integers, sum consecutive ...
2
votes
0answers
420 views

Module to dynamically generate controller concerns

I'm rebuilding a Rails project from when I started developing, and I wanted to figure out how to really DRY my controllers out—without making them unreadable by doing half/all of the work in a ...
1
vote
2answers
209 views

Simple RPG - Alice fights Bob

This is designed to be a simple example of a RPG battle, for a talk I hope to give. The rules: Alice and Bob both have 5 hit points and inflict 2 damage. Each turn, Alice attacks Bob, and Bob attacks ...
7
votes
4answers
1k views

“Can a given array be made strictly increasing by removing one number?”

The premise of the problem is that given an array of numbers, can you remove only one to make it a strictly increasing sequence? [1, 1, 1, 2, 3] would be ...
1
vote
1answer
50 views

The code is used to add the digits in a digital clock to each other. I duplicated code and could use some help DRYing it out [closed]

I'm using Codefights to improve my skills. This challenge is to change the number submitted as total minutes into the digital read out on a clock i.e. hh:mm and ...
3
votes
1answer
149 views

Ruby sudoku solver using backtracking (interview)

I was required to implement the code to a spec file I was provided in Ruby. I was rejected with the message "The quality of your code isn't sufficient for this position". Can you please tell me what ...
4
votes
1answer
271 views

Ruby command-line app that outputs from JSON file

I made this application that runs fine, but I feel the code is sloppy as there are many if statements. I'm a beginner so I'm not sure how to improve it, at least not without messing everything up. Any ...
5
votes
2answers
1k views

Phone number to words based on the dictionary

This is the problem: Given a 10 digit phone number, you must return all possible words or combination of words from the provided dictionary, that can be mapped back as a whole to the number. ...
4
votes
1answer
42 views

Ruby Twitter Search by punctuations

With twitter search the punctuation and special characters will be considered part of the term you're searching for, so searching for '#twitter!' will return '#twitter!', "twitter?', '#twitter', etc. ...
2
votes
2answers
73 views

Build Hash with 2 input variables

I have a case that looks like this : John has 120 candies, He needs some plastic bags and marks each plastic bag with the number and puts 50 candies in each bag. So John can only fill up to 3 ...
3
votes
1answer
158 views

Rails 5 controller to get information about Steam users

I've done a bit of modifications and finally got it working, and did a lot of reading about @ vs @@ vs just declaring and I came ...
1
vote
1answer
179 views

Leetcode: Longest substring without repeating characters

I saw the editorial solutions for this problem, but they are written in an unfamiliar language. I'd like to post my code, which worked, and understand the timing of my solution. I don't understand how ...
4
votes
1answer
686 views

Parent-child relationship

I am implementing a parent-child relationship, represented by hashes that look like this: ...
1
vote
0answers
137 views

Quadratic equation solver in Ruby

I wrote a simple quadratic equation solver with Ruby to help me solve quadratic equations. Please tell me what I did right or wrong, and what I can do to improve it. ...
3
votes
2answers
388 views

Seed a database

I'm looking for advice / best practice to avoid doing a deeply nested set of IF/THEN statements in Rails. The intention is to ...
2
votes
0answers
840 views

Convert a curl command into Ruby

I had a curl command which I converted using this. This is the Ruby script: ...
6
votes
1answer
245 views

Expression evaluator with Shunting-Yard algorithm

I am especially concerned about: Correctness: Does my algorithm always output the correct answer given a correct input? Readability: Is my algorithm easy to understand for an experienced Ruby ...
3
votes
1answer
2k views

Find the lowest and second lowest values in an array of numbers

Trying to find way to do this with only one pass through the array, so I won't run a sort on it and take arr[0..1]. Not sure how to make it look better: ...