ruby

How do I convert an octal number to decimal in Ruby?

I am trying to find a clean way of referencing an array's index using octal numbering. If I am looking for the array index that is octal 13 it should return the value for a[11]. This is what I have come up with to accomplish it, but it doesn't seem very elegant or efficient: a = [ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62 ] ...

Race conditions in Ruby class methods

I'm working on an API integration project in Ruby and I was about to create a class method to somewhat follow what Rails does with the finder methods but I stopped short of doing so because I'm concerned I might introduce a race condition. I wouldn't be storing anything within the member variables, just instantiating objects with the th...

How to print array of integers into console quickly?

I have an array of integers a = [1,2,3,4] When I do a.join Ruby internally calls the to_s method 4 times, which is too slow for my needs. What is the fastest method to output an big array of integers to console? I mean: a = [1,2,3,4........,1,2,3,9], should be: 1234........1239 ...

Retrieving XRDS document in OpenID using Ruby

I am just playing around with the OpenID protocol. I am trying to send Discovery request and retrieve the XRDS document from google . When I try to do it from the terminal using the curl, I am getting the following output curl --url "https://www.google.com/accounts/o8/id" <?xml version="1.0" encoding="UTF-8"?> <xrds:XRDS xmlns:x...

validates_exclusion_of in Authlogic custom validations

Hi, I'm trying to use custom validations with Authlogic gem for Rails. In the method "acts_as_authentic" I'm just using some kind of validations such as "merge_validates_format_of", but now I'm trying to use "validates_exclusion_of" that's not working because there are only some of ActiveRecord validation methods implemented for Authlogi...

Combining Option Routes in Rails 2.3

I'm tring to setup a Route like so: GET /settings/ PUT /settings/ GET /settings/photos PUT /settings/photos This is the routing code that I have setup for it: #I just do this for code reuse get = { :method => :get } put = { :method => :put } pub.settings '/settings', :controller => :settings, :action => :index, :conditions => get pub...

Rails inconsistently fails to send datetime parameters

I am revising someone else's project which allows users to submit content either for either immediate or scheduled publication. There are a pair of radio buttons to select this, and a datetime field that is enabled by javascript if the radio button for 'scheduled' is selected <%= f.hidden_field :scheduled_state %> <%= f.radio_button :im...

Is Rails 3 modular like Merb and Ramaze

Hey, Will Rails 3 be a modular framework like Merb or Ramaze? By this I mean, can we use any persistence framework (DataMapper or Sequel) and jQuery in Rails 3 application (via the command line for example)? Or the default stack (ActiveRecord, Prototype) is still enforced? (Sorry, I'm pretty new to Ruby/Rails community). Thanks. ...

How can I change the background color of specific characters in a RTF document?

I'm trying to output RTF (Rich Text Format) from a Ruby program - and I'd prefer to just emit RTF directly without using the RTF gem as I'm doing pretty simple stuff. I would like to highlight specific characters in a DNA sequence alignment and from the docs it seems that I can either use \highlightN ... \highlight0 or \cbN ... \cb1 Th...

Can't turn array into a string error

I have a simple call JSON.parse(Panda.get("/videos/#{self.panda_video_id}/encodings.json")) Which returns : can't convert Array into String This is because the Panda.get("/videos/#(self.panda_video_id}/encodings.json") call returns an array in the new Panda 1.0.0 gem. I also tried : JSON.parse(Panda.get("/videos/#{self.panda_vi...

ruby regex question

I have the following string in a file called mib.txt: [name=1.3.6.1.2.1.1.5.0, value=myrouter.ad.local (OCTET STRING)] name the following code: f = File.read("/temp/mib.txt") name = f.match(/1.3.6.1.2.1.1.5.0/) puts "device name is #{name}" It returns the 1.3.6.1.2.1.1.5.0 just like I asked it to, but what I really want is to find ...

Ruby wrapper for apt-get

Is there a wrapper for package managers like apt-get and yum, written in ruby? ...

Do Ruby on Rails sites have performance issues?

Are Ruby on Rails site usually slower than java or .net sites? (This is assuming developers are not abusing the technology.) A lot of Ruby sites I have seen have performance issues. ...

Sanitizing RSS input in Rails

Hello, I'm pulling in the RSS feed from Craigslist into a rails app I'm building. When I try and insert content from the posts into my database, there's plenty of bad characters that cause the database to choke. I've tried a few different methods (the Sanitize plugin, hpricot, regexing the input) but nothing seems to work right. I'm ...

Best way to handle 404 in Rails3 controllers with a DataMapper get

It's very simple, I want to handle a normal [show] request with a call to DataMapper like I did in Merb. With ActiveRecord I could have done this: class PostsController def show @post = Post.get(params[:id]) @comments = @post.comments unless @post.nil? end end and it handles the 404 by catching the resource's exceptions. ...

nested rails gems and gem management

Rails is collection of several gems, all who's source resides in the rails repository, aka active_record has its own gemspec but is at github.com/rails/rails/active_record. While I use this stuff all the time, I dont really know the details of creating my own gems using this strategy. I have a project for work where certain codebases wil...

ruby regex find and replace

I have the following output: time = 15:40:32.81 and I want to eliminate : and the . so that it looks like this: 15403281 I tried doing a time.gsub(/\:\s/'') but that didn't work. Thanks for your help in advance ...

Why ActiveRecord::Base.connected? is false, after calling establish_connection

I develop Sinatra application and use there ActiveRecord for working with database, but I encountered one problem. I wrote a test for a model and it breaks with SQLite3::CantOpenException: unable to open database file Connection to database is established in test_helper.rb with the following code: Dir.chdir('..') do ActiveRecord::Ba...

How do I use a ruby regex to get a substring?

I want to get the numbers out of strings such as: person_3 person_34 person_356 city_4 city_15 etc... It seems to me that the following should work: string[/[0-9]*/] but this always spits out an empty string. ...

How do you do an inner product with ruby NArray?

I'm looking for something like the inner method in numpy. I have a 4 dimensional array called 'labels' and a one dimensional array (a vector) called 'discounts'. In numpy I can do numpy.inner(labels, discounts) and this will do the inner product between discounts and each row of the last dimension of labels returning a 3 dimensional arr...