ruby

Require statements inside methods?

I am working on developing an API for a test suite. One of the methods in the API requires the use of a library that isn't needed anywhere else in the API. My question is whether the require statement for using the library should be placed inside the method or every time the API loads. The library isn't very large so it won't have a s...

how to unescape \\ in a regex that has been escaped by Rails?

Hi! I'm trying to store regexes in a database but they're getting escaped by rails. For example \w*\s\/\s becomes \\w*\\s\\/\\s in the database and when retrieved. I'm inserting trying to use them with mystring.sub(/#{regex_variable}/, ''), but the escaped regex is not matching as desired. What's the best we to resolve this so the r...

Directly Downloading a File From an RSS feed Using Ruby - Handling Redirects

I'm writing a program in Ruby that downloads a file from an RSS feed to my local hard drive. Previously, I'd written this application in Perl and figured a great way to learn Ruby would be to recreate this program using Ruby code. In the Perl program (which works), I was able to download the original file directly from the server it was...

In rails, how to g-zip a combined javascript file which generated by the 'javascript_include_tag'?

I was thinking javascript_include_tag with :cache option would g-zip the combined javascript file automatically: <%= javascript_include_tag 'j1.js', 'j2.js', 'j3.js', :cache => 'js_all' %> But it just generate a file which simply combined all those javascript files in plain text mod; Then how to g-zip the combinded file automatica...

RMagick with a direct form submitted Image from Sinatra

I am trying to do something quite simple using Sinatra and RMagick. Take a image, through a simple form file upload Use RMagick to resize it Then store it in a database for persistence (irrelevant) But after going through the RDocs and endless head banging testing I can't seem to get the form image to a RMagick object cleanly. This...

I know how to set class methods, but how do I set instance methods on the fly?

I asked a previous question on class methods, but I really want to understand how to do this for instance methods as well. Thanks! =) The code below sets class methods for a given array: class Testing V4_RELATIONSHIP_TYPES=[1=>2,3=>4] V4_RELATIONSHIP_TYPES.keys.each do |key| self.class.send(:define_method, "get_#{key}_type"...

Check if a command prints out a string that matches a regexp with Ruby?

I want to run: > ruby --version ruby 1.9.2p0 (2010-08-18 revision 29034) [x86_64-darwin10.4.0] and then see if 1.9.2 is printed out. If so, i return true. How would this method look like using a regexp? ...

how can I read a file, bytes at a time in Ruby?

I want to iteratively read a fixed number of bytes in a file, and return them My code is below. I have taken it from an example on the internet File.open('textfile.txt') do |file| while (buffer = file.read(size)) do yield buffer end end I get the error no block given. ...

The process for contribution to git packages?

Until now I have only used git packages with gem: gem install <package> Haven't contributed to correcting things to git-hub. I wonder how you all do it? Eg. when I have installed the gem package, should I manually create a folder where I do: git clone <repo> Then when I find the gem package behaving strangely, I just correct the ...

A RubyGems with documentation, source management and API documentation?

I have been wondering why so many coders have bad organization in their documents. I think I know why. As usual it has to do with standardization. If no standard it out there, everyone has to reinvent the wheel all the time. So I upload a gem at RubyGems.org. But that was only 1/4 of it all. I have to write a tutorial for it, making...

Add existing classes into a module

Hello, I have some existing ruby classes in a app/classes folder: class A ... end class B ... end I'd like to group those classes in a module MyModule I know I could do like: module MyModule class A ... end class B ... end end but is there a meta programming shortcut that could do the same so I could...

Regular expression not working when put in an object

Hi! I'm trying to store regexes in a database but they're not working when used in a .sub(), even though the same regex works when used directly in .sub() as a string. regex = Class.object.field // Class.object is an active record containing "\w*\s\/\s" mystring = "first / second" mystring.sub(/#{regex}/, '') // => nil mystring.sub(...

Reset loop, or return to start of loop, in Ruby? (Finding a number's factors)

I am trying to find the factors of a number N. I want to be able to iterate over my prime numbers array, and when a condition is met, go back to start of the iteration, rather than just continuing on. arrFactors = [] N = 150 [2,3,5,7,11].each do |a| if N % a == 0 arrFactors.push(a) N = N/a break if N == 1 <return t...

How do I add a strategy to Devise

I'm trying to add a really simple strategy to devise, and it doesn't seem to be working. Here is the code that I am trying to use #config/initializers/devise.rb Devise.setup do |config| config.orm = :mongo_mapper config.warden do |manager| manager.strategies.add(:auto_login_strategy) do def valid? params[:auto_l...

How do I configure Ruby alone with Apache + FastCGI?

Hi, There are many how-to's for people who want to use Rails with Apache and fastcgi. But I need to use Ruby alone without any frameworks (for educational purpose). So does anyone know how to configure Apache with FastCGI and to write a simple "Hello world"-style dispatcher on Ruby? ...

Why should I use File.join()?

I wonder why I should use: puts "In folder #{File.join ENV[HOME], projects}" Instead of: puts "In folder #{ENV[HOME]/projects}" I am aware of that File.join will put the appropriate separator (/ vs \) depending on the OS. The script is already so tightly tied to what version of ruby you are using, what gems you have installed and ...

ActiveRecord association not found - but only on second load

Hi All, I recently upgraded from Rails 2.0.2 to 2.3.8 and have been ironing out the kinks. This one is baffling me. I have a page that runs fine in production, but in development mode it runs fine on first load, then on reload it crashes with: ActiveRecord::ConfigurationError - Association named 'average_prices' was not found; per...

Runing a Thorfile?

Is there a way to run a file that is a Thorfile (named rails.rb) without having to install it? Cause initially I change the code every minute, so I don't want to be able to run that file directly without having to update it all the time. ...

Do you check in your rvmrc file?

Ruby Version Manager allows us to use an .rvmrc file in each project to set up which ruby version and gem set to load. Should this file be checked in to source control? Or does this presume too much about other developers' working environment? ...

RVM do not recognize system's ruby version

Hello, I am running ruby 1.8.7 (2009-12-24 patchlevel 248) on my fedora 13 machine. Now i want to try out ruby 1.9.2 so I installed RVM (rvm 1.0.1 by Wayne E. Seguin). I want RVM to know my system's ruby, but unfortunately when I run "rvm list" it doesn't list any rubies. How can I make rvm recognize hence switch between system's and r...