September 2010
38 posts
Answer by Mladen Jablanović for How can I get SQL... →
It seems thatm in Rails 2.x, a private method called ActiveRecord::Base#construct_finder_sql could be used, I need to test it more and see whether it will work for me: ActionType.find(:all,...
Answer by Mladen Jablanović for Iterate over a... →
Take look at Enumerable#each_cons method: ([nil]+array+[nil]).each_cons(3){|prev, curr, nxt| puts "prev: #{prev} curr: #{curr} next: #{nxt}"
} prev: curr: a next: b
prev: a curr: b next:...
Answer by Mladen Jablanović for Iterate hash for... →
As others have pointed out, Hashes are not about order. It’s true that 1.9 hashes are ordered, but that’s just a convenience, not their primary feature. If the order is important to you, just use…
How can I get SQL statement created by... →
I am using will_paginate with some complicated queries and it is unable to correctly calculate number of total records (in order to display proper number of page links) - namely because of…
Comment by Mladen Jablanović on Delete all the... →
File.open('/tmp/file', 'w') {} should be enough.
Comment by Mladen Jablanović on How can I get SQL... →
Thanks, I’ll keep that in mind when we migrate to 3. I forgot to mention that we’re still using 2.3.x, sorry.
Comment by Mladen Jablanović on How can I get SQL... →
Thanks, I’ll give it a try. Do you perhaps know whether it constructs SQL mimicking AR behaviour, or wraps AR somehow? I.e. can I rely that it will give exactly the same query AR#find would?
Comment by Mladen Jablanović on Ruby or Python... →
+1 for the Zen part about taking the slightly longer way learning. :)
Comment by Mladen Jablanović on Why cant I do if... →
@swanand: zero? and empty? are there in core lib (not sure which classes you had in mind, though).
RT @newsycombinator: Leaving .net http://j.mp/9tEHWD
Answer by Mladen Jablanović for validation before... →
From ActiveRecord::Base docs: Sometimes you want to be able to read the raw attribute data without having the column-determined typecast run its course first. That can be done by using the ...
Comment by Mladen Jablanović on Check if a... →
What’s the point of the first eval?
Comment by Mladen Jablanović on noob confused... →
I don’t think it’s very useful for Sequel?
Comment by Mladen Jablanović on noob confused... →
I think your question is too broad. What exactly puzzled you in the Sequel documentation? I think the README there is extremely well-writen, both for beginners and, as a reference, for experienced…
Answer by Mladen Jablanović for Fill sparse array →
Range.new(*rare.transpose.first.sort.values_at(0,-1)).inject([]){|a,i| a<<[i, Hash[rare][i] || a.last.last]
}
Step-by-step explanation:
rare.transpose.first.sort.values_at(0,-1) finds min and…
Comment by Mladen Jablanović on How to DRY up a... →
@Steve: Shouldn’t it be country.state.try(:name) instead?
Answer by Mladen Jablanović for Array of indexes... →
Functional, not-very-readable solution: (a[0,1]+a.each_cons(2).reject{|i,j| j-i==1}.flatten+a[-1,1]). each_slice(2).map{|i,j| i..j}
And a nice one: class Array # splits array to sub-arrays...
Comment by Mladen Jablanović on Better data... →
Just a small remark: prior to 1.9, Ruby’s Hash wasn’t ordered, so there’s no guarantee that each here would traverse the hash in the same order it was defined. If the order is important, one should…
Answer by Mladen Jablanović for Trace the code in... →
There is Kernel#set_trace_func which mostly covers what you require: proc takes up to six parameters: an event name, a filename, a line number, an object id, a binding, and the name of a…
Answer by Mladen Jablanović for Can you use Ruby... →
For example, you can write more-or-less standard RoR application, and access it from a browser on your touchscreen-equipped PC. The vendor should have provided accessibility features for it such as…
Answer by Mladen Jablanović for Create array... →
So, couple of things: Unlike in PHP, Ruby makes difference between arrays (Array class) and associative arrays (Hash class). You need key->value type of structure, you should use Hash, which you…
Koja je poenta potrošiti gornjih 50 piksela na ogroman naslov koji se ni ne vidi ceo?! #wp7 #fail
Answer by Mladen Jablanović for ruby variable... →
In Ruby you don’t “create variables”, you are creating objects (an array, in your case), and assign them to variables. So, in your example, you have a single array with two names, local_ar and ...
Comment by Mladen Jablanović on Calculate... →
@randomguy: Hey, thanks for the credits, it’s really nice to have an effort of such magnitude recognized! :o)
Answer by Mladen Jablanović for Calculate... →
There is a method. Here it is: class Numeric def percents self * 100 end
end 0.56.percents
=> 56.0
Comment by Mladen Jablanović on ruby string escape... →
@Gz Rain: You don’t have to escape anything if you are using parametrized queries, or ORM (which you do, if you are using ActiveRecord). ORM will do all the needed escaping for you.
Comment by Mladen Jablanović on Object methods and... →
You haven’t said anything about the actual object these methods belong to, and whether param is part of its state.
Answer by Mladen Jablanović for Why doesn't Ruby... →
I think there’s another reason: ++ in Ruby wouldn’t be remotely useful as in C and its direct successors. The reason being the for keyword: while it’s essential in C, it’s mostly superfluous in…
Comment by Mladen Jablanović on Ruby can only show... →
Let’s get this straight, you are judging language by its ability to deal with timezones?
Comment by Mladen Jablanović on Search ruby hash... →
Of course that any solution should traverse whole hash when there are no blanks.
Gledao “The Ghost Writer” Polanskog u bioskopu. Dobra fotografija i puno aluzija na Tonija Blera.
Comment by Mladen Jablanović on What does "" (two... →
@AboutRuby: A single literal on a line (specifically, on a last line in a method) shows very clear intention IMHO. It’s one of Ruby idioms.
Jel gledao neko novi Karate Kid, razmišljam da posle par godina posetim bioskop? :) Ozbiljno pitam.
Comment by Mladen Jablanović on Ruby print out a... →
Why should the first indicate “five give or minus a half”? Lack of decimal digits doesn’t have to imply rounding of any kind.
Comment by Mladen Jablanović on Does anyone have... →
I completely understand the attachment to VS (I simply can’t imagine writing C# without it), but, believe me, you don’t need it for Ruby. IMHO, the most valuable tool in Ruby is irb.
Šta fali novim #limundo pravilima, osim što destimuliše one kojima ne odgovara aukcija kao način prodaje?
Answer by Mladen Jablanović for Comparing the... →
In general, if you have array arr of N such arrays, you can just check if there are any duplicates there: arr.length == arr.uniq.length
because, for example: ...
RT @markoa: Cvetanje Tise - Radi se o tome da jedan insekt, izvesni Palingenia Longicauda, provodiu mulju reke kao larva… http://tumbl …