Ruby is a dynamic, open source programming language with a focus on simplicity and productivity.

learn more… | top users | synonyms

2
votes
1answer
9 views

XML Import class - Style improvements & Efficiency

I wrote a class to import a large XML file. It works great, but I feel like its ugly, and horribly inefficient although I do like how easy it is to follow what it does. How can I improve this to ...
0
votes
0answers
23 views

clicking blank links with Mechanize in ruby [on hold]

I made a script in ruby that uses mechanize. It goes to google.com, logs you in and the does an image search for cats. Next i want to select one of the results links from the page and then save the ...
1
vote
0answers
30 views

Rails ActionController API

I'm working on a Rails 4 App with an API as well. I'm working on the API right now and i wrote code for a controller that works the way i want it but i don't feel that i did everything the right way. ...
1
vote
1answer
92 views

Is there a flaw in my program?

Ok, so I was attempting to build a program using ruby that asks the user to input as many numbers as they want as long as it's more than 3, and it returns the mean, median, and mode of those numbers. ...
2
votes
2answers
66 views

How do I refactor this form object to lessen verbosity?

I wrote this form object to handle an e-commerce order form. My areas of concern are the verbosity of the populate method, and the repetition of the validations. I omitted some address fields and ...
1
vote
1answer
43 views

What are best practices of creating database importer in Rails?

I have sample database in GES files and I wrote importer in Ruby. Sample file: WAR_APO.GES: https://gist.github.com/regedarek/cd0ce73c5d1e14ff9818 I will have several of GES files which I want to ...
1
vote
1answer
46 views

First Occurrence of Number in Binary Search?

I am trying to write a function, where given a sorted array, and a number, it returns the FIRST index of that number. Is this correct? Code is in Ruby, but even if you don't know Ruby, you can pretty ...
1
vote
1answer
30 views

DRY Up Rails HTTP Basic Auth

Can anyone think of a way to DRY up this code or is there a way to make it more efficient or quicker. Its what authenticates a user with my API and i want to make it as fast as possible. def ...
1
vote
1answer
44 views

Replace URL parameters with Ruby

I have a method to replace URL parameters in an URL. It receives url as mandatory parameter and prefix and/or hash as optional parameters. Examples: url_replace( '/news?b=2', { b: nil } ) ...
0
votes
0answers
21 views

Ruby on rails with active record perfomance

I've got relation in my database as following : Customer has many Orders Order has many Parts Order has many Upc So my requirement is to copy all orders from one customer and attach or copy those ...
0
votes
1answer
45 views

How do I automatically call settings for each method?

I am using sendgrid gem to send emails and track by categories, class#method_name and arguments. class NotifyMailer < ActionMailer::Base #* 1) Extracting these 4 lines into separate ...
2
votes
2answers
62 views

More Ruby Idiomatic for creating consecutive words from string

This is method which creates an array of all possible consecutive substrings from from a string def get_seperated_tokens(query) result = [] length = query.split.count tokens = ...
1
vote
2answers
65 views

Eliminating nested each

I have an unusual data structure that I need to search - the structure is the result of parsing a JSON file returned from an HTTP request. The top-level object is an Array, but from top to bottom I ...
1
vote
2answers
126 views

get rid of if else statement

Any suggestion on improving this code. I know its badly typed. I am trying to find a best way to structure it. I want to avoid the if-else-statement and want to reduce the number of code lines. def ...
0
votes
2answers
89 views

Refactor or keep this high-complexity method?

Is this acceptable for readability or would it be better to refactor it into smaller methods? Tips? Comments? Examples? def save_with_payment if valid? charge = Stripe::Charge.create(:amount ...
1
vote
0answers
47 views

Ruby CSV reading performance problems

I (ruby beginner) have a performance issue with the following ruby code. The idea is to aggregate a CSV into a smaller, better-to-handle file and calculating the average of some values. Basically the ...
1
vote
1answer
63 views

Ruby string splicer

So I came across an interesting problem awhile back, and I finally got around to solving it. Basically, it needs to allow the user to provide two words, and have them progressively splice together, ...
0
votes
2answers
62 views

How can I DRY this Rails code?

users_controller.rb: class UsersController < ApplicationController def new end def create user = User.create(params[:user]) if user.id session[:user_id] = user.id ...
0
votes
1answer
30 views

A flexible Ruby Observable module

I was learning a bit about about observers. Ruby has an Observable module, but I decided to make my own with a few tweaks. The first important feature is that this module calls one or more observer ...
0
votes
0answers
24 views

How can I refactor Api::SessionsController?

I have session controller for mobile devices. I would like to refactor it a little, for example because my session controller is sub class of Devise::SessionsController I had to duplicate error ...
3
votes
1answer
51 views

Securely deleting files, while attempting to protect the user against obviously dangerous inputs

As far as I can tell, no gem currently exists to wrap shred, the *nix utility to securely delete files. I created a small class to do this, the key methods are: def self.run cmd ...
1
vote
0answers
46 views

An implementation of parallel workers in Ruby

Much in the spirit of this question, I have implemented a simple class for parallel workers and wanted to get some feedback on it. Can I make the code even more concise or readable? Are there any ...
1
vote
1answer
42 views

Implementation of insertion sort in Ruby, code correctness

To implement an insertion sort that sorts an array with optional block to determine sorting order, how can I improve this code? (Seeking best practices and code correctness) def ...
1
vote
1answer
39 views

How Can I Refactor This Code For Input Errors

I'd like to refactor the code below to account for if someone types in something besides a number 1 thru 5 or a comma. def roll_again puts "Which dice would you like to keep from this roll? (1, 2, ...
1
vote
1answer
47 views

Is this a good way of handling item reservations?

I have a site were we sell unique items. When a user clicks to buy an item we have to reserve it. This prevents other buyers from purchasing the item while the first buyer is checking out. When a ...
1
vote
1answer
51 views

How Can I Refactor the Yahtzee Program I Built in Ruby

require'yaml' require 'text-table' class Yahtzee def initialize puts "Welcome to Yahtzee, please enter your name." @name = gets.chomp @turn = 0 @scorecard = {"aces" => 0, "twos" ...
2
votes
2answers
77 views

Separate Numbers with Commas in Ruby

Help! My code works, but it is ugly!! As a newbie, how should I approach refactoring this code? def separate_comma(number) a = number.to_s.split('') b = a.size/3.0 if a.size < 4 p ...
0
votes
1answer
40 views

How should I refactor my image uploader script?

I come from a C background and need help refactoring the following imgur image uploader script I wrote. It checks if a token_file with access_token and refresh_token exists. If not, it instructs the ...
0
votes
1answer
48 views

In ruby how can I break down this method into smaller components?

For work, my colleague and I sketched this method up as a brain dump of how we want this method to work. What I am trying to figure out is how can we break this method into smaller components so that ...
3
votes
2answers
94 views

Common Ruby Idioms

So, I'm a JavaScript developer. I'm pretty sure that will be immediately apparent in the below code if for no other reason than the level/depth of chaining that I'm comfortable with. However, I'm ...
0
votes
0answers
57 views

Review tic-tac-toe code

tic-tac-toe using negamax for AI, my first real program in ruby is not looking too good, help me make it better! Some sore points are: def get_mark good_mark = false until good_mark ...
2
votes
3answers
85 views

Refactoring a single method with many (separate) if statements in Ruby

I have a Ruby method, and I've broken much of the logic into helper methods. This method takes a string and parses the syllables. Without getting into the helper methods, it's pretty easy to tell what ...
1
vote
1answer
41 views

Seeking code review for registering or logging in when creating a new comment object

This service object allows users without a session to create a comment, and register OR login at the same time. If a user is signed out he must enter an email and a password along with the comment. ...
0
votes
1answer
141 views

Review my ruby for performance

So I'm using the acts_as_taggable_on gem to tag 3 different models: Question, Blog, and Video Each one has a site_id to distinguish which site they show up on (SaaS-ish). I wanted to get all tags ...
1
vote
3answers
79 views

Shortening a multitude of && evaluations in this if statement?

I have these two functions and I was just wondering if there is any way to shorten the line of code with the many && statements. Shortening of the rest of the code would be cool too, but it's ...
0
votes
0answers
49 views

Am I correctly testing that methods from Kernel have been called?

Any time I'm testing a Ruby class that calls a Kernel method such as sleep or exit, i define a singleton method on the class instance and test that it has been invoked: it "must throttle requests" do ...
1
vote
1answer
50 views

content_tag concatenation clean up

I need to concatenate two content_tag and it works but it seems a little messy to me. Any thoughts on how may I refactor this? def format_price(money) mny = get_price_string(money).to_money str = ...
1
vote
1answer
85 views

define_method vs method_missing and CodeClimate scores

This may be a better fit for another board, but here goes: I've made a very simple gem that acts as a days-of-the-week bitmask. To be hip, I registered it on CodeClimate, where it's currently scoring ...
2
votes
0answers
52 views

Rails service + oauth code structure

I'm having trouble structuring the logic of OAuth integration into my web app. In the app, a user creates a Report that consists of data from their google analytics account. User steps: User clicks ...
1
vote
3answers
63 views

Ruby: Code review

I have recently started using Ruby and would like to hear your take on the following piece of code. class Generator def initialize @context = nil end def start(params) @context = ...
2
votes
2answers
73 views

Should I be using Regex to uppercase uncommon characters?

I've written a function for converting strings to upper case. It currently works by replacing each character using a Regex pattern with a hash: # Special upcase function that handles several ...
2
votes
1answer
70 views

What is the best practice for working with a project as nested modules?

I come from the world of Python, so I'm used to every file representing their own enclosed module, never polluting the global environment. Up until now (I've worked with Ruby for a week) I've created ...
1
vote
2answers
63 views

Manipulate Hash to typecast true/false for certain values

is there a better why how can I refactor this code, making sure that values in a Hash are typecasted to true/false if their value is '1' or '0' while leaving unaltered the rest? I'm using Ruby 2.0.0 ...
0
votes
1answer
69 views

Quick and dirty command line application in Ruby. Now it's time to refactor, where should I start?

#!/usr/bin/env ruby require 'pivotal-tracker' require 'yaml' TEMP_DIR = "#{ENV['HOME']}/.pivotal/" TEMP_FILE = TEMP_DIR + "temp.yml" CONFIG_FILE = TEMP_DIR + "config.yml" unless File.directory? ...
5
votes
3answers
163 views

What's a good way to create a sorted hash counting the occurances of a number in an array in ruby?

I figured something like this a= [1, 2, 3, 2, 2, 2, 3, 1, 1, 1, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] a.inj­ect({}) { |h, el| h[el]­ = a.cou­nt(el) if h[el]­.nil? ; h }.sor­t_by ...
1
vote
1answer
73 views

DRY up Rails Navigation Code

In my Rails 4 app i used to have every view contain a content_for :nav tag which included all of the links for the nav bar in my layout. This was getting annoying because if i wanted to add a button i ...
3
votes
3answers
271 views

Ruby multiples of 3 and 5 code challenge

I have implemented the first coding challenge on http://projecteuler.net/. I would love to get it reviewed by some expert rubyists! Thanks in advance :) # Multiples of 3 and 5 # # If we list all of ...
4
votes
2answers
164 views

Trying to get better at Ruby and programming in general- here's a simple TopCoder question in Ruby

I want to get general feedback and feedback about if I can make this more Ruby-like, if there are any obvious inefficiencies, and if I need to organize my class differently. The topcoder question is ...
1
vote
1answer
58 views

Clean up Rails Routes

Is there any way I can DRY up this Rails 4 routes.rb code? match '/oauth/apps/' => 'oauth/applications#index', :as => :oauth_applications, :via => :get match '/oauth/apps/new/' => ...
0
votes
3answers
98 views

Tell me how to improve my code?

I am a redneck coder that I was not happy! How can I improve this code: def show if params[:type]=="all" if params[:dir] =="next" @work = Work.find(:first, :conditions => ["id > ? ...

1 2 3 4 5 7