ruby

Rails' new_record? not reset (to true) if transaction is rolled back

So take the following test in Rspec: require 'spec_helper' describe Author do self.use_transactional_fixtures = false after(:each) do Author.destroy_all end it "should behave normal when using transactions" do my_author = nil begin Author.transaction do my_author = Author.new(:name => "My Name") ...

Packaging precompiled binaries inside of a gem

I've got a ruby web app that uses lilypond to generate sheet music based on user input. I'd like to move the hosting to heroku (I've recently used heroku on a few projects and really liked it, plus my traffic is low enough that it'd be free host it on heroku, for a while at least). However, heroku's dyno architecture doesn't allow you ...

Confusing output from time...

This command gets some data from MySQL and then manipulates it. Why is the 'real' time so much higher than the 'user' time? >time ./command.rb real 45m45.457s user 3m36.478s sys 0m28.226s For clarification - I understand the difference between the real, user, and sys output. However, I am confused at why there is such a gre...

How to sign data properly in Ruby (HMAC?)

I have a server (RoR app) sending information to a client (a Ruby Sinatra app) and I need a way for the client to be certain the data has come from my server, rather than an evil third party. The client has to login to the server before anything will be sent back the other way so the server could reply to the login with a shared key use...

What are the advantages and disadvantages of the require vs. import methods of loading code?

Ruby uses require, Python uses import. They're substantially different models, and while I'm more used to the require model, I can see a few places where I think I like import more. I'm curious what things people find particularly easy — or more interestingly, harder than they should be — with each of these models. In particular, if y...

python crypt.crypt in ruby?

hi i need this code in ruby I don't know how I write the crypt.crypt method in ruby, any ideas? (I want to simulate the linux comand .htpasswd) import random import crypt letters = 'abcdefghijklmnopqrstuvwxyz' \ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' \ '0123456789/.' salt = random.choice(letters) + random.choice(letters) pa...

Is it possible to recursively require all files in a directory in Ruby?

I am working on an API that needs to load all of the .rb files in its current directory and all subdirectories. Currently, I am entering a new require statement for each file that I add but I would like to make it where I only have to place the file in one of the subdirectories and have it automatically added. Is there a standard comma...

Why GetPixel() is so slow using Ruby (or probably Perl or Python too) and how to do it by other methods?

I tried using Win32's GetPixel() with Ruby and it is really slow even on a Quad Core Intel machine. I think for example, if I get a really small region such as 100 x 10 pixels = 1000 pixels and it can take 30 seconds to complete. Is there a better way such as by getting a region all at once? ...

Ruby: got TypeError when calling function. What's happening?

I am a ruby newbie and have a problem with getting the code below to work def factorial(n) if n == 0 1 else n * factorial(n-1) end end puts factorial(numbers) I keep getting the error message can't convert Fixnum into Array (TypeError) in `factorial'. Anyone able to help me with answer to what I am doing wrong in this...

Objective reasons for using Python or Ruby for a new REST Web API

So this thread is definitely NOT a thread for why Python is better than Ruby or the inverse. Instead, this thread is for objective criticism on why you would pick one over the other to write a RESTful web API that's going to be used by many different clients, (mobile, web browsers, tablets etc). Again, don't compare Ruby on Rails vs Dja...

How do I find a specific entry inside a zipped directory using the rubyzip gem?

I have a zip file named test.zip which contains a directory named invoice. Inside the invoice directory there are documents each with different names. I would like to find a specific document named summary.txt which is inside the invoice directory. I am able to get a handle to test.zip using the following: zip = Zip::ZipFile.open("/p...

How do I find .index of a multidimensional array

Tried web resources and didnt have any luck and my visual quick start guide. If I have my 2d/multidimensional array: array = [['x', 'x',' x','x'], ['x', 'S',' ','x'], ['x', 'x',' x','x']] print array.index('S') it returns nil So then I go and type: array = ['x', 'S',' ','x'] print array.index('S') it r...

Passing @object into a rails partial render

I have a partial: 'profiles/_show.html.erb' that contains code like <%= @profile.fullname %> I'm trying to render the partial but I'm not sure how to pass the @profile. I tried using local but apparently it sets 'profile' on my partial instead of '@profile'. <%= render :partial => 'profiles/show', :locals => {:profile => @app.profi...

Rails Community Engine plugin unit tests fail

Community Engine installed fine locally on my mac, but when I try to install it on my web server, it's giving me some trouble. Here's the problem I'm having now, when I run rake test $ [~/projects/polis]# rake test --trace (in /home/mculp/projects/polis) ** Invoke test (first_time) ** Execute test ** Invoke test:units (first_time) ** In...

How to have pdf viewer in ruby

How can I embeded pdf viewer in ruby on rail? Now I tried <embed src="MyPdfDocument.pdf" width="500" height="375"> but it doesn't work. I used firebug and see the error like this [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIStreamListener.onStopRequest]" nsresult: "0x80004005 (NS_ERROR_FAILURE)"...

Yield in view for multiple blocks

I am writing a small Sinatra-based app and would like each view to be able to insert various items into the layout, for example the page title or extra CSS/javascript references in the head. Currently my layout (erb) looks like this (simplified): <html> <head> <title>Hard Coded Title Here</title> <link rel="stylesheet" ... /> <...

Overriding the "for" keyword in Ruby. Is it possible?

I search around and tried overriding the "for" keyword but I found nothing. I am trying something like that: def for("maybe_arguments_go_here") print "Hello from function for!" end for i in 1..3 print "Hello from for" end ...

wxruby and rubymsn

Hello I'm currently playing with wxRuby and RubyMSN to learn to program desktop-programs. I know it is a hard task instead of just crating a notepad etc, but I need a bigger task than a notepad. I now do manage to use them by them self, but I cant get them to work together. The problem is the loop. RubyMSN wants to have an endless loop...

Can Ruby 1.9 used with Rails by a Ruby/Rails beginner?

About half a year ago, when I started to learn Ruby and Rails, I first tried Ruby 1.9 but I soon gave up, because at that time nothing worked out of the box and almost every helping blog or tutorial was designed for Ruby 1.8. What about now? (Dec 2009) Is it possible to get an existing Rails application running by a Ruby and Rails begin...

Best way to output a tag who's name is dynamically created?

In my Rails application I have a partial which is called recursively. In the partial I want to output a <h1>, <h2>, <h3> ... depending on the level. (Cap at level 6, of course) Something like this: <h1> <p><%= ... %></p> <% books.each do |book| %> ... <% end %> </h1> -------> <% open_h(1) %> <p><%= ... %></p> <% books...