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

learn more… | top users | synonyms

3
votes
0answers
29 views

Heterogenous tree in the application domain: How do I represent them?

The Domain I have three types of items in my domain: ItemA, ItemB, ItemC. (I can't use their real names.) ItemA has one attribute: thing_id. ItemB has 6 attributes: thing_id, name, description, ...
2
votes
1answer
56 views

Bitwise operation to get the weekday name

The function days_name receive a val (string) corresponding to weekdays in hexadecimal. ...
5
votes
2answers
28 views

Adding credentialsID node to SVN location nodes

I'm new to ruby. Recently I wrote a script to add a credentialsID node to each SVN location node in each config.xml file for all ...
0
votes
0answers
31 views

Ruby: What is the best way to declare mutex and mutex.synchronize shortcut? [closed]

I am using mutex in my ruby code across many files. to deal with that I created a singleton class with one mutex object that I use all the time: ...
2
votes
1answer
40 views

Process a line in chunks via Regular Expressions with Ruby

I'm writing a Ruby script to reverse engineering message log files. They come from an external system with the following characteristics: Each line of the log file has at least one message. Each ...
1
vote
2answers
22 views

Rails Carousel View

In my web site, I created a carousel. Just now, I realized that I most likely repeated myself with this code. I would like to learn how to DRY up this code. Here is my view file (.html.erb) format: ...
3
votes
1answer
45 views

Check if a record already exists and update if it exists

I want to check if a record already exists in a database, and return true or false. If it exists, I want to update the ...
0
votes
1answer
32 views

Ruby Optimize Performance

I'm trying to do the Reverse Groups Challenge on Codeeval. My logic is function but when I try to submit my solution is says that timed out at 10 seconds. How can I improve the speed here? ...
5
votes
1answer
51 views

A conditional active record query

I have a conditional active record query that checks to see if a relationship of type X exists (.count > 0) where attribute x ...
4
votes
2answers
111 views

Reverse Game HackerRank Ruby Solution

Problem Statement Akash and Akhil are playing a game. They have N balls numbered from 0 to N−1. Akhil asks Akash to reverse the position of the balls, i.e., to change the order ...
5
votes
3answers
62 views

Cautious File Writer

I'm trying to follow the path of Sandi Metz and others who state that aiming for small and simple is good for writing understandable code. I've written a method that takes care of writing content to a ...
0
votes
1answer
64 views

Reducing the sorting time of a large number of numbers

I have to sort many large numbers in a given time limit. The program below works, but not when the test cases reaches \$10^6\$, in which case it exceeds the time limit. ...
1
vote
2answers
38 views

Ruby Anagrams Checker

I am quite new to Ruby, coming from a JavaScript background. I wrote this simple Ruby script that finds whether two strings are anagrams or not. To run it: ruby ruby_anagrams.rb 'script1' 'script2' ...
4
votes
2answers
55 views

DRYING up multiple controller action pages in Rails code

One of my rails controllers is horribly overcrowded with a bunch of methods that link to static web pages. Controller ...
4
votes
2answers
73 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 ...
8
votes
1answer
114 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? ...
2
votes
1answer
68 views

Graceful way to iterate through two arrays and build a master array?

params[:controller_id].each do |cid| params[:zone_number].each do |pzn| zone_ids << Zone.find_by_controller_id_and_number(cid,pzn).id end end I have ...
8
votes
1answer
65 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: ...
1
vote
1answer
30 views

Ways to express proc inside a method

I'm interested in seeing different ways the proc can be expressed inside a method that takes args. This method is meant to be a sort of FizzBuzz that will dynamically replace "Fizz" or "Buzz" as well ...
3
votes
1answer
29 views

Good approach to raise an exception

I have class House and module Lockable. Locking and unlocking House should reflect the real ...
1
vote
1answer
24 views

Checking if an associated object has failed to save/create

I smell something wrong here, but am not quite sure what the most idiomatic way to solve the problem. I have a controller for (job)listings and it has a method ...
2
votes
1answer
18 views

Find and select image files from webpage

For some reason, I feel like this is a bit messy and could be cleaner. Any suggestions? I'm selecting any image files ending in .png or ...
3
votes
1answer
25 views

Optimize mass import of XML to SQLServer in Ruby

I have the following Ruby code that's designed to update item, price and stock data for items in a MSSQL database. It's running on a Ruby 1.8.6/Rails 1.2.3 installation, in its own controller (for ...
4
votes
2answers
49 views

Generating sequence that uses the previous outcome for the current

I do not like to use while loops in Ruby. I was wondering how I can generate a "squared sequence" (e.g., squares the first input, then squares the outcome, etc.) in ...
5
votes
3answers
711 views

Repetitive controller with all methods nearly identical

I have an API with the following code ...
2
votes
3answers
49 views

Code to evaluate a dotted quad IP address has clumsy use of inject/fold - can I match the pattern in Ruby?

I wrote this to consume valid IPv4 addresses in dotted-quad decimal form. ...
7
votes
1answer
50 views

Command line flashcard game

I've built a simple command line flashcard game for learning Korean. I'm a beginner programmer, so I just wanted to make sure I'm following best practices, especially pertaining to OOP principles. ...
5
votes
2answers
37 views

Convert cardinal abbreviations into full cardinal direction

I'm still learning Ruby and I made the following function to convert "N" or "SE" into ...
4
votes
1answer
61 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: ...
3
votes
2answers
103 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
1k views

FizzBuzz in Ruby

I have this implementation of the FizzBuzz challenge in Ruby: ...
10
votes
3answers
283 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 ...
13
votes
2answers
128 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
261 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
102 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
134 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: ...
6
votes
2answers
74 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
77 views

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

I have three models which look like this: ...
1
vote
1answer
38 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
147 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
82 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
66 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
52 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
1k 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
61 views

Written worker using sidekiq for activerecord class

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

Ruby Bot application [closed]

I just want to know if this looks good. ...
1
vote
1answer
52 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
12 views

Memcache Keys Management Class

I have written below class to manage Memcache keys. My Memcache keys structure are like: ...
6
votes
2answers
66 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
100 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: ...