Ruby is a dynamic, open source programming language with a focus on simplicity and productivity.
1
vote
1answer
16 views
speed up square digit function
for the following function
#!/usr/bin/ruby -w
e=0
g=0
h=1
while h < 10000000
a = h
f=0
while f !=1 && f!= 89
d=0
f=0
while a.to_s[d] != nil
...
0
votes
2answers
43 views
A pattern to destructively extract items from an array
I want to efficiently (few intermediate objects) and neatly (few lines and easy to read) filter an array, removing and returning rejected items. So like a call to delete_if except instead of returning ...
0
votes
2answers
44 views
Re-factoring amateur ruby code
I am new to ruby and I would be grateful if I need help with re-factoring my code.
class Beam
def initialize
puts "Please specify span of beam"
@span = gets.chomp.to_f
puts "How many ...
1
vote
2answers
46 views
Quicksort in Ruby!
I've been teaching myself Ruby this weekend. First of all, I am aware that there is a built-in sort function. I've written this code strictly as an exercise.
I have background primarily in C# and ...
0
votes
2answers
42 views
ruby notes scrubber search for specific characters [closed]
I am looking to making a not scrubber for my testing notes. I want to run a script that will read the entire file looking for special "tags" that I made in my notes, like @ ? (c) etc...
I then want to ...
1
vote
2answers
43 views
How can I make an easy to understand subtraction accumulator?
I'm currently following the tutorials over at RubyMonk, and one of the problems I need to solve is to write a subtract function that would meet these conditions:
invoking subtract(4, 5) should ...
0
votes
0answers
8 views
How do you have threads in Ruby send strings back to a parent thread [migrated]
I want to be able to call a method that repeats x amount of times on a separate thread that sends messages such as "still running" every few moments to the console while I am free to call other ...
2
votes
1answer
66 views
General case of the 24-challenge problem
I'm working on an algorithm to solve the 24-challenge game. The basic idea is to combine positive integers using arithmetic operators to produce an expression that evaluates to exactly 24. For ...
0
votes
1answer
41 views
Suggestions for Ruby string parsing
I need to add quotes to each word in a string. Here is something that works but it looks ugly to me;
"this is a test".split.to_s.delete("[],")
produces
"\"this\" \"is\" \"a\" \"test\""
split ...
-2
votes
0answers
39 views
user has_role? please help me to review my code [closed]
this is the user model code:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and ...
0
votes
1answer
57 views
Math Calculus in ruby
I have several calculus to do in my ruby app.
My code is currently working but I find it very ugly.
@guestreviews = GuestReview.where(:reviewed_id => @user.id)
@hostreviews = ...
1
vote
3answers
80 views
Optimizing sum_combination_for(n) code
I'm working on a piece of code for calculating all the possible combinations of numbers that sum to n. For example:
sum_combination(3) = [
[1,1,1],
[1,2],
[3]
]
So my ruby code is this:
...
1
vote
1answer
43 views
Rails: Setting a transient attribute on a set of objects from one model based on information from a junction model
I have a user model, a task model, and a junction model user_task that saves the data joining the two:
class Task < ActiveRecord::Base
attr_accessor :completed_on
has_many :user_tasks
class ...
3
votes
1answer
127 views
Nasty Age Printing Method
I have this ugly age printing method. Can you do better?
def age(birth_date)
today = Date.today
years = today.year - birth_date.year
months = today.month - birth_date.month
(months -1) if ...
3
votes
2answers
49 views
Preventing Division by Zero
numerator: Value being divided.
denominator: Divisor value.
method so far:
def calc_percentage(numerator, denominator)
((numerator/ (denominator.to_f.nonzero? || 1 )) * 100)
end
What are the ...