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

learn more… | top users | synonyms

40
votes
4answers
1k views

Chat bot for posting recent answers

I am the half-robot side of syb0rg that will be posting the recent answers of Code Review to the CR Answers chatroom. Here is the list of review suggestions I would like, in order of preference: ...
21
votes
4answers
10k views

Unit-testing a controller in Ruby on Rails

I intend this to be a general question on writing an effective set of test cases for a controller action. I include the following ingredients: Ruby on Rails RSpec: A testing framework. I considered ...
18
votes
2answers
3k views

Ruby Koans' Greed Task

Having coded in Java and C# for quite some years, I'm currently learning Ruby. I'm working my way through the Ruby Koans tutorial. At some point, you are to implement a method that calculates the ...
17
votes
5answers
1k views

Parse webpage and save fetched images to a directory

I had a task for a programmer position which I failed. I am a newbie programmer and I accept that. The only problem is that employer never told me what the actual problem with the code is. So maybe ...
15
votes
5answers
2k views

FizzBuzz in Ruby

I have this implementation of the FizzBuzz challenge in Ruby: ...
15
votes
2answers
1k views

99 bottles of beer on the wall

I'm super new to this so please be nice. I tried solving Chris Pine's challenge to print the entire lyrics to 99 bottles of beer on the wall. This seems to work, but is there a better/more efficient ...
15
votes
1answer
619 views

Weekend Challenge: Ruby Poker Hand Evaluation

I'm late to this weekend challenge (sorry), but since it's all in good fun, I hope that's ok. I'm no poker player, though, so I may have completely overlooked something. The ...
14
votes
5answers
6k views

Use of a regex stored inside yaml file

I installed settingslogic and in the configuration file I put the regex for the email as follows ...
13
votes
3answers
311 views

Is there a more succinct way to write this Ruby function?

Just for fun, I want to try to write this Ruby function more succinctly. I imagine it can be done, I'm just not knowledgeable enough with Ruby yet to know how. Any suggestions? ...
13
votes
2answers
238 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 ...
13
votes
1answer
1k views

Ruby Sudoku solver

This week's weekend challenge #3 seemed like a great opportunity to learn Ruby! Unfortunately, my workload and looming vacation did not cooperate. :( The puzzle will make forced moves automatically, ...
12
votes
3answers
8k views

Generating a random alphanumeric string succinctly and efficiently

I want to generate a random alphanumeric string in ruby, as succinctly and efficiently as possible. The following solution works, but is obviously not very efficient. Please review the following ...
12
votes
1answer
134 views

Multiplayer bowling in Ruby, with variable skill

This is a multiplayer bowling simulator in Ruby. It allows for variable skill levels, and produces weighted random results for each roll based on those skill settings. The methods are grouped in four ...
11
votes
4answers
921 views

Genetic algorithm for Clever Algorithms project

I wrote a bunch of Ruby code for a book project I've just finished. One criticism is that it is fine code but not very "ruby like". I agree my style was simplified for communication reasons, and ...
11
votes
3answers
438 views

Pretty way of keeping sensitive info out of a logged command string in Ruby?

I have a long command that I am building with shovels (<<), with the intention of eventually running system(command). I'd ...
11
votes
3answers
2k 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 ...
11
votes
5answers
622 views

Help refactor this to be less procedural and more recursive

Reference: Is this a faithful rendition of the selection sort algorithm? I'm working through an elementary book on sort and search algorithms, and writing some sample code to test my understanding. ...
11
votes
3answers
986 views

Proper capitalization for book titles

I'm working through some exercises to sharpen my Ruby skills. The following code is how I solved this particular question. I'd like to know what other developers think of this solution. I'm self ...
11
votes
1answer
366 views

Faster way to find maximum deviation?

So, it was a part of my coding challenge and the last one, but I failed because it failed to yield a result in two seconds for some of sample inputs (out of six samples, three passed but three failed ...
10
votes
5answers
2k views

Is there a more ruby-esque way to display the multiples of a given number?

I'm learning Ruby since yesterday evening. Here's a method I made that's supposed to print out multiples of any numbers up to any quantity. ...
10
votes
3answers
308 views

Counting words / lines in Ruby - more compact / idiomatic way?

I solved this problem in Ruby: Write an utility that takes 3 command-line parameters P1, P2 and P3. P3 is OPTIONAL (see below) P1 is always a file path/name. P2 can take the values: ...
10
votes
3answers
773 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 ...
10
votes
3answers
4k views

Ruby Koans Proxy Project

I've just finished the Ruby Koans and one of the last projects was creating a Proxy class that sends methods to another class. I was wondering whether you would change anything about my solution (if ...
10
votes
4answers
349 views

Count cars passing in opposite directions

The problem comes from codility, whose coding problems I'm starting to enjoy as their evaluation criteria really looks at runtime. Task description A non-empty zero-indexed array ...
10
votes
4answers
144 views

Lottery number generator

...
10
votes
3answers
132 views

Making kexec reboots less painful

kexec is a way for a Linux kernel to directly boot another Linux kernel without going through the usual BIOS startup sequence, which can take several minutes on enterprise servers. The big problem ...
10
votes
2answers
134 views

Ruby format analyser

I have a requirement to validate file names related to architecture after they are uploaded. Once they have been uploaded I must warn the user if the file name is not standards compliant. What's in a ...
10
votes
1answer
118 views

Object-Oriented FizzBuzz in Ruby

I don't use Ruby on a regular basis, but I felt inspired to write an extensible, object-oriented version of FizzBuzz to sharpen my skills. I welcome all feedback. The main class: ...
10
votes
2answers
208 views

A Ruby implementation of derangement (secret santa) algorithm

A secret santa gift exchange is a game where each player is randomly assigned a person to whom they anonymously give a gift. The algorithm is referred to as Derangement. For example, given the ...
10
votes
2answers
265 views

Cash Register Kata

The Setup This kata is a spin on the old backpack problem. Please give feedback on both the kata itself as well as my solution. I'm not sure if the kata needs to lead the student more or if this is a ...
8
votes
5answers
584 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 ...
8
votes
4answers
1k views

Creating a 2D array of map tiles

I am a C++ programmer new to Ruby and someone suggested that I post my code here since it doesn't quite match Ruby standards. Is there a more Ruby way of doing this? I have created a 2D array full of ...
8
votes
3answers
195 views

Search for directories with a certain prefix that contain no files of a given type

These two functions search for directories with a certain prefix that contain no files of a given type. Does the code adhere to Ruby standards and conventions? Am I doing something the hard way or ...
8
votes
4answers
3k views

How would you refactor this if statement of a Rails controller?

I first saw this gigantic if and tried to refactor it. Could only end with a endless switch statement. Old code - ...
8
votes
1answer
289 views

Hot 18 game (modified Blackjack)

I've just completed my first Ruby game. It's a modified BlackJack type game, called Hot 18. I've used all methods for this Ruby game. It's a one-player game, but you can play with 1, 2, or 3 hands. I ...
8
votes
1answer
185 views

Spell a given number in English

I wrote a program that will take a number from 0 to 999,999,999,999 and spell out that number in English. How could I improve the readability of my code? ...
8
votes
2answers
387 views

A text editor driven file renamer in Ruby

I wrote the following Ruby script several years ago and have been using it often ever since. It opens a text editor with the list of files in current directory. You can then edit the file names as ...
8
votes
2answers
298 views

FizzBuzz from a casual rubyist

As a casual Rubyist I am mainly interested, how ideomatic my solution is. ...
8
votes
1answer
178 views

Outputting a .txt file from a .csv file

I wrote this little script in PHP a bit ago. It takes a .csv file and outputs a .txt file with certain data arranged in a "psuedo-array" looking format. I recently started messing around with Ruby so ...
8
votes
2answers
134 views

String substitutions in ruby and coding style

I wrote a short ruby script to generate various screenshots, and event though I am not completely new to ruby I rarely use it. Full script (41 LOC): https://gist.github.com/1229115 The string ...
8
votes
1answer
84 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 ...
8
votes
1answer
200 views

RubyGem for Enums

A little background first. I've been working on a fairly large Rails application which quickly grew into a breeding ground for "smelly" code. One antipattern we were using a lot was storing objects ...
7
votes
4answers
451 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 ...
7
votes
4answers
247 views

Adding intervals to an interval store

Write a program to do addition of intervals to an interval store. An interval is represented by an array of two elements - the lower and the upper bound (you can assume integers). Assume ...
7
votes
3answers
145 views

How clear is this ruby code?

Elsewhere, there was a question about an elegant solution to a problem. I'm curious whether this solution is elegant from the perspective of easy to understand for a ruby programmer (which I am no ...
7
votes
2answers
359 views

using method missing to reduce the code in ruby

I have a set of Class methods which is : ...
7
votes
3answers
148 views

Counting all pairs that differ by k

Given an array of numbers and another number k, how can I find the number of 2 elements such as their difference is equal to k? ...
7
votes
3answers
100 views

Calendar Print Method

I'm currently learning Ruby and need help in editing code to be more idiomatic. I have the method below I could use some pointers on how to make more "Ruby" :-) ...
7
votes
1answer
335 views

I've created a disaster. Please point out a coding strategy to refactor this.

Originally, I had everything in the base method #read_wall(). I'm not sure what happened with the array, but when I tried: ...
7
votes
1answer
496 views

Breaking a chess coordinate string into 2D coordinates

Given a chess coordinate as a string (e.g. "a1") I'd like to transform it into a 2-D array (so, for "a1" I'd like to get [1,1]). Here's what I came up with: ...