Ruby is a multi-platform, open-source, dynamic, object-oriented, interpreted language created by Yukihiro Matsumoto (Matz) in 1993.
0
votes
1answer
29 views
Change non-diagonal surrounding elements in a 2-D array
I'm a beginner working through an exercise on ruby arrays. Given a two-dimensional array with 0's and/or 1's, it should return an array with the non-diagonal surrounding elements of 1 changed to 1 if ...
0
votes
0answers
18 views
Renaming image files based on OCR words
I wanted to rename a large batch of image files, based on a best-guess attempt to OCR words from the file. These are all logos, which generally bear a the name of an organization.
The code presumes ...
4
votes
3answers
68 views
Idiomatic ruby to calculate distance to points on a graph
Can this code be made more like idiomatic ruby using methods such as map or inject?
@random_tour is a variable length array of points on a graph:
...
2
votes
1answer
47 views
Arabic numbers to Roman numerals conversion
The following converts Arabic numbers to Roman numerals.
I decided to factor each digit into its place value, then use a mapping to convert it to its Roman equivalent.
For example, 1997 is factored ...
0
votes
1answer
33 views
Common ancestor in a binary tree
This method finds the common ancestor of two nodes in a binary tree.
Any style suggestions or ways to make it more idiomatic? Is the algorithm correct? Rubocop says the cyclomatic complexity is too ...
2
votes
1answer
34 views
Update record with CSV
I'm using Rails 4.2.5, and I'd like to update existing records with a csv file with two fields: id,category. Where if the category field is blank, it defaults to misc.
Here's an example csv file:
...
4
votes
0answers
24 views
Using dynamic method names in place of case statements
Using Rubocop, pretty much any case statement is caught by the Cyclomatic Complexity cop, often the Assignment Branch Condition and method length cops as well. To get around this, I've been ...
4
votes
2answers
85 views
Optimize the CSV Upload function in Ruby On Rails
The code is working fine however, I would like to fix code after unless and can I store thread ID in the database too? Is there a way to improve performance?
...
0
votes
2answers
37 views
Extracting data from a string
I created a method that takes a string value with a fixed length of 9 characters and extracts three values, house (4 characters), ...
3
votes
0answers
57 views
Google CodeJam 2016 - Ruby - Counting Sheep
https://code.google.com/codejam/contest/6254486/dashboard#s=p0&a=0
I just wrote a small Ruby class to solve the first of the challenges in the G CodeJam. I am not strong at this sort thing so ...
3
votes
3answers
78 views
Finding the number in a list that differs in parity
Given any string with a series of numbers find out which one of the numbers differs from the others in evenness and return its position.
Examples:
...
4
votes
1answer
53 views
Balanced parenthesis in Ruby
I'm solving the "Balanced Parenthesis" problem in Ruby, and I came up with this solution.
...
2
votes
0answers
16 views
ActiveAdmin menu-generating mixin
I've written an ActiveAdmin mixin, such that you can include it and call simple_menu_for Foo, to reduce duplicate code when working with simple models. Here is the ...
5
votes
1answer
52 views
2
votes
2answers
101 views
Algorithm to rotate array elements in Ruby
I wrote this algorithm to rotate elements in an array. It's not very efficient, but it works. It has another disadvantage that it doesn't rotate right (it would be nice to pass it negative steps, for ...
2
votes
1answer
26 views
Order processing
I have recently built a plain old Ruby program using TDD with RSpec to model this. I wanted to get feedback.
Repo
For the company whose application we’re looking at, order processing
looks ...
-1
votes
1answer
28 views
Skipping Google search results that point to certain sites
I have the following code that will skip certain URLs if needed:
...
1
vote
0answers
19 views
0
votes
0answers
36 views
Whitewidow, SQL vulnerability web scraper
I believe it was yesterday I posted a program that scrapes google for SQL vulnerable web pages here. I never got an answer, so I just went all out and made it look a little prettier, it still only ...
3
votes
0answers
43 views
SQL vulnerability web scraper
I've created a program that will be used for pentesting. It scrapes the first page of Google out of an array of searches. It then attempts to find an SQL error within the site by adding an apostrophe ...
1
vote
1answer
57 views
Refactor code to solve the 'Line is too long' error in rubocop [closed]
Method.create(domain_id: domain.id, type_notification: type, message: title, url: url, owner_id: self.user.id)
How can I reduce this line to 80 ?
4
votes
2answers
67 views
Product of 2 numbers equals sum of numbers between them
Problem: Find all the pairs (a, b) whose product is equal to the sum of all numbers in the sequence [1..n] excluding both a and b.
I have made code for one program and need refactoring for code to ...
1
vote
3answers
58 views
Project Euler - Smallest multiple (Ruby)
My code is very slow to run, and I am trying to find a way to make it more efficient. I know there must be a better way than to write each modulo operation.
The goal of the script is to find the ...
1
vote
1answer
21 views
Mini Test testing save on one and many trips
I know there is a balance to be struck between writing dry code and damp code but I have the feeling that this is a bit too repeative. Is there a terse yet self explainatory way to rewrite these ...
3
votes
2answers
42 views
Ruby method that is validating the payload received
What is a cleaner and/or clearer way of writing the following method
...
2
votes
1answer
40 views
Count associations in a Ruby model
I'm trying to figure out if there's a better way to run the code below.
Basically Course is an association of Student and ...
2
votes
0answers
34 views
Dynamically create instance variables from a list of classes
I have a situation, where I want to add many classes as instance variables of another class, plus perform some action on each class. The purpose is to have an 'app' with many 'views', to represent ...
0
votes
2answers
49 views
Notify owner of post and other commenters
I have a rails4 app. When a user comments on a post it should send a notification to all the guys who commented on the post and to the post creator. I have a working method in the controller, but it's ...
1
vote
2answers
79 views
Controller method to manage SMS subscriptions
I have a huge method in my controller that is not DRY and just could use some general TLC. I'm new to ruby and I have not refactored my own code before, only contrived code you find on tutorials. ...
2
votes
1answer
33 views
Changing a user status flag, with validation
I am writing a function which on update of any attribute of model sets the status of variable is_kyc_verified to false.
Here is the code of the User model and the ...
3
votes
1answer
78 views
REST API with Ruby on Rails and ReactJS
I'm currently working on building a simple REST API using Ruby on Rails. I noticed that a lot of the things I did were a bit redundant, especially the models. Just to clarify: I want this to be a REST ...
3
votes
0answers
27 views
Memory efficient Log Parser
I wrote this together for a test I'm doing that parses the Heroku access log. What I'm mainly interested is if this code follow common best practices and if there are any improvements I can make in ...
5
votes
1answer
66 views
Adding ports to a random list of IP addresses
I have created an application that I will be using for pentesting. It creates a list of random IP addresses, pings them, and adds ports to them (but it's not complete).
It works exactly how it ...
4
votes
2answers
64 views
Counting numbers whose digits all go up or down
I have created a program which takes a value x and uses this value to create a power of ten value like so 10 ** x.
This has ...
3
votes
1answer
32 views
Simpler method to find users in child categories
I am using the ancestry gem to organize my post categories tree.
I have 8 parent categories, and they all have many child categories.
To retrieve an array of child categories, I use:
...
1
vote
0answers
27 views
Generating a Postgres query dealing with creating a time series
I want to sum the number of my published publications model in a given interval and return a time series. (related SO)
The main challenge is that I want the code to respect the time zone passed to ...
4
votes
0answers
47 views
Seeding a database
I'm seeding my database with really complex code as I think. It works but is not good.
Constrains: Every meal has exactly ONE main dish (marked as is_main in ...
0
votes
0answers
19 views
Whois lookup tool
I've created a tool that searches for keywords given as ARGV then scrapes the first page of Google for the keywords, from there it logs all the urls found on ...
1
vote
1answer
45 views
Removing folders from my rails app with rake
I'm trying to remove the tmp/letter_opener folder. Everything works fine, just wondering if there's a better way to write it. My next step is to write some tests, ...
1
vote
0answers
26 views
Use Ruby variable in Style tag
In my view, I have a CSS class in ruby variable which I'm using in style tag like below:
...
6
votes
2answers
89 views
Prime multiplication table
I am new to Ruby (1 year as hobby project on weekends) and solved a code challenge and it was not as good as they wanted. I want to know how i could make it better and how to make the structure ...
1
vote
0answers
39 views
Progressively split and map a large, line-based string
Goal
I have a large string (~10MB in ~400k lines). I want to "efficiently" split it on a delimiter and map the resulting sets of lines. Efficiently is defined as something that is fast (wall time), ...
2
votes
0answers
77 views
A two-method program for running background jobs
Edit: since originally posting this I have: realized that it won't run from irb/pry, and refactored to remove some sleep calls. I've also separated the sections ...
0
votes
1answer
23 views
Keep classes anonymous in TicTacToe program
My console TicTacToe program
This is my first large project attempt and I want to learn how to keep classes as anonymous as reasonable and as protected/secure as reasonable.
The program works as ...
3
votes
3answers
59 views
generate word list based on Spanish text file
I'm a beginner and wrote a program takes a text file and writes a downcased vocabulary list to another text file. I intend to use it mainly to work with text in Spanish, so I added a line to downcase ...
4
votes
2answers
141 views
Ruby method that returns an age from a date of birth
I have a method on my user model to calculate the user's age and return a human readable string. My user's can be between 1 month old and above so the returned string is different depending on if the ...
3
votes
1answer
55 views
Displaying a Google map with multiple complex markers and unique info windows
This JavaScript file is part of a large Ruby on Rails app. Any feedback is appreciated.
Update 3.7.16: as per @Jonah's suggestion, here's a description of the code's purpose:
I've built a ...
0
votes
1answer
31 views
NestedHash Class
I want to make a Ruby nested hash table class to avoid repeating ugly code and to keep my programs as true to the OOP paradigm as I can. Are there any ways to make this class definition smaller ...
2
votes
2answers
35 views
Choose a color with probability proportionate to its weight in a group of colors
Input: An array of colors with weights. e.g [['red', 10], ['blue', 20], ['green', 30]]
Output: A random color from the array, based on it's proportion to the other ...
2
votes
3answers
128 views
Adding minutes to a time value in Ruby
I recently submitted a code-challenge for an interview according to the following guidelines:
About
Write a function or method that accepts two mandatory arguments and returns a result.
...