Ruby is a multi-platform, open-source, dynamic, object-oriented, interpreted language created by Yukihiro Matsumoto (Matz) in 1993.
4
votes
3answers
73 views
Setting locale based on preferences with fallbacks
Consider the following code. Are the multiple assignments to the loc variable a code smell? If so, how can I refactor this?
...
4
votes
2answers
54 views
Scan array for 3 consecutive numbers that sum to 7
Write a function lucky_sevens?(numbers), which takes in an array of
integers and returns true if any three consecutive elements sum to 7.
...
6
votes
2answers
57 views
Printing a Tic-Tac-Toe board in Ruby
I have two implementations of a function that prints out a basic board for Tic-Tac-Toe.
Which of these should I include and why?
...
2
votes
1answer
39 views
Printing polynomials
I am taking a class on ruby and our assignment is to take user input and print out a polynomial. Wondering if I can get any feedback to how to improve my code.
...
1
vote
2answers
35 views
Ruby OOP convention/layout
I am new to programming and I have just written my first program. It takes files from an SD card and copies then into a new named folder and has the option to name the files themselves or simply move ...
5
votes
1answer
37 views
Report with Subtotals — In Search Of An Abstraction
High Level Explanation of Code's Purpose
I wrote a class to create sectioned reports, where each section has a subtotal. It allows these reports to be nested within each other, to create multiple ...
1
vote
0answers
23 views
Copying csv to sql_table
I wrote code that takes csv and export it to sql table. It looks like this:
...
1
vote
1answer
33 views
Use an optionally passed-in block without repeating yourself
I need to open and read several files for a project. Some of them are plain text files, and I just want a standard File.open to read from. One of them is an XML ...
0
votes
1answer
32 views
Ruby - changing value of variable without being repetitive [closed]
I have a couple statements that look too bloated for good ruby practice.
@q = @q=~/AND/ ? @q.split("AND") : "False"
@q.each { |g| @q[g] = @q[g].strip }
How can I ...
3
votes
1answer
45 views
HTTP client for company services
For quite a while now I've been writing a bit of Ruby code, but so far I still don't think it was good idiomatic Ruby. But there's been a new requirement and I tried my best writing it better, more ...
5
votes
3answers
226 views
Finding nearest power of 2
I have the following simple program which calculates and returns the nearest power of 2 of a given number. Because I use increments and decrements to find the closest power, this takes a very long ...
3
votes
1answer
88 views
Simple SSH bruteforcer in Ruby
I'm pretty new to Ruby, and working in the IT security field, I thought to make something useful to my work while I learn the language (even though I'm reinventing the wheel).
The script is working ...
0
votes
0answers
39 views
New to building API's, using Grape API and need some help cleaning up a very long if/else statement
My incoming message code connects to the Slack API, using the slack-ruby-client gem. This code is handling so many things; what are some ways I can break this apart ...
3
votes
1answer
65 views
Merge Sort - Using Ruby and Recursion
I am implementing the algorithms of Course: Coursera Algorithm I.
I implemented the Merge-Sort using recursion and although it sorts correctly I am not sure if it is a correct implementation of this ...
5
votes
2answers
57 views
Simple TicTacToe game with AI in Ruby
What changes do you recommend from the perspective of structure, logic, etc?
...
4
votes
2answers
67 views
Simple Translation Gem
I have a small gem that I've been maintaining. This gem provides a simple way to do translation using an alternative Google API.
I've been working with Java last year and recently did some ...
6
votes
2answers
65 views
Ruby command line Mastermind game with AI
I've created a small command-line 'Mastermind' game using Ruby. The intent of the project (aside from having fun in the build!) was to learn and emphasize OOP concepts and principles.
The game has ...
8
votes
3answers
181 views
Initialize a hash with all 0 values based on two arrays
Given two arrays (for instance a list of people and a list of subjects), I need to initialise a Hash with all zeros for all possible combinations of these two ...
4
votes
2answers
74 views
The Badge Bot 9000
Because I'm both lazy and wanting to mess around more with time management and Ruby, I thought it would be fun to create a simple script that opens Code Review once every day to go towards the daily ...
1
vote
0answers
28 views
XML/HTML batch generator
As part of an exercise I've written some code to generate multiple HTML files based on information stored in a couple of XML files. I'm not really sure how much code you'd need to evaluate my function ...
3
votes
0answers
44 views
Multiple many-to-many filter with range using filteriffic
I should say that the code I have works. I feel like there has to be a much more efficient way to do it (especially when I just crashed my server with it).
Relevant schema:
...
4
votes
2answers
179 views
Maze solver for 2D matrix in Ruby
I got rejected for a junior Ruby job entry where I had to solve a 2D matrix as a maze with walls.
This is the main class:
solver.rb
...
4
votes
2answers
71 views
Parsing mathmatical functions
I have finally finished a Ruby calculator project, which is not based on eval. Instead it parses input char by char. The project is hosted on GitHub.
However, I find a specific part of the program ...
0
votes
1answer
34 views
Checking whether users have a role that allows them to index sheets
Is there any DRYer way to write this method ? or is it better to keep it this way for better reader understanding ...
Each role has a list of authorised activities for a specific resource ( in this ...
0
votes
1answer
50 views
Yet Another Regex-Based MarkDown to HTML converter
I wrote a simple script to translate (a subset of) StackExchange markdown to HTML. I used Regex for this task, but luckily I only had to use simple patterns.
...
1
vote
1answer
28 views
Check that an image is available to be served
I have a couple of images in my database and I need to check if the images are still good or if they're deleted or corrupted. I came up with a solution but it's very slow. I was wondering if there was ...
1
vote
1answer
81 views
Checking to see if all values in an array are equal
I have an array that might be either one-dimensional, two-dimension, or three-dimensional. I'm wanting to check to see if all values in a given direction are constant.
Here are 3 functions:
...
0
votes
1answer
80 views
What's the best name for a service object in Rails?
One challenge I regularly face is how to name my service objects in Rails.
I like the advice in this blog post on Gourmet Service Objects, a service object performs one action. It holds the business ...
4
votes
2answers
50 views
Print the length of words as input as a histogram with horizontal bars
Input
A list of words separated by any number of spaces.
Output
A horizontal ASCII art histogram, where the n-th line is composed by as many asterisks (*) as the ...
2
votes
1answer
36 views
Importing Excel file to a database just once
How should I rewrite an if-return-else condition in the middle of this function?
...
9
votes
1answer
208 views
Brainfuck Printer Generator
Given a text, this program will output a Brainfuck program, that when executed will print the text back.
Writing Brainfuck in this style to print a sentence is pretty straightforward. Iterate over ...
4
votes
2answers
200 views
Ruby Interrupted Bubble Sort
Working through CodeEval's Interrupted Bubble Sort, I have a working algorithm, but there's a speed issue. I'm trying to do this with Ruby and I keep getting an error that it's timing out after 10 ...
1
vote
1answer
43 views
One hash with params populate two objects
I have a JSON API where a user can update a post and related details to that post. To simplify the process the user just fills in one hash and we don't expose our architecture. What I did here works ...
8
votes
1answer
65 views
Novice implementation of Tic-Tac-Toe
After reading a lot of tutorials, I was finally able to code my version of a Tic-Tac-Toe game. I would like to have some honest review of the code to see what I can do to better my coding.
...
2
votes
1answer
62 views
6
votes
2answers
67 views
Letter-scoring Scrabble class
I wrote Ruby code (copied below) but was told that the code does not compile (it does on my computer) and that it does not answer the question:
Create a class called Scrabble that implements a ...
3
votes
1answer
57 views
Twisting words! (boustrophedon formatting)
I decided to implement properly the Code Golf challenge about twisting strings. I report the assignement verbatim for convenience:
Twisting Words!
Given a string and a positive integer. You ...
3
votes
2answers
39 views
One element only subarray max length
Task description
You are given a list and an item, find the length of the longest consecutive subsequence of the list containing only the item.
Testcases
...
7
votes
1answer
51 views
Summing the size/count of associations for multiple records
I'm working on a plain Ruby class within a Rails application.
For the total_leg_count method---is that the best way to go about achieving what I want? I feel like ...
2
votes
1answer
41 views
Skinny model, fat controller in Rails managing user IP addresses
As part of learning Rails framework, I have small app in progress. Basically idea is, user can create IP address, define is it in use, to which server is assigned, which user is it using and role of ...
5
votes
1answer
74 views
Caesar Cipher encryptor using ASCII
I have built a Caesar Cipher in Ruby that utilizes ASCII for the encryption. I am not sure whether or not this was the best way to go about the task but this way made the most sense to me. Please give ...
3
votes
3answers
69 views
Palindrome Validator
This code returns true if the sentence is a palindrome, and false otherwise. Is there a better way of doing this?
...
3
votes
0answers
17 views
Monkey patch data in dictionary
The chart data is stored in a nested dictionary struct @line_charts, and I need to convert the prices to target currency with the function ...
3
votes
1answer
52 views
Stock picker that tells when to buy and when to sell
The task of this project was to create a method that takes in an array of stock prices, one for each hypothetical day. From there, the method should return the best day to buy and the best day to sell ...
6
votes
1answer
70 views
Prefix Sum in Ruby, Genomic Range Query from Codility
I'm currently going through some lessons on Codility. I've just spent a couple of hours with GenomicRangeQuery, which is intended to demonstrate the use of prefix sums.
The task description is here. ...
4
votes
2answers
39 views
Custom delete method refactoring
I'm new to Ruby and don't know all the methods, so I was thinking there would probably be and easier way to do something like this. I'd love any input on how to refactor this.
...
5
votes
0answers
52 views
Converting ASCII to an image
So after I finished my fractal generator, I decided that text is ugly, at least compared to pictures. So I made a text-to-picture converter:
...
3
votes
2answers
65 views
Insertion sort in ruby
The code is correct, passes all testcases, I am looking for input on cleaning it, readability etc
...
10
votes
0answers
57 views
Generalized 2D cellular automata simulator
This is a simple Game of Life implementation, with one neat gimmick: You can specify the rules that the simulator uses to decide whether cells should live or die. Congrats, God!
I'd especially like ...
2
votes
5answers
104 views
Sudoku solver in Ruby
All the test cases pass except one of the test case which runs > 10 seconds. What can I do to make my solution faster?
...