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
1 answer
32 views

Write your implementation of Ruby's String#count method

The task is to write a simplified version of Ruby's String#count method oneself. Simplified because it doesn't have to have the negation and sequence parts of the original. Here my solution: ...
  • 3,860
4 votes
1 answer
173 views

Solution to the n-queens puzzle in Ruby

I started studying programming in January of this year and found a free online course that teaches Ruby. Below is my solution to the Eight Queens problem. I wanted my solution to be able to return any ...
3 votes
1 answer
73 views

Finding the lowest perfect square that can be added to a number to make another perfect square

The code below returns the smallest perfect square that can be added to a number n to result in a perfect square. It works perfectly fine but I need it to run ...
1 vote
1 answer
393 views

Caesar Cipher, done in Ruby

It's an assignment from the Odin Project. Here's my implementation: ...
  • 3,860
1 vote
1 answer
43 views

Ruby script implementation

I created a script to automate the task of uploading a CSV file to Box. I have a couple of files a Box client and a MySQL client. I create an instance of both on the function.rb file which is the one ...
5 votes
2 answers
284 views

Ruby function to capitalize all words in a string

The task is to write a function, which capitalize the first letter of all words in a string. For this we'll class a word as any characters separated by a blank. Because the standard ...
  • 3,860
1 vote
2 answers
89 views

API Get Request to pull data from an online database

This code gets detailed information about a particular game from an online database using an API Get request. The question I have doesn't pertain to the request but is more about the code itself. The ...
3 votes
1 answer
156 views

Ruby simple interactive interpreter

This is a solution to this problem: https://www.codewars.com/kata/53005a7b26d12be55c000243/train/ruby The task is to make a simple interpreter that will take expressions and calculate the results. I'...
1 vote
1 answer
220 views

Ruby Exercise: Implement your own "#group_by" method

Task: "Implement a method 'gruppiere', in a way that it can be invoked on all enumerable objects (Enumerable). The method receives a block and returns a hash. The items of the enumerable a grouped ...
  • 3,860
1 vote
1 answer
249 views

Ruby - Sending commands to multiple servers using Net::SSH

The idea At my work, I'm tasked with retrieving logs for a list of online orders from a total of 12 servers. To do this, I SSH into them (with a jump host in between) one by one, run the script for ...
  • 111
2 votes
0 answers
88 views

Profitwell API wrapper build as Ruby gem

I've created my first gem in Profitwell API wrapper https://github.com/eebasadre20/profitwell. I love to get some feedback, comment, or suggestion on the code structure, method implementations, and so ...
2 votes
1 answer
157 views

Linked List implementation in Ruby

I was working on Leet code design a Linked List problem. My solution with a @tail pointer, althought it added complexity it made adding to tail O(1), also having a ...
3 votes
1 answer
90 views

Presenter pattern implementation

I've recently discovered patterns and decided to implement Presenter pattern inside a pedagogical project. I spent few days playing around with different implementations, reading some Gem's source ...
  • 133
2 votes
0 answers
634 views

Building google maps inside a ruby on rails application

This is a basic map that was built: To render it, you would put this in the .html.slim files .acme-map-container-4 data=map_identifier(resource: @warehouse.id) ...
1 vote
0 answers
55 views

Encapsulating Logic to Reuse in Various Contexts

I'm experimenting with ways to reuse code throughout a Rails web app. I'm interested in feedback concerning one such approach! Context A user's request may be scoped to a physical location so that ...
4 votes
1 answer
2k views

Rails 6: Attaching Devise current_user.id to form submissions

In my Ruby on Rails 6 app: I have a User class built with Devise, and I scaffolded a List class where each List should belong_to a User. I want Lists created through my app to be automatically ...
2 votes
1 answer
72 views

FizzBuzz encapsulation In Ruby (identifying and separating concerns)

I'm working on an article about encapsulation in OOP, and I'm using the FizzBuzz problem to demonstrate what I think is so great about it. I'm hoping to share this with developers that might also be ...
0 votes
1 answer
157 views

Case of Cyclomatic Complexity

I'm trying to recreate the method inject (Works like Reduce) from ruby Enumerable Module. Using Rubocop (A Linter) I got the error: ...
  • 11
2 votes
1 answer
235 views

Ruby parallel assignment

I recently wrote the following code: ...
1 vote
1 answer
45 views

Return values from array A in the order they are in array B

So my question is the cleanest way to do #product_panels_for below: ...
  • 133
1 vote
1 answer
63 views

custom hash from group_by hash

I am trying to create a custom hash from a table. User has many transactions. A transaction has a ...
1 vote
0 answers
51 views

Advent of Code Day 3, how to optimize my ruby solution?

I have solved the 3rd day puzzle of Advent of Code but wanted some feedback on my solution. The puzzle here wants to find where two jumbled wires intersect closest to the origin. Puzzle input is ...
  • 11
2 votes
1 answer
28 views

Comparing verbosity of algorithmic Ruby class

At a high level, I'm writing a Ruby class that compares components on two differing versions of the same JSON structure, which in turn decides whether or not said elements should be updated on a PDF ...
  • 121
2 votes
1 answer
776 views

Remove multiple array elements by indices

I prepared a method to make possible to select elements of the array except for some indexes. ...
3 votes
1 answer
93 views

Tentative to transform this scrape script into a class object program

From the last few days I have been studying scraping webpages and for further development I challenge myself to transform the script I have created into a class which will receive inputs from a user. ...
  • 33
2 votes
1 answer
92 views

Ruby coin toss probability tool

I haven't been programming for a while, I wanted to do a simple project to warm myself up and get back into the game; I made this simple tool that finds the probabilities of coin toss combinations. ...
  • 173
2 votes
1 answer
63 views

Change architecture of Ruby app to be more object oriented, readable

I'm working on a Slack bot for service desk which sends direct message to user on a Slack when their ticket will be on user_action_needed status. I'm using AWS ...
  • 197
1 vote
1 answer
274 views

Rails: create a relationship between comment and user

I'm trying to connect the create of a comment to the current_user. I have a model comment which is a polymorphic association. <...
1 vote
1 answer
33 views

Concatenate unless note [closed]

mails = [] mails.concat(@note.cc_emails) unless @note.cc_emails.nil? mails.concat(@note.to_emails) unless @note.to_emails.nil? Everything what I'm doing here is ...
2 votes
2 answers
335 views

Rock, Paper, Scissors completed game

I have just completed my first Ruby OOP project. Fairly simple but still a bit time consuming as I am still learning the language. I would like feedback and ways I can improve on this and make the ...
  • 31
4 votes
2 answers
357 views

Rails Helper to display rating in stars

Using Rails 6. Here's a piece that I wrote just to display number of stars. Obviously I am disgusted by my own code. How would you refactor? ...
  • 321
3 votes
0 answers
101 views

Assigning raffle numbers to users in Rails

Background One part of our Rails app is running a Raffle, usually twice a month, with around 600k entrants each round. Each raffle ticket record will have at least a unique raffle number (consisting ...
2 votes
0 answers
80 views

Ruby script for automating software install on a brand new Linux OS

I have some scripts to automate software install, apply some configurations and so on after installing a new system. I used to have them as bash scripts but it was generally a poor idea. I have ...
3 votes
1 answer
352 views

RSpec shared example for CRUD Controllers that might be nested or not

I've been trying to create a shared example to quickly and efficiently test controllers with CRUD operations that might also be nested. I came up with this ...
  • 31
1 vote
2 answers
111 views

Removing duplicate hashes in an array while keeping count of the times the hash was present

I have an array of hashes (price_params['items']) where each item has a key called quantity what I'm trying to do is to clean ...
-1 votes
1 answer
41 views

Translator method failing to translate two words [closed]

Sorry if this has been answered to death—I'm working on a pig latin translator for Odin Project. The code below works fine for individual words, but when it receives two words ("eat pie"), the output ...
  • 13
2 votes
1 answer
120 views

Ruby code to parse and analyze social media stats in json format

What do you think about this code I wrote. It reads several json files ( I attached them as pastebins) and the code outputs summary information calculated from the input. ...
7 votes
1 answer
96 views

Incremental, encrypted backups for git

I would appreciate any review of the following script I recently posted on GitHub (gitcrypto). Most notably, I would be interested in a review of the cryptographic aspects (I am not an expert in this ...
1 vote
1 answer
128 views

Ruby: Remove middle node from Linked List

Currently, I'm going over the CTCI, I'm working on the problem removing the middle node. I'm using Ruby to go over these problems. I have the following solution. ...
3 votes
1 answer
64 views

Store personal data for later use

I'm new with Ruby and am trying to improve my coding skills. This program gets personal information for three people, and then prints them. It works, but I'm sure it's not the best and certainly not ...
2 votes
1 answer
201 views

Easily interacting with two external apis

Recently I rewrote some methods/functions written by a previous developer mostly due to the fact that the previous implementation overdid the whole 'OO' thing, and used inheritance inappropriately. ...
1 vote
1 answer
2k views

better multiple fields validation on ruby/rails

I have a form for an item (Job Item), that has sets of 2 fields based on a type. field and field + _dolar e.g: ...
4 votes
1 answer
208 views

Repeater for blocks of Ruby

I've written a module for repeating blocks of code, generally to cover issues related to eventual consistency and testing screen elements which may take some time to fully load. It looks like this: <...
  • 191
2 votes
1 answer
144 views

Command-line implementation of Mastermind

I just finished this command-line implementation of the Mastermind board game that allows two players to play against each other. I'm fairly new to Ruby, but I think I did a decent job with this. ...
  • 123
2 votes
1 answer
207 views

Remove duplicates from an unsorted Linked List in Ruby

Currently I'm going over the cracking the coding interview. I'm in the Linked List 2.1 question which is as follow: Remove Duplicates, write code to remove duplicates from an unsorted Linked List....
1 vote
1 answer
37 views

Rails initializer to be more clean [closed]

I need a little bit of refactor my initialize method because I think it will make initializer more flexible and readable. ...
  • 197
2 votes
1 answer
310 views

Dynamically combine ActiveRecord Relation results [closed]

This isn't the actual use-case I have but for simplicity, let's say I have an ActiveRecord Animal model in a Rails app. The Animals have a name (string) and ...
0 votes
1 answer
586 views

ActiveRecord Query for many to many association

I have two Models in my Rails 6 / PostgreSQL application which have a many to many relation; Car and Driver. The goal is to get ...
  • 153
2 votes
2 answers
139 views

Calculating p-value for Welch's t-test

I am attempting to translate some of my Perl code to Ruby, from http://rosettacode.org/wiki/Welch%27s_t-test I am a very very beginner with Ruby, but I have verified that this is producing the ...
  • 217
4 votes
0 answers
74 views

Identifying the missing number in an array

This is from LeetCode: Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. Example 1: Input: [3,0,1]...

1
2
3 4 5
33