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
59 votes
4 answers
2k 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: ...
rob0t's user avatar
  • 593
39 votes
2 answers
2k views

It'th wabbit theathon

I don't know regex. I want to learn regex, but learning regex just for the sake of regex would be quite boring. So I decided to learn Ruby as well (which so far looks a lot like Python, which I'm ...
Mast's user avatar
  • 13.1k
30 votes
4 answers
26k views

Setting defaults in a Ruby options hash

I have a specific method that I like because it lets me decide whether or not I want to use the default. If I want anything different I enter in :option => value ...
David West's user avatar
28 votes
4 answers
15k 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 ...
Steven's user avatar
  • 281
26 votes
2 answers
2k 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 ...
citylights's user avatar
25 votes
5 answers
55k views

Use of a regex stored inside a YAML file

I installed settingslogic and in the configuration file I put the regex for the email as follows: ...
ecoologic's user avatar
  • 785
23 votes
2 answers
4k 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 game-...
Felix Alcala's user avatar
22 votes
5 answers
4k 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 ...
paulus's user avatar
  • 407
22 votes
5 answers
7k views

FizzBuzz in Ruby

I have this implementation of the FizzBuzz challenge in Ruby: ...
Undo's user avatar
  • 790
22 votes
1 answer
3k 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 ...
Flambino's user avatar
  • 33k
20 votes
1 answer
496 views

Are your servers down...?

If you came here because of the title, chances are your server isn't down. But if by some miraculous reason it is: A while ago, I posted a program that would ssh and ping all the servers at work. I ...
13aal's user avatar
  • 822
19 votes
2 answers
543 views

Count the occurence of nucleobases in DNA string

Inspired by this meta question I decided to take a look at Rosalind. Their first challenge seemed easy enough: An example of a length 21 DNA string (whose alphabet contains the symbols 'A', 'C', 'G', ...
Mast's user avatar
  • 13.1k
18 votes
4 answers
1k 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 ...
jasonb's user avatar
  • 283
17 votes
3 answers
25k views

Ruby function to join array with commas and a conjunction

Should take an array such as ['dog', 'cat', 'bird', 'monkey'] and return 'dog, cat, bird and monkey'. Looking for a more ...
Larsenal's user avatar
  • 273
17 votes
2 answers
1k views

Battleship Challenge: Naval Build-up

The community challenge for this month says: Everyone has played Battleship. Let's implement the logic that sinks one. But that presumes that there's something to sink. We can't have the armada ...
Flambino's user avatar
  • 33k
16 votes
3 answers
3k 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 words ...
Mohammad's user avatar
  • 1,212
16 votes
2 answers
3k views

Deaf grandma doesn't allow me to leave until I say bye three times

So, I'm working through Chris Pine's "learn to program" and I have been banging my head against the wall regarding the "deaf grandma" lesson. I think I got it. I'm looking for advice regarding ...
J.R. Bob Dobbs's user avatar
15 votes
4 answers
14k views

Setting the default display name for my model

I have a method in my Ruby code that sets the default name for my model. Rubocop complains that Assignment Branch Condition Size is too high, 21.24/15. How can I improve this? ...
Antarr Byrd's user avatar
15 votes
1 answer
285 views

Name/word generator using DTMC in Ruby

I just wrote a basic DTMC algorithm focused on generating names, though it could be used to generate lots of other things. It's designed to be run from the command line, so input is taken from a file, ...
user avatar
15 votes
1 answer
479 views

Ruby chess engine gem

This is my first Ruby gem that claims to provide all the rules of the chess game. My goal was to keep this library as simple as possible via OOP principles, and I'd be glad to hear any feedback from ...
Anikeev Gennadiy's user avatar
14 votes
3 answers
447 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? ...
Jason Swett's user avatar
14 votes
2 answers
31k views

Early return, unless return value is nil [closed]

I have a lot of code that looks like this: value = TopicLinkClick.create_from(new_params) return value unless value.nil? # do something else I find this code is ...
Dzung Nguyen's user avatar
14 votes
2 answers
753 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 ...
Dave Gordon's user avatar
14 votes
1 answer
231 views

TicTacToe with AI in ruby - follow-up overload

I already posted this program numerous times, always surprised by the amount of improvement that is possible suggested by people here. Is it possible to make it even better, without nitpicking? At ...
Glubi's user avatar
  • 269
13 votes
3 answers
2k views

Counting words / lines in Ruby

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: “...
Sebastian N.'s user avatar
13 votes
2 answers
7k views

Handling many networking exceptions in Ruby

What would be a prettier/faster way to do handling of multiple errors in Ruby? Here is the code I am working with: ...
Mark Szymanski's user avatar
13 votes
4 answers
2k 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 ...
Anthony's user avatar
  • 439
13 votes
1 answer
3k 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, ...
David Harkness's user avatar
13 votes
1 answer
3k views

Object-Oriented Chess Game in Ruby

I wrote a chess game in Ruby using object-oriented principles. One of the challenges was deciding which particular methods/actions belonged to a particular class, as there were some that felt as if ...
scobo's user avatar
  • 571
12 votes
4 answers
6k views

Displaying the multiples of a given number

Here's a method I made that's supposed to print out multiples of any numbers up to any quantity: ...
user avatar
12 votes
3 answers
797 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 ...
CHK's user avatar
  • 221
12 votes
2 answers
928 views

Did not pass the interview about an RPN Calculator

I recently received a simple interview assignment as to implement an RPN calculator in Ruby. The program takes user input and quits when the user presses 'q'. I did it in under 30 minutes and was ...
johnathon's user avatar
  • 121
12 votes
4 answers
1k 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 ...
Michael Hampton's user avatar
12 votes
1 answer
5k views

Robot toy simulator

I have already flunked the test with this code so don't feel bad for cheating any employer. I didn't get any feedback though just wondering what could have gone wrong. You don't need to get into the ...
sethi's user avatar
  • 297
12 votes
1 answer
478 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 ...
a2bfay's user avatar
  • 215
12 votes
2 answers
1k views

Minesweeper: how many mines are near?

Minesweeper in Ruby (from exercism.io): Write a program that adds the numbers to a minesweeper board. Minesweeper is a popular game where the user has to find the mines using numeric hints ...
Spike's user avatar
  • 1,423
12 votes
1 answer
303 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 ...
user avatar
12 votes
1 answer
360 views

Fractal generator

Refer to the help message and title of this post for what the code does. I'm looking for tips on: Efficiency: While the find-and-replace approach I have works perfectly fine and does so in a ...
user avatar
11 votes
5 answers
723 views

Selection sorter

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. ...
upasaka's user avatar
  • 241
11 votes
3 answers
13k 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 ...
Nat's user avatar
  • 1,016
11 votes
3 answers
3k 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 ...
fr00z1's user avatar
  • 514
11 votes
2 answers
2k views

Luhn algorithm in Ruby

I'm learning Ruby 2.3 and I've tried to implement a function which performs the Luhn credit card verification algorithm on an input string, returning true if it ...
Aaron Christiansen's user avatar
11 votes
3 answers
3k 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 ...
MrPizzaFace's user avatar
11 votes
2 answers
216 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 ...
Mohamad's user avatar
  • 2,034
11 votes
3 answers
6k 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 ...
Joel C's user avatar
  • 111
11 votes
1 answer
2k views

Finding maximum deviation

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 for ...
yangtheman's user avatar
10 votes
4 answers
7k views

Regex to enforce password requirements

I'm trying to write regex to validate the password for the given rule. Passwords must be at least 8 characters in length and contain at least 3 of the following 4 types of characters: lower case ...
Bibek Sharma's user avatar
10 votes
3 answers
18k views

Roman numeral converter

I'm writing a program to convert integers to Roman numerals (naively -- it doesn't know how to do the subtraction trick yet). What I have is functional, but it is not "Good Ruby". ...
tom's user avatar
  • 263
10 votes
2 answers
446 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 ...
Aleksandr Levchuk's user avatar
10 votes
4 answers
3k views

Lottery number generator

...
Frommel's user avatar
  • 101

1
2 3 4 5
33