Questions tagged [ruby]
Ruby is a multi-platform, open-source, dynamic, object-oriented, interpreted language created by Yukihiro Matsumoto (Matz) in 1993.
1,618
questions
0
votes
1answer
51 views
Insertion Sort Implemented in Ruby
I'm a new programmer and I'm periodically going into the Intro To Algorithms CLRS textbook and trying to translate pseudocode into Ruby for skill practice. This is my implementation/translation of ...
0
votes
2answers
13 views
Ruby: possible Poker holds for a hand of length n
Hello everyone and thanks in advance for taking the time to read my post!!
In the process of learning Ruby, I'm writing a video poker machine that analyzes hands represented by arrays of Cards, like ...
0
votes
0answers
28 views
Addition/subtraction of Ruby arrays
Let's say I have this array.
items = [
{
amount: 100,
add: true
},
{
amount: 50,
add: false
},
{
amount: 100,
add: true
}
]
...
1
vote
1answer
60 views
How can I make this CSV Importer code better of any code smell?
I wrote this method a few years ago in a Ruby on Rails project, which I think I am not proud of. How can I make this code better to show in-depth professionalism in Ruby?
lib/merchant/web_csv_importer....
4
votes
1answer
42 views
Generating product variants in Ruby
Based on the sample variants, I need to get all combinations of all variants. In the example I have 3x3x2=18 variants.
...
1
vote
1answer
43 views
Ruby Symbol Table implementation using Binary Search
I'm currently going over Robert Sedgewick's Algorithms book. I implemented a Symbol Table using two parallel array one for keys and one for values. The keys array is ordered and the Symbol Table uses ...
1
vote
1answer
46 views
What changes can I make to simply this function that groups parameter key value pairs
I am writing a function that can be used in a ruby script that will match the parameter and values together so the script can use them in an intelligent way. The idea is if you were to run a script ...
2
votes
1answer
59 views
Idiomatic Ruby if/else in a Sinatra route
Apologies if this question doesn't fit the format here, seemed more appropriate here than SO due to the more nebulous nature of the question.
Think mini tinyURL clone: POST to service to save the url +...
1
vote
1answer
50 views
Ruby: Database Authentication Module
I have created a module that validates the credentials against different databases.
...
2
votes
3answers
166 views
Better way to count the number of occurrences of a maximum number in the array in Ruby
I am trying to write a program to get the count of the maximum number in an array of integers. The below code is what I have written. Is there any better way to write this code in Ruby?
...
1
vote
2answers
95 views
Ruby Hangman Game
I'm still new to coding and self-taught. If anyone could take a look at my code for my Ruby Hangman game and give any tips or advice whatsoever, it would be greatly appreciated. I'm trying to improve!
...
-2
votes
1answer
35 views
Getting account status from a client [closed]
I have the following Ruby method.
def get_status(creds)
client = create_client(creds)
status = client.account_status
client.close_session
status
end
...
1
vote
1answer
80 views
Priority Queue implementation for top k frequent elements leetcode in Ruby
While working on the following leetcode problem: https://leetcode.com/problems/top-k-frequent-elements/ I used a Priority Queu implementation in Ruby. I have learned how to implement a Priority Queu ...
3
votes
1answer
83 views
Reverse the words, move the vowels and downcase everything
I'm brand new to Ruby.
The function takes in a string of any number of words, and reverses the order of the words. Also, for each word, it takes the vowels and moves it to the end of the word. It also ...
2
votes
2answers
82 views
Ruby Mastermind game project with AI
I'm self-learning and pretty new to coding and created a Mastermind game in Ruby.
Any general feedback or advice whatsoever would be greatly appreciated.
The game is completely functional right now, ...
1
vote
1answer
38 views
Should methods be inside classes? in Ruby
I have coded more in Python but I am learning Ruby now. So I am trying to write a solution to create a family_tree and add and get relationships. The code below is ...
1
vote
2answers
51 views
Return nil after loop and also in unless block in Ruby
I have a working code as below. Is there any better way to return nil after the each loop and ...
-1
votes
1answer
70 views
Correct way to define variables and methods in Ruby [closed]
I am trying to write a Person class in Ruby which has some methods and properties. The below is how I have implemented it now.
...
1
vote
0answers
42 views
Random Word Chain Finder in Ruby
Wrote this program out of curiosity after initially solving the Word Chains puzzle. Explanation of puzzle in link below.
http://rubyquiz.com/quiz44.html
NWordChains#find_word_chains will take in a ...
4
votes
3answers
54 views
App to seed DB, generate CSVs, and export individually or in complex zip files Rails 5
I have an app whose sole purpose is to seed data files and add the data to different CSVs which are zipped and exported by the user. My application controller is filled with lines that all look like ...
1
vote
0answers
34 views
Custom include? method for a substring
Task:
Write a function which returns true if a given substring is found within a given string. If the substring isn't found, then return false.
My implementation:
...
1
vote
1answer
94 views
Ruby Tic-Tac-Toe two-player
As a preface, I'm very new to coding (self-learning) so if you notice any bad habits please let me know, it would be GREATLY appreciated.
I'm self-learning Ruby so I don't really have a frame of ...
1
vote
1answer
62 views
How can I reduce the complexity of this ruby method?
I'm parsing a file made up of various sections. I have a current_section variable that tracks which portion of the file I'm currently processing and the following <...
1
vote
1answer
26 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:
...
4
votes
1answer
94 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
1answer
50 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
1answer
92 views
1
vote
1answer
40 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
2answers
61 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 ...
1
vote
2answers
56 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
1answer
107 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
1answer
44 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 ...
1
vote
1answer
45 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 ...
2
votes
0answers
38 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
1answer
51 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 ...
0
votes
0answers
41 views
mergeSort implementation in Ruby
I'm currently going over Robert Sedgewick Algorithms book and I'm trying to recreate his Java implementations in Ruby. I would like to know If I'm following best practices. I try to use clean code ...
3
votes
1answer
64 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 ...
1
vote
0answers
275 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
0answers
49 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 ...
3
votes
1answer
282 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
1answer
51 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
1answer
73 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:
...
1
vote
1answer
72 views
1
vote
1answer
42 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:
...
1
vote
1answer
46 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
0answers
41 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 ...
2
votes
1answer
26 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 ...
2
votes
1answer
240 views
Remove multiple array elements by indices
I prepared a method to make possible to select elements of the array except for some indexes.
...
2
votes
1answer
76 views
Tentative to transform this scrape script into a class object program
From the last few days I have been studying scrapping 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.
...
2
votes
1answer
58 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. ...