ruby

Split a value and units in Ruby

What is the most elegant way to split a value and units, so you have: 13min.some_method["value"] = 13 13min.some_method["unit"] = "min" I think it should use unpack or split, but I can get it to work! ...

Does ActiveRecord::Base.connection.execute only execute a single statement at a time?

I have a bunch of SQL statements to execute on a database. (I'm doing things that Rails doesn't provide methods for, as far as I know: creating views, adding foreign keys, etc. It's mostly for non-Rails interaction with the data.) In essence, I'm doing the following: sql = "statement_1; statement_2; statement_3; etc;" ActiveRecord::...

Understanding Simple ActiveRecord Updates in Rails

I'm just diving into ActiveRecord and have not been able to find an answer to my question. If I am updating an object's attributes and then calling save()... will ActiveRecord save to the DB ONLY when the new values are different from the old values? Let's say I do something like this: thing_to_update = Thing.find_or_create_by_code(so...

How to access a property of an array in Ruby

I have this object of class Array >> answers_to_problem => [#<Answer id: 807, problem_id: 1, player_id: 53, code: "function y = times2(x )\r\n y = 2*x;\r\nend", message: nil, score: 12, output: nil, hide: nil, create d_at: "2010-02-02 11:06:49", updated_at: "2010-02-02 11:06:49", correct_answer: nil, leader: nil, success: true, clo...

JRuby and Test::Unit's assert_raise

I'm having trouble making assert_raise recognize java exceptions. I can do assert_raise(NativeException) { @iter.next } which works fine, but if I try to get more specific java_import 'java.util.NoSuchElementException' #... assert_raise(NoSuchElementException) { @iter.next } I get the error Should expect a class of exception, Ja...

Create an Array of Fixed Length Given an Array of Arbitrary Length

I'm looking to write a method that creates an array of a fixed length (in my case 12) from any array it is provided of arbitrary length (though the length will always be 12 or less) by repeating the objects in order. So for example given the array a: a = [1, 2, 3, 4] I would want to have returned: a = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, ...

When to use association extensions vs named scopes?

From a cursory glance, they appear to be simply two different approaches to the same set of problems, except that named scopes are chainable, while association extensions are not. Can anyone explain further, or provide an example that would be more appropriate for an association extension than a named scope? ...

Is there a Ruby 1.8.7 built-in to split an array into same sized subarrays?

I've got this going: def split_array(array,size) index = 0 results = [] if size > 0 while index <= array.size res = array[index,size] results << res if res.size != 0 index += size end end return results end If I run it on [1,2,3,4,5,6] like split_array([1,2,3,4,5,...

A book about RoR for the Ruby illiterate?

Is there a book about RoR for someone that has little or no understanding of Ruby? Although RoR is a great reason to start learning Ruby, I'd be bored out of my pants if I first have to read a full book on Ruby and then a book about RoR. Something that demonstrates the basic principles of both Ruby and RoR would be an ideal intro to th...

Troubleshooting: Rails can't save to database in production?

I could use a little help trouble shooting this problem. When using the app to create a new record nothing is being saved to the database. There are no visible errors presented. Dropping to the command line, and using the console with the same production environment, I can create a new object and save it (I have to bypass validations)....

How can I display the correct created and modified times in my webapp?

I'm working on a Rails application that's kind of like a blog. Users create Entries. I'm trying to work out how to handle time storage and display. I've read this writeup about Rails timezone support. It's great but it doesn't cover what my app needs to do. It only looks at cases where you want to convert stored time to the current logg...

Strip words beginning with a specific letter from a sentence using regex

I'm not sure how to use regular expressions in a function so that I could grab all the words in a sentence starting with a particular letter. I know that I can do: word =~ /^#{letter}/ to check if the word starts with the letter, but how do I go from word to word. Do I need to convert the string to an array and then iterate through...

Handle version conflict when multiple concurrent editor on the same model rails

how to Handle version conflict when multiple concurrent editor on the same model rails? for example user A click edit on the post controller and start editing the content user B click edit on the post controller and start editing the title user B is done and click save user A is done and click save and override the modifications done by...

RMagick freezes ruby processes when required on OS X Snow Leopard

I've got quite a strange problem. I've installed ImageMagick via homebrew. I've installed rmagick-2.12.2 and rmagick-2.11.2 with no issues. I get no errors on installation of either. ImageMagick seems to be installed and in my path: $ convert --version Version: ImageMagick 6.5.6-5 2010-01-30 Q16 OpenMP http://www.imagemagick.org Cop...

How to get custom form fields from controller -- a.k.a. how to not use date helpers in form

I created a form with scaffold that includes two datetime fields. I replaced them with a datepicker, and now I'm trying to grab the date from within the controller. Instead of having ruby do all the magic work with the datefields, I instead just have to fields, start_date and stop_date that I want to use, but I can't figure out how to g...

How do I get ruby-svm to look for libraries in macports' /opt/local?

I want to install Ruby SVM. I already have macports with normal settings, and installed libsvm via port just fine. But when I go to compile rubysvm, it barfs. :( Ruby SVM: http://rubysvm.cilibrar.com/download/ (most links are 404 though) libsvm: http://www.csie.ntu.edu.tw/~cjlin/libsvm/ Try this: sudo port install libsvm wget http://d...

Arrow keys don't work in irb/ruby command line scripts?

While using gets to accept user input, pressing the arrow keys outputs text to the screen, presumably the character codes. How can I prevent this from happening, and further how can I get the arrow keys to properly move the cursor around? irb(main):001:0> foo = gets ^[[A^[[D^[[B^[[C => "\e[A\e[D\e[B\e[C\n" Edit: maybe I should men...

Custom ActiveRecord finder invoking named scopes?

I have a custom finder defined below: class ContainerGateIn << ActiveRecord::Base ... def self.search(text) result = if text text.split(' ').inject(self) do |result, criteria| case criteria when /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/ result.search_by_date(criteria.to_date) else r...

Does dependency injection exist in Rails?

Does the fact that Rails have an MVC approach mean that is has dependency injection? Or is there a reason that we don't talk about dependency injection in Rails? If Rails does have dependency injection, what does it consist of? ...

Begin, Rescue and Ensure in Ruby

Hi, I've recently started programming in Ruby, and I am looking at exception handling. I was wondering if ensure was like finally in c#. So if I have the following: file = File.open("myFile.txt", "w") begin file << "#{content} \n" rescue #handle the error here ensure file.close unless file.nil? end or should I do this? #store the ...