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

learn more… | top users | synonyms

4
votes
1answer
17 views

Is this a good Ruby implementation of Prime Factorization?

I'm doing some practice problems on Khan Academy. The current one is Prime factorization. I came up with this: ...
-1
votes
0answers
14 views

Ruby on Rails: how to link employees table with devise authentification [on hold]

I have a model "Employee", some employees have access to the site via devise authentification. What's the best solution to combine it? I need such functions: 1) Click a button in index view of ...
0
votes
0answers
17 views

Same method but slightly different [on hold]

I have a process in my application that requires I take an additional step from the 'usual' process. Let's say I have a class/method like this: ...
3
votes
2answers
65 views

Capitalize all words in sentence string in Ruby

I have created the following code: def LetterCapitalize(str) str = str.split(" ").each {|word| word.capitalize!} str = str.join(" ") return str end This ...
13
votes
4answers
924 views

FizzBuzz in Ruby

I have this implementation of the FizzBuzz challenge in Ruby: ...
1
vote
0answers
17 views

What kind of exception to ask for out of range arguments? [migrated]

What type of exception should I throw? I have a console class which describes rectangle of cells which a user can index by passing in a coordinate: ...
10
votes
3answers
225 views

Git pre-commit hook to check that changes to certain files only happen on the master branch

As a beginner in Ruby, I am trying to optimize this script that is a client side pre-commit hook for git. Was hoping I could get a review to ensure I am following all ruby idioms, and to ensure I ...
12
votes
2answers
105 views

Nokogiri crawler

The following code works but is a mess. But being totally new to Ruby I have had big problems trying to refactor it into something resembling clean OOP code. Could you help with this and explain what ...
8
votes
5answers
244 views

Convert total minutes into “Hours:minute:seconds” for marathon data

The point of the script below is to read in the 10th column which is the total number of minutes a runner took to finish the marathon and convert it into form of "hours:minutes:seconds". Is there a ...
6
votes
3answers
86 views

Generate random 10% of file to be used in testing with Ruby

I'm new to Ruby, but not to programming. I just need a simple script that, given a text file, will pull out around 10% of the lines at random. Below is what I came up with based upon a Python script ...
3
votes
2answers
122 views

Building a list - why is this code inefficient?

I'm attempting this code challenge. Here's my code modified to handle one simple example test case: ...
5
votes
1answer
59 views

Project Euler code evaluation - digit fifth powers

This is for Project Euler #30: Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits: \$1634 = 1^4 + 6^4 + 3^4 + 4^4\$ \$8208 = 8^4 + ...
4
votes
1answer
67 views

Better way of checking if the current user already like a post

I have three models which look like this: ...
1
vote
1answer
37 views

Cleaning up a class that contains the object it represents [closed]

I've recently started writing a tic-tac-toe game (following the Odin project) and am trying to write a class to hold the board game. What I'm not sure about, is that the board class holds all the ...
7
votes
1answer
140 views

Less-repetitive code for document-analyzer

I've refactored my code such that every function has only one responsibility. Now I'd like like to work on making it DRY. I'm noticing a lot of duplication in the ...
4
votes
1answer
72 views

How could this “complex method” for codeclimate be refactored?

I'm trying to figure out how to refactor some code based on what codeclimate is telling me. So far it seems that any method that is longer than 5 lines or has an ...
1
vote
0answers
50 views

Variable assign expression with multiple expressions

I have three types of variable assign expression with multiple expressions. It looks ugly, but I'm not sure how to make it more readable. Type 1 ...
0
votes
1answer
44 views

Better practice to find the max/min element in a loop

Is this the most elegant way to avoid declaring additional variables outside the following_triggers loop? ...
11
votes
3answers
872 views

The Love-Letter Mystery

Problem Statement James got hold of a love letter that his friend Harry has written for his girlfriend. Being the prankster that James is, he decides to meddle with it. He changes all the ...
1
vote
0answers
38 views

Written worker using sidekiq for activerecord class

I have two model classes as follows: ...
2
votes
1answer
51 views

Ruby Bot application [closed]

I just want to know if this looks good. ...
1
vote
1answer
50 views

Better way of comparing integers

I wrote this class which is comparing some values, how can I improve its performance and maybe make it prettier? You can see at the bottom what type of data it requires, also the array size can reach ...
1
vote
0answers
10 views

Memcache Keys Management Class

I have written below class to manage Memcache keys. My Memcache keys structure are like: ...
6
votes
2answers
59 views

Item-based collaborative filtering

I wrote the following code using Ruby to compute item-based collaborative filtering (using Jaccard coefficient of similarity). I would appreciate feedback about any potential issues with the code ...
2
votes
1answer
51 views

Performance of Codility MaxCounter challenge solution

I'm doing some of the Codility challenges using Ruby. The current challenge is "The MaxCounter," which is described as: Calculate the values of counters after applying all alternating operations: ...
6
votes
3answers
64 views

Learning Ruby Caesar Cipher

I am trying to learn Ruby and made a simple Caesar cipher program and I was wondering if the following code follows the 'ruby way' or the way most ruby users would do it ...
3
votes
4answers
77 views

How can I prettify blocks with more than 2 of each block?

Actually, I just have no ideas. How can I make breaklines to make it more readable? Can anyone suggest ways on improving this? ...
1
vote
0answers
30 views

Improving argument passing to dumb tasks

I'm becoming acquainted with ruby by taking some of my legacy shell scripts and rewriting them as .rb The pattern that is emerging is that I have a number of different dumb tasks, that do what they ...
1
vote
1answer
52 views

Replace outside comma with newline

I'm looking for a better alternative to replace insert + gsub. ...
3
votes
3answers
126 views

How would you refactor this case statement?

I've got this code in my ApplicationHelper file: ...
3
votes
0answers
54 views

Validating control structures in a Ruby chess AI

I'm trying to reimplement a bit of C code (the Micromax chess algorithm) as Ruby in order to run in a Ruby environment. It's mostly done, but I'm concerned about the control structures, for a few ...
0
votes
1answer
31 views

Store location with some excluded path

This code stores the last know location in the session. The location is not stored if it is one of the exceptions in the if statement. ...
4
votes
0answers
33 views

How to create a 'tee' command in Ruby for a Unix like pipeline?

I came across the question about creating memory-efficient Ruby pipe class with lazy evaluation. Some code is given to effectively create a pipeline of commands using lazy enumerators. I have been ...
3
votes
0answers
73 views

Refactoring eBay webform into a more structured pattern

I've been working with this code for quite some time, trying to figure out how I can break it up. It feels very clunky and unorganized. The code takes in a hash that comes from a webform and also ...
1
vote
0answers
43 views

Writing an MVC project for the first time - how close am I?

I've recently started programming (learning Ruby) and wrote a small game. Now I've started reading about MVC and am trying to refracture my code to follow that architecture. The only classes with big ...
2
votes
1answer
45 views

Unit-testing account transaction code

I'm playing with unit-testing in Ruby. There is a situation that I don't know if is good enough or if I am doing the wrong abstraction. I have two classes: ...
3
votes
0answers
89 views

Ruby on Rails text editor

I'm currently building a text editor using contenteditable and RoR back end. As the editor is WYSIWYG and trying to follow DRY, I used only one file to 3 actions: Show, Edit and New. Edit and new ...
7
votes
4answers
307 views

Ruby-ize for loop - counting all the n-digit numbers that contain the digit 5 anywhere

I wrote this a while back when my fiance was taking a Number Theory class. I wrote about it here and it recently came back to my attention. Anytime I write a for ...
4
votes
3answers
103 views

Search for User by first and/or last name, efficiently

I am refactoring a user search that just feels dirty. (Users#search) I need to allow a blank param to search on partial or only a first or last, but don't want to return all the records in the ...
1
vote
1answer
52 views

Is my code clean enough and standard when processing Ruby on Rails requests using User's time zone?

I have an application in Ruby on Rails (3.2) and I want all the requests to take into account the currently logged in user's time zone. Also, I want to have a helper that will let me display date ...
4
votes
1answer
98 views

Merging with different modules

We have an action merge for two projects. In the projects we have different modules like Feed, Files, Post etc.... ...
7
votes
2answers
113 views

A Ruby sorting program for multiple criteria

This code review expands on an earlier one that deals with generating a football table from match data. This review deals with ordering said table based on several criteria. Your ...
1
vote
1answer
30 views

Move update_attributes into class and check for non-nil params to update only

There are a number of things wrong with the code here: ...
4
votes
1answer
62 views

Small football table builder in Ruby

Please review this small program I wrote to create a World Cup Group Table. You feed the program match info, and it builds a table based on that info. ...
5
votes
2answers
49 views

Refactor .each where each is redirected into a hash

What would be the best way to refactor this : ...
2
votes
1answer
32 views

Avoid calling nested method two times

For a given file name path, I need to get the folder one level before it. This can be accomplished in ruby with the following code: ...
2
votes
2answers
30 views

Refactor mass assigning from a hash

Here is the piece of my controller action that takes a session[:auth] hash and then assigns the values to the ActiveRecord object represented by ...
4
votes
0answers
107 views

Hacking Ruby Hash: Fastest #to_struct method

I am trying to make the fastest #to_struct method in Ruby's Hash. I am including a use case and benchmark so you can run and see if you have really improved the ...
2
votes
1answer
58 views

Ruby on Rails: refactor big search method

I have big search method in my model that corresponds to search a proper Car. It looks like this: ...
4
votes
1answer
102 views

Optimize a zscore algorithm

I wrote a zscore algorithm in Ruby that runs fine, but is incredibly slow when I have 8000+ entries to score. Can anyone help me figure out a way to improve my code, please? ...