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
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
69 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
37 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
266 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
106 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
54 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
300 views

Check if a Binary Search Tree is valid

I've written the following code in Ruby: ...
5
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
66 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
1k 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
856 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
393 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
194 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
986 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
49 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
130 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
245 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
41 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
156 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
165 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 ...
2
votes
1answer
636 views

Parent-child relationship

I am implementing a parent-child relationship, represented by hashes that look like this: ...
1
vote
0answers
122 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
325 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
790 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
233 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: ...
3
votes
0answers
485 views

Ruby script to to check the status of a rabbitmq queue and send alert

We are using a message broker called rabbitmq. The code checks the status of number of messages in q queue and sent messages. This is achieved by a curl request to a given API.The script has a sleep ...
1
vote
0answers
120 views

Find largest substring which repeats to yield entire string

My job is to find the largest repeating substring in a given string, which repeats to re-create the entire string. For example, if input="cdcdcdcd" then the longest pattern would be "cdcd". If no ...
2
votes
0answers
49 views

SSH rate limiting using Puppet firewall module

I used to do simple SSH rate limiting using an UFW Puppet module. To get more flexible blocking I've changed to using the official firewall module instead. Are there any significant issues with the ...
1
vote
0answers
101 views

Simple Sinatra app that acts as a webhook for an api.ai chatbot

I'm in the process of learning Ruby which I never used. I've built quite a few web applications in different languages, but this is my first time using Ruby. Things work, I enjoy writing Ruby, but I ...
4
votes
1answer
102 views

Path Finder Maze

The goal of my code was to find all possible moves from each open position in the maze (@moves_hash), assign it a value for how many moves it would take to get to ...
4
votes
2answers
50 views

Retrieve next card(s) base on the provided parameter

How can I remove the duplicated params[:next_cards]? ...
5
votes
2answers
103 views

FixedArray Class In Ruby

I had an assignment recently to build a FixedArray using Ruby. It's supposed to simulate a FixedArray in C, that is, an array ...
2
votes
2answers
244 views

Update single common column in multiple has-many relationships rails

I want to update a single column in 4 tables. All relationships are has-many among them. Private schools has many private classes Private classes has many lesson plans Lesson plans has many quizzes ...
2
votes
0answers
25 views

mapping event uuids to events in ruby

I have quite an interesting challenge for you today. The following code will produce a map with events as values and event uuids as keys. I've also listed out the spec for it. I would like to know ...
2
votes
1answer
421 views

Handling ajax responses in Rails

I have the following method which creates a like for a post: ...
2
votes
1answer
280 views

Redis lock implementation

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

Rails like and unlike posts

I have the following two methods which handle the liking and unliking of posts in my Rails application: ...
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
34 views

Ruby splits text into matrix creating a hash of counts of column 2

My code has two main concerns it seems unecessairly long unable to use new each_with_object method without shadowing answer(variable) How should I proceed to reduce my code and make my intention ...
2
votes
1answer
924 views

Array of date by each week

I have two dates, example : date1 = "4-02-2017".to_date date2 = "4-02-2017".to_date + 29 I try to get array of date by each week with this : ...
1
vote
1answer
27 views

Class for Stripe transactions

I just wonder whether it is a common approach to hit the third-party API and fetch values within the initialize method? Ex. ...
1
vote
0answers
551 views

Find all permutations in an array of string in ruby

I was at the job interview yesterday. And there is a question that asks me to find all the permutations in an array of string in ruby: input: ['abc', 'cba', 'ab', 'ba', 'acbdef'] output: ['abc'...
2
votes
1answer
60 views

Displaying model statistics (as a sentence) in a view

A Rails 4.2 app has three models: User Author Quote ... and the following associations: users HABTM authors author has_many quotes If the user is associated with any authors, his "profile" view ...
3
votes
1answer
544 views

Single Linked List Implementation in Ruby

The #traverse_until method is a helper function that traverses the linked list and returns the node and index of the node once a condition (in a block) is met or until the end of the list. I would ...
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 ...
1
vote
2answers
72 views

Expense family script

I wrote a script in Ruby for a better family budget overview. The expenses are hold under a CSV file. Any feedback is welcomed! ...