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.
1
vote
Splitting a range into min and max?
Update
@steenslag points out the minmax builtin. No monkey patching necessary.
template_min_datestamp, template_max_datestamp = Template.range(:created_at).minmax
0
votes
Manipulate Hash to typecast true/false for certain values
hsh = {}
preferences.each do
#something that updates hsh
end
A more idiomatic Ruby way to do this is with a map, as the answers show. This will in general result in more concise, expressive code. …
8
votes
Accepted
Formatting a price string
Code review-wise, I'd avoid long if-elsif chains and see about converting them to a case. Also, I'd move the final formatting to the end. Otherwise, not too bad except for the fact that there are much …
2
votes
"Which Century" programming exercise
I would be more formulaic regarding the century calculation:
century = (year - 1) / 100 + 1
Also, consider using .ordinalize from ActiveSupport (via require 'active_support/core_ext/integer' - you …
1
vote
Eliminating nested each
You may want to look into JSONPath, which gives you XPath-like querying of JSON objects.
Note that the leading zero in your sample JSON makes it invalid.
0
votes
Building XML by enumerating through array & hashes
A very clean approach is to use the xml-simple gem. Simply use xml_out with two options:
RootName to specify the XML root element
AnonymousTag to provide a tag name for your top-level hash
Other o …
2
votes
An abundance of ternary operators
The reason why it doesn't look clean is because the underlying models are inconsistent.
In one, you return a boolean true/false for age_eligible? and in another, you call it age_eligible_code and sto …
0
votes
Deal action with a pack of cards
I would model the domain with objects. A good set of classes would be Deck, Player, and Card. This will hide away implementation details and keep things easy to read. It also helps with naming, e.g. a …
3
votes
Should I be using Regex to uppercase uncommon characters?
One-to-one substitution is precisely what tr is for.
def norwegian_upcase(string)
string.upcase.tr('æ-ý','Æ-Ý')
end
That being said, the unicode_utils gem provides a method for this:
def norwegi …
3
votes
Accepted
Array even and odd indexes sorting and printing its values
It's a good idea to become familiar with all the methods on Array and Enumerable. It just so happens that Enumerable has a method called partition that has a purpose of dividing an array into .two par …
0
votes
Case statement with array includes?
Review
Overall, I don't see anything wrong stylistically. I wouldn't be too concerned about performance unless you have extremely large arrays -- do you expect either the values "buckets" or the inpu …
0
votes
Accepted
Ruby Hangman with JSON to save and load
One of the ways to structure your objects is to describe your problem in a sentence or two, then turn the nouns into classes, and the verb phrases into methods.
In this case, you may end up with Hang …
5
votes
Ruby implementation of Conway's Game of Life
I won't talk about style, because @tokland's comments are spot-on.
However, some quick notes about efficiency. For large board sizes, this implementation is inefficient.
For example, neighbors_to sc …
2
votes
Modifying an image URL based on hash options
Overall, it's reasonably tight code. You've taken steps to reduce method size, which is good.
To answer your questions specifically:
I think an instance variable would be better, because then you c …
2
votes
Project Euler - Smallest multiple (Ruby)
puts (1..20).reduce{|m,n| m.lcm(n)}
or even shorter:
puts (1..20).reduce(:lcm)
Because most of the calculations are done in the ruby implementation (e.g. C for MRI), this will be fast. …