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
57
votes
4answers
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: ...
36
votes
2answers
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 ...
28
votes
4answers
21k 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 ...
26
votes
4answers
14k 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 ...
24
votes
2answers
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 ...
23
votes
5answers
22k 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: ...
22
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 game-...
21
votes
5answers
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 ...
21
votes
5answers
7k views

FizzBuzz in Ruby

I have this implementation of the FizzBuzz challenge in Ruby: ...
21
votes
1answer
2k 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 ...
20
votes
1answer
344 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 ...
17
votes
4answers
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 ...
17
votes
2answers
442 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'...
17
votes
2answers
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 ...
15
votes
3answers
424 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? ...
15
votes
4answers
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? ...
15
votes
1answer
210 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, ...
14
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 words ...
14
votes
2answers
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 ...
14
votes
2answers
719 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 ...
14
votes
1answer
173 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 ...
13
votes
2answers
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: ...
13
votes
1answer
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, ...
12
votes
3answers
19k 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 ...
12
votes
3answers
1k 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: “...
12
votes
2answers
808 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 ...
12
votes
3answers
862 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 ...
12
votes
1answer
3k 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 ...
12
votes
1answer
352 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 ...
12
votes
2answers
650 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 ...
12
votes
1answer
248 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 ...
12
votes
1answer
299 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 ...
12
votes
0answers
80 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 ...
11
votes
4answers
4k 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: ...
11
votes
3answers
662 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
5answers
690 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. ...
11
votes
3answers
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 ...
11
votes
3answers
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 ...
11
votes
2answers
1k 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 ...
11
votes
3answers
5k 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 ...
11
votes
4answers
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 ...
11
votes
1answer
981 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 ...
11
votes
1answer
1k 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 ...
10
votes
4answers
6k 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 ...
10
votes
3answers
11k 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 ...
10
votes
2answers
20k 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 ...
10
votes
2answers
184 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
2answers
415 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 ...
10
votes
4answers
2k views

Lottery number generator

...
10
votes
2answers
496 views

Ruby script on-all-nodes-run not only for teaching

I wrote the following Ruby script several years ago and have been using it often ever since on a Bioinformatics computer cluster. It pulls out a list of hosts from the Torque queuing system ...