How to randomly sort (scramble) an array in Ruby?
I'd like to have my array items scrambled. Something like this: [1,2,3,4].scramble => [2,1,3,4] [1,2,3,4].scramble => [3,1,2,4] [1,2,3,4].scramble => [4,2,3,1] and so on, randomly ...
I'd like to have my array items scrambled. Something like this: [1,2,3,4].scramble => [2,1,3,4] [1,2,3,4].scramble => [3,1,2,4] [1,2,3,4].scramble => [4,2,3,1] and so on, randomly ...
I am using dynamic fixtures and whenever I run my tests I am getting an error that thinks my association is a column, when it should be owner_id: ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'owner' in 'field list': INSERT INTO `companies` (`custom_host`, `name`, `created_at`, `updated_at`, `api_key`, `id`, `subdomain`, ...
I'm trying to create an API wrapper for Issuu using ruby and am running into errors when attempting to POST the data. When trying a simple GET through the browser with all the params in the querystring, I am able to retrieve the expected results; however, when I try to perform the same operation using a POST in code (which the API claim...
Hi, Can anyone please give directions on how to install ruby 1.9 I tried installation directions given all over the web. Can't get it to work. Please kindly give step by step direction. I tried using macports but everytime I type in ruby -v it gives me 1.8.6. Thanks Ken ...
How do I tell ruby to create files with the attributes FILE_ATTRIBUTE_TEMPORARY and FILE_FLAG_DELETE_ON_CLOSE? ...
I encountered a strange question about override initialize message of BigDecimal. class Test1 < String def initialize(a, b) puts a puts b end end require 'bigdecimal' class Test2 < BigDecimal def initialize(a, b) puts a puts b end end >> Test1.new('a', 'b') a b >> Test2.new('a', 'b') TypeError: wrong argument t...
Hi, could you tell me plz - how to write tests for projects, which uses in model establish_connection to connect another database? ...
I am new to ruby. OS: Windows XP Ruby Version --ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32] When I run gem update --system after installing ruby I am getting this error C:\Documents and Settings\abhisheksreepal>gem update --system Updating RubyGems Updating rubygems-update ERROR: While executing gem ... (Gem::Ins...
Has anybody got any ideas on this one? When we run: printf("%.0f", 40.5) On a windows box the return is "41" but on our production ubuntu server we're getting "40" ...
I'm not sure what the pattern of behaviour is but some programs - notably simple rack.ru based ones running in thin - can't easily be debugged. When a breakpoint is tripped, no source is available Note this isn't the same as this other S.O. [issue][1] [1]: http://stackoverflow.com/questions/323010/in-ruby-why-does-my-irb-interactive-de...
I'm working on a Chart class and it has a margin parameter, that holds :top, :bottom, :right and :left values. My first option was to make margin a setter and set values like this: # Sets :left and :right margins and doesn't alter :top and :bottom chart.margins = {:left => 10, :right => 15} It's nice, because it is clearly a setter, ...
I am trying to return the index's to all occurrences of a specific character in a string using Ruby. A example string is "a#asg#sdfg#d##" and the expected return is [1,5,10,12,13] when seaching for # characters. The following code does the job but there must be a simpler way of doing this? def occurances (line) index = 0 all_index ...
How do I set global configuration for RSpec in Ubuntu. Specifically so, --color and --format specdoc stay turned on, across all my projects (ie every time I run rspec anywhere). ...
Is there a nice way to tell AASM that if an exception is raised while processing any assm_event I want that error to be caught by a specific block of code? eg currently I do something like assm_state :state_1 assm_state :state_2, :before_enter => :validate_something assm_state :failed assm_event :something_risky do transition :fro...
Consider a "person" stored in a hash. Two examples are: fred = {:person => {:name => "Fred", :spouse => "Wilma", :children => {:child => {:name => "Pebbles"}}}} slate = {:person => {:name => "Mr. Slate", :spouse => "Mrs. Slate"}} If the "person" doesn't have any children, the "chilren" element is not present. So, for Mr. Slate, we ca...
On Ruby, when using DBM require "dbm" db = DBM.open("somedata") db[1] = 2 # ok p db[1] # gives error does anyone know db[1] = 2 is ok, but printing out db[1] will give error? If it requires db["1"] to be valid, then how come it doesn't apply to both cases but to one case only? ...
While trying to implement support for conditional GETting in a rest system, we have come across the fresh_when and stale? methods. The following code works fine with 304 and not further rendering: if stale?(:etag => resource, :last_modified => resource.updated_at.utc) respond_to do |format| format.html # show.html.erb ...
I am trying to capture screenshots on test failure using selenium-client and rspec. I run this command: $ spec my_spec.rb \ --require 'rubygems,selenium/rspec/reporting/selenium_test_report_formatter' \ --format=Selenium::RSpec::SeleniumTestReportFormatter:./report.html It creates the report correctly when everything passes, since no...
class User < ActiveRecord::Base end class Consumer < User end class Merchant < User end u = User.find(id) How do I type cast the variable u to the type Consumer? ...
Ok, so I wanted to create a hash which has an empty hash as the default value. A bit weird, I know, but let's say that I thought it might be useful. So here's what I did: >> a = Hash.new({}) => {} >> a[:a][:b] = 5 => 5 >> a => {} >> a[:a] => {:b=>5} >> a.keys => [] >> a.size => 0 >> a[:a].size => 1 In other words, I don't see hash me...