Search Results
Search type | Search syntax |
---|---|
Tags | [tag] |
Exact | "words here" |
Author |
user:1234 user:me (yours) |
Score |
score:3 (3+) score:0 (none) |
Answers |
answers:3 (3+) answers:0 (none) isaccepted:yes hasaccepted:no inquestion:1234 |
Views | views:250 |
Code | code:"if (foo != bar)" |
Sections |
title:apples body:"apples oranges" |
URL | url:"*.example.com" |
Saves | in:saves |
Status |
closed:yes duplicate:no migrated:no wiki:no |
Types |
is:question is:answer |
Exclude |
-[tag] -apples |
For more details on advanced search visit our help page |
Ruby is a multi-platform, open-source, dynamic, object-oriented, interpreted language created by Yukihiro Matsumoto (Matz) in 1993.
9
votes
How clear is this Ruby code?
Took around a 100 seconds to understand. Would be twice faster if I had a commentary: # this code merges two presorted files.
All is pretty ok, but what you forgot here is to close all opened files. …
-1
votes
Generating sequence that uses the previous outcome for the current
Wolfram Mathematica has NestWhile, that can be implemented in Ruby like this:
def nest_while f, expr, test
expr = f[expr] while test[expr]
end
nest_while ->i{ (i**2).tap &method(:puts) }, 2, ->i{ i … <10000 }
4
16
256
65536
=> nil
Also you might be interested in implementing NestWhileList: using yield like at SO: Are there something like Python generators in Ruby?. …
0
votes
iterate over an array and delete elements conditionally
You may use drop_while to get rid of iterative operator break:
@urls = @urls.drop_while do |url|
begin
doc = perform_request(some_params)
false
rescue TimeoutError
Rails.logger.warn(" …
4
votes
How do I generate a list of n unique random numbers in Ruby?
in last if x = #{x}"
puts "in last if i = #{i}"
array[i] = x
puts "#{array[i]} is in the array"
p array[i]
else
redo
end
end
p array
Now refactoring:
You need to know, that Ruby … type == "no"
puts "in last if type=#{type}, x=#{x}, i=#{i}"
array[i] = x
puts "#{array[i]} is in the array"
else
redo
end
end
p array
Then get rid of "yes" and "no", and use native Ruby …
3
votes
Subtraction accumulator
I don't understand, why do you need .first and .drop:
def subtract(*numbers)
numbers.inject{ |acc, x| acc - x }
end
Or:
def subtract(*numbers)
numbers.inject :-
end
2
votes
Accepted
Creating consecutive words from a string
As @Michael Szyndel mentioned in a comment, Array#combination is the more appropriate method to use.
def get_separated_tokens query
tokens = query.split
(0..tokens.size).to_a.combination(2).map{ …
2
votes
Accepted
Item-based collaborative filtering
It appears, that these lines of code are junk you forgot to remove:
unless child["activities"].include?(generic_activity)
Instead of accumulating set, just use users_for_activities.keys.
Replace: …
1
vote
Accepted
Destroy script for provisioning
Use Kernel#abort:
puts "[#{$me}] ERROR: host to destroy must be in first argument. Exit."
exit 1
will behave the same as:
abort "[#{$me}] ERROR: host to destroy must be in first argument. Exit."
…
1
vote
Accepted
Sum of numbers that equal 100
first, :+)
end
end
end
end
end
0.0013sec vs 2.0sec
Making it recursive (break on any depth) could achieve the optimal speed, but such solution becomes not that so easy to read as Ruby …
1
vote
A pattern to destructively extract items from an array
You need #partition:
irb> a = (1..9).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
irb> a.partition(&:odd?).tap{ |y, n| a = n }.first
=> [1, 3, 5, 7, 9]
irb> a
=> [2, 4, 6, 8]
3
votes
Accepted
OO coding style in short Ruby scripts
Never use OOP just because people say it's cool. If you do not know how to apply it, that means you just do not need it in this task. That's it. Forget about OOP. Involving it without much need only m …
0
votes
Determines whether a number is a power of 2
You also can convert the number to binary string form and match it with a regex:
!!num.to_s(2)[/^10*$/]
1
vote
Accepted
Implementation of insertion sort in Ruby, code correctness
arr[compare], curr) :
arr[compare] < curr
)
arr[compare + 1] = arr[compare]
end
arr[compare + 1] = curr
end
end
But looks like Insertion sort and Ruby …
8
votes
Accepted
Rails authentication checks
Another view on @tokland solution:
def authenticate!
return fail! unless account = Account.find_by(subdomain: subdomain)
return fail! unless user = account.users.find_by(email: params["user"]["em …
4
votes
Splitting a text file into paragraphs and words
Anyway try to avoid usage of i in Ruby. You shouldn't bother with indexes.
I assume you wanted to get a three-dimensional array, not the array and some hashes. …