Ruby is a multi-platform, open-source, dynamic, object-oriented, interpreted language created by Yukihiro Matsumoto (Matz) in 1993.

learn more… | top users | synonyms

1
vote
1answer
31 views

Arabic to Roman Number in Ruby on Rails

I am making a class method in ruby to convert from roman number to Arabic and vice versa. However the loop is run to match each character and again to match with roman mapper. Can I optimize the same? ...
2
votes
0answers
25 views

Unbounded knapsack solution with items of equal value

My solution correctly solves the problem, but I would like to optimize its performance. For a capacity of 7 and bars = [ 4, 1, 1, 2, 1], the output is 7. ...
1
vote
2answers
30 views

Nesting loops on same array but skipping same element

I'm having a bit of trouble trying to find a more Rubyist way to achieve the following. Essentially, I want to try and iterate over every element e and apply ...
1
vote
1answer
39 views

Conditional merge with Ruby to include a parameter if it hasn't been included

I have the following function, and I have a hunch it can be written more concisely in Ruby. ...
1
vote
1answer
25 views

Ruby object declared as constant [on hold]

I have this piece of Ruby code: HTTPCLIENT = HTTPClient.new def read_page(url) HTTPCLIENT.get(url).body end The HTTPClient object is being created and ...
3
votes
1answer
48 views

Ruby FizzBuzz solution with Rspec test

My code makes the four tests pass, but I'm worried I'm missing something. Is the code too simple and not encompassing enough? I'm just beginning to learn so any feedback is welcome! Here's the Rspec ...
0
votes
1answer
18 views

Restricted file upload to remote the Ruby way

I am using a Rails web app on the machine webapp, from which I call an action, that pushes a file to a remote server backend. ...
5
votes
1answer
31 views

Adding product with attributes

I've been working on refactoring a piece of my code that allows a user to add items to their cart. Each product can also be added with different attributes. Thus if somebody adds the same product, ...
0
votes
0answers
32 views

Ruby thread pool implementation

I'm in the process of adapting the simple thread pool described here to my application. I'm new to concurrency in Ruby, but here is what I have so far: ...
9
votes
2answers
198 views

Luhn algorithm in Ruby

I'm learning Ruby 2.3 and I've tried to implement a function which performs the Luhn credit card verification algorithm on an input string, returning true if it ...
1
vote
1answer
34 views

Method to find posts in children categories

I use the gem act_as_votable to allow users to select categories they like. To get the categories a user likes: current_user.get_voted(Category) These ...
5
votes
1answer
157 views

Method to count unread messages

I currently use this method to count unread messages for a user (self here): ...
2
votes
3answers
110 views

Split string into 160-character chunks while adding text to each part

The send_sms_message method takes text as an argument. The length of text is unknown and can ...
1
vote
5answers
92 views

Ruby array search, returning either a result with a good score or the last result

I have an array of hashes, each of which has two attributes: "title" and "Zscore". I want to return either the result of ...
6
votes
1answer
66 views

Calculating protein mass

This question is part of a series solving the Rosalind challenges. For the previous question in this series, see A sequence of mistakes. The repository with all my up-to-date solutions so far can be ...
2
votes
2answers
41 views

Random distribution in Ruby

Below is a Ruby implementation of a random statistical event, based on a hash with the actual observed counts of outcomes. I'd be interested in feedback in particular on what techniques I might use ...
1
vote
1answer
59 views

Rails Grape API Endpoint optional params validation in update

I have the following rails grape api endpoint in the users resource: ...
3
votes
1answer
38 views

Semi-playable chess game in Ruby

I made a simple, semi-playable command line Chess game for my class exercise. At this point, this game can do the following things: Print out the updated board representation after every move ...
3
votes
0answers
59 views

Reduce encoded length of UTF-8 encoded Ruby string in C extension

I'm writing a Ruby extension in C. It's a string processing module working on UTF-8 encoded strings only. One method, full_width_to_ascii!, converts full width ...
3
votes
1answer
45 views

Redirecting to the appropriate URL for some type of notification

I have a polymorphic notification model with action attribute. When user clicks on a notification he/she should be redirected to given page the notification refers to through notifiable. What is the ...
2
votes
2answers
61 views

Custom map on `Array`

I have the following problem: Given an Array, apply a certain method x to each Array ...
2
votes
1answer
65 views

Ruby class to model (pre)historical Dates with (im)precision

I am relatively new to Ruby, having worked with Rails for about 8 months. For fun and my own education, I have written a Ruby gem (no Rails) that models dates with a precision (second, minute, hour, ...
3
votes
1answer
54 views

Using Ruby to record tasks

I started writing this program to get a better understanding of different aspects of Ruby. I tried to follow the ruby style guide here. What it's supposed to do is take the exercises/tasks you give ...
1
vote
1answer
63 views

Getting predicted values for a student report form

I have a situation where I need to predict the input values for a form used to create a new model. The model is a "class report" that a teacher submits for a student after their class finishes and the ...
4
votes
1answer
40 views

ActiveRecord selection, modified by optional parameters

I have an action method in my Rails controller which filters an ActiveRecord model depending on the submitted GET parameters. Rubocop complains that Assignment Branch Condition Size is too high: ...
2
votes
1answer
162 views

Weight-loss calculator

I have a table that holds a user's weight for specific days. In my weights_controller.rb index action I can show these weights - no problem. What I also want to ...
2
votes
2answers
45 views

A sequence of mistakes

This question is part of a series solving the Rosalind challenges. For the previous question in this series, see The Genetic Code. The repository with all my up-to-date solutions so far can be found ...
6
votes
1answer
70 views

CodeWars Solution to “Simple Elevator”

I am new to programming (less than a year), and I would like to improve. I solved a problem on CodeWars and copied both my solution and the top voted solution below. I listed specific questions after ...
5
votes
1answer
84 views

Convert to Roman numerals in Ruby

Challenged by Sandy Metz's newsletter, I've tried to implement this kata from exercism. However, my solution, which passes the tests, is quite a lot simpler than Sandy's solution. Ok, so she's using ...
2
votes
1answer
77 views

Rails validating enum

The Ruby on Rails framework doesn't allow you to validate enums submitted through a form in any sane manner. Evidently, enums are meant to be used to maintain internal application state, and are not ...
1
vote
1answer
105 views

Merging two hashes with the mean values of each key if both hashes exist

There are two methods, hash1 and hash2, that return hashes or nil. The hashes have the same ...
1
vote
2answers
112 views

Merge array of hashes, keeping duplicate values as arrays

I am working on a Ruby gem to pull data from the Wikidata API. The data is deeply nested, as it contains a lot of repetition. I have written a piece of Ruby (sans Rails) code that enables an array ...
4
votes
0answers
70 views

Electron Application with JavaScript Backdoor and Ruby Command-Line Listener

I have created an Electron Application with a JavaScript/NodeJS Backdoor and a Ruby command-line listener. I created this program for remote administration of my home computer securely using a new ...
5
votes
3answers
53 views

Client categorisation based on hourly payment

I am currently working on a small internal rating system for clients to be able to prioritise my work based on what client pays best. The input data is the hourly rate they are paying and based on ...
2
votes
1answer
25 views

Polymorphic event notification model in Rails

I have a polymorphic notification model. When user gets a notification he can get on the page of the notifiable to check out what happened exactly. For instance ...
5
votes
4answers
54 views

Two HTTP notification methods

I have two very similar methods, which make HTTP requests, the difference is that one makes PUT and another GET. Is there any proper Ruby way not to repeat the setup code and not to pass the flag ...
3
votes
1answer
36 views

Install ruby using rbenv in bash script

I want to write an automated bash script for installing ruby and rails using rbenv. But I am getting issue of session reload in terminal. My bash script:- ...
0
votes
0answers
17 views

Converting from a madlib program using hashmaps to using 3 different arrays to hold the noun, verb, and adjectives

Using erb and Sinatra, I need to make an array of each variable that can be added in, [noun], [verb] and [adjective], allowing me to return the number of each there are and create the appropriate ...
2
votes
1answer
40 views

Refactoring random number generation

As I'm learning Ruby I wrote this little program to generate a "nice" number randomly (like 4747, 6969, etc.). As I'm currently breaking the "do not repeat yourself" rule, how would you refactor the ...
5
votes
1answer
43 views

Generate dyamic 'week of' date range

I was in need of a method to dynamically generate a week range, so that the view reads something like "Week of May 16 - 22" and updates automatically. Here's the helper method I came up with, but I'm ...
4
votes
3answers
165 views

Guess a number, any number, between 1 and 10

I am learning Ruby and would like to know how I am doing as far as writing the actual code. Please keep in mind that this is my first Ruby script program, but I am trying to learn ruby in a very ...
4
votes
2answers
108 views

Sum of numbers that equal 100

I was asked to find all the numbers that added up to 100 with no duplicates in Ruby. I float between Python, GoLang, and Ruby in a day. I am always concerned if I am doing something in the most ...
5
votes
1answer
93 views

Alternately taking elements from an array

Given the task of having to return an array that alternately takes elements from two arrays, what would be most Ruby way of completing the task? As of now, I have this code, but it doesn't feel very ...
6
votes
3answers
465 views

The Genetic Code

This question is part of a series solving the Rosalind challenges. For the previous question in this series, see Wascally wabbits. The repository with all my up-to-date solutions so far can be found ...
4
votes
1answer
59 views

Command line based email generation tool

Unfortunately due to my job I will have to leave out some of the information that is created by this generator, however I will be replacing it with another string of information. I have created an ...
2
votes
2answers
42 views

Recursive categories fetch and view

I have categories and subcategories in my view. The code looks like this ...
3
votes
3answers
92 views

Sort array of hashes with nil values last

I wrote a method which sorts an array of hashes by given hash keys. The method should put nil values at the end. ...
4
votes
1answer
63 views

Determining whether a task is active based on many conditions

I wrote a Rails model instance method that checks many separate conditions, and returns a single boolean. It does not employ any nesting of branching structures. Each check has its own comments ...
2
votes
1answer
48 views

Expanding pixels in an image

Custom class Image takes a 2-D array of 0's and 1's upon initialization. Method transform returns a modified array by modifying 0's adjacent to a 1, such that ...
4
votes
3answers
94 views

Wascally Wabbits

This question is part of a series solving the Rosalind challenges. For the previous question in this series, see Mendelian inheritance. The repository with all my up-to-date solutions so far can be ...