I'm writing some Rails code for a partial view, and I want it to only show a comment field if somebody is already logged onto a site here.
If the page is viewed by someone who isn't a member of the site yet, the shared/comment_not_logged_in fragment should be passed in.
However, I'm totally stumped as to why I can't run the same check ...
In my quest to transition from a decade of C++ to Ruby, I find myself second guessing how to accomplish the simplest things. Given the classic shape derivation example below, I'm wondering if this is "The Ruby Way". While I believe there's nothing inherently wrong with the code below, I'm left feeling that I'm not harnessing the full p...
I have:
var keys = [ "height", "width" ];
var values = [ "12px", "24px" ];
And I'd like to convert it into this object:
{ height: "12px", width: "24px" }
In Python, there's the simple idiom dict(zip(keys,values)). Is there something similar in jQuery or plain Javascript, or do I have to do this the long way?
...
I need to derive an important value given 7 potential inputs. Uncle Bob urges me to avoid functions with that many parameters, so I've extracted the class. All parameters now being properties, I'm left with a calculation method with no arguments.
“That”, I think, “could be a property, but I'm not sure if that's idiomatic C#.”
Should I...
I believe you must be familiar with this idiom, which is sort of java's excuse for closures
//In the "Resource Manager" class
public void process(Command cmd){
//Initialize
ExpensiveResource resource = new ExpensiveResource();
//Use
cmd.execute(resource);
//Release / Close
resource.close();
}
//In the Client class...
manage...
If you want to check if something matches a regex, if so, print the first group, you do..
import re
match = re.match("(\d+)g", "123g")
if match is not None:
print match.group(1)
This is completely pedantic, but the intermediate match variable is a bit annoying..
Languages like Perl do this by creating new $1..$9 variables for mat...
I'm finding myself typing the following a lot (developing for Django, if that's relevant):
if testVariable then:
myVariable = testVariable
else:
# something else
Alternatively, and more commonly (i.e. building up a parameters list)
if 'query' in request.POST.keys() then:
myVariable = request.POST['query']
else:
# somethin...
Consider the following contrived example:
void HandleThat() { ... }
void HandleThis()
{
if (That) return HandleThat();
...
}
This code works just fine, and I'm fairly sure it's spec-valid, but I (perhaps on my own) consider this unusual style, since the call appears to return the result of the function, despite the fact that ...
I have a standard many-to-many relationship between users and roles in my Rails app:
class User < ActiveRecord::Base
has_many :user_roles
has_many :roles, :through => :user_roles
end
I want to make sure that a user can only be assigned any role once. Any attempt to insert a duplicate should ignore the request, not throw an error ...
Let's say there is a function like
void SendToTheWorld(const Foo& f);
and I need to preprocess Foo object before sending
X PreprocessFoo(const Foo& f)
{
if (something)
{
// create new object (very expensive).
return Foo();
}
// return original object (cannot be copied)
return f;
}
Usage
Foo foo;
SendToT...
if params[:parent_type] == "Order"
parent_id = nil
else
parent_id = params[:parent_id]
end
Would a Ruby person laugh at me for writing it this way? It doesn't seem particularly concise like some Ruby code I've seen.
...
I'm a Mac user, so I know that for Mac OS X, I'd like my games packaged up in a nice .app bundle (like Aquaria did, for example). But what is the standard on Windows? And what is the standard on Linux?
I'm relatively unfamiliar with both, but from what I understand, there's no equivalent of a Mac application bundle on either. Do users t...
I have a model that needs to have a field named complex and another one named type. Those are both python reserved names. According to PEP 8, I should name them complex_ and type_ respectively, but django won't allow me to have fields named with a trailing underscore. Whats the proper way to handle this?
...
I'm putting together some brief pages on programming and programming languages for a corporate wiki. We are not a software or IT company, but we have many technical employees (engineers, geoscientists...).
I'd like to implement a brief example program (think no more than 5x the complexity of "Hello World", if that's a measurable metri...
Ok, consider this common idiom that most of us have used many times (I assume):
class FooBarDictionary
{
private Dictionary<String, FooBar> fooBars;
...
FooBar GetOrCreate(String key)
{
FooBar fooBar;
if (!fooBars.TryGetValue(key, out fooBar))
{
fooBar = new FooBar();
fo...
I just invented a stupid little helper function:
def has_one(seq, predicate=bool):
"""Return whether there is exactly one item in `seq` that matches
`predicate`, with a minimum of evaluation (short-circuit).
"""
iterator = (item for item in seq if predicate(item))
try:
iterator.next()
except StopIteration...
I could do this easily in C++ (note: I didn't test this for correctness--it's only to illustrate what I'm trying to do):
const int BadParam = -1;
const int Success = 0;
int MyFunc(int param)
{
if(param < 0)
{
return BadParam;
}
//normal processing
return Success;
}
But I cannot ...
I'd like to know the best way (more compact and "pythonic" way) to do a special treatment for the last element in a for loop. There is a piece of code that should be called only between elements, being suppressed in the last one. Here is how I currently do it:
for i, data in enumerate(data_list):
code_that_is_done_for_every_element
...
How can you keep track of time in a simple embedded system, given that you need a fixed-point representation of the time in seconds, and that your time between ticks is not precisely expressable in that fixed-point format? How do you avoid cumulative errors in those circumstances.
This question is a reaction to this article on slashdot....
For instance, in Python, I can create a class like this:
class foo(object):
bar = 'x'
def __init__(self, some_value):
self.some_attr = some_value
...where bar is a class attribute and some_attr is an instance attribute. What is the idiomatic way to do things like this in Ruby?
...