ruby

How to untaint execution of system command

I have a script that can be called by untrusted users, we need to make a call along the lines of this made up example: system 'somescript ' + data We need to ensure if data == 'filename; dosomethingelse' then the ; is escaped (or any other special character. The result is the shell command run is actually somescript filename\;\ dosome...

Ruby equivalent of Perl Data::Dumper

I am learning Ruby & Perl has this very convenient module called Data::Dumper, which allows you to recursively analyze a data structure (like hash) & allow you to print it. This is very useful while debugging. Is there some thing similar for Ruby? ...

how to let ruby call default browser to open localfile

In this question, I find that using system('start http://www.google.com') is OK. If the file is in local disk, though, using system('start file:///c:/temp/a.html') doesn't work. How do I have Ruby get the default browser to open a local file? ...

Stop file access from Ruby

I have a server application that allows users to execute their own ruby scripts. The server that the scripts run on is a virtual instance on Amazon's EC2 so no permanent damage can be done. However I'd like to take whatever precautions I can to stop any dangerous/malicious script, reboots are still something I'd like to avoid. At the mo...

Dynamically Generate Editable PDF

Is there a way to create an editable PDF programmatically? By editable, I mean you can click in a text area and type in your name, that kind of thing. I'm using Ruby and have found PrinceXML and Princely to be nice projects. I'm wondering if they could do that? ...

How to add task With hoe Rakefile

I'm creating a gem package with hoe library. The package shoud do "cd ext/lib/ && make " when "gem install pkg.gem" How to add task when package installing. # -*- ruby -*- require 'rubygems' require 'hoe' file ["ext/lib/*.c", "ext/lib/*.h"] do Dir.chdir "ext/lib" do sh "make" end end Hoe.spec 'mypackage' do |p| p.develope...

Delayed Job Rake Task Failing

I'm trying to get delayed job to work as a rake task, but for the life of me I can't figure out what I'm doing wrong. Given the following setup: #config/environment.rb Rails::Initializer.run do |config| config.gem 'delayed_job' end #Rakefile begin require 'delayed/tasks' rescue LoadError STDERR.puts "Run `rake gems:install` ...

Mocking ActiveRecord relationship beheavior in RSpec tests

I've run into this problem with testing. Let's assume I have two models, User and Post, where user has_many :posts. I'm trying to spec out a code block that includes something like this: user = User.find(123) post = user.posts.find(456) I know how to mock out the User.find and user.posts parts. The user.posts mock returns an array of...

Ruby and Enterprise Architect

I'm trying to build a class diagram in Sparx Enterprise Architect for future usage in Ruby project. How should I set up base code datatypes (Settings -> Code Datatypes...)? It should be something like: Datatype = Common Type (additional notes, if any). Does anyone know what pairs should be input? I'd also highly appreciate advice on h...

How to overwrite the dots in a ruby range?

Is there any possibility to overwrite the dots in a ruby range?. My aim is, to manipulate the given objects before the range is created. I thought of something like this require 'rubygems' require 'active_support' #actual i have to call explicitly .to_date Date.today.to_date..1.month.since.to_date #this should give me a range with Da...

Regular Expression - replace word except within a URL/URI

Writing a globalization module for a web application and I need a regexp to replace all instances of a word with another word (the translation) - except - words found within a URL/URI. EDIT: I forgot to mention that I'm using Ruby, so I can't use 'Lookbehind' ...

Ruby on Rails: Finding all topics in a certain category?

I'm trying to find all topics in a one particular category, but I'm not sure if this is the most efficient way to do it: Topic.all.select { |topic| topic.categories.include?(category) } The above works for me but it seems that it takes MySQL a long time to find the records. Is there anything more efficient? ...

Recover from failure of require/load in ruby

I recently discovered the Hanna RDoc template and I like it a lot more than the default. I want to use it in my project, but I also don't want my project to require it. The only change I had to make to my Rakefile to get the hanna template to work was to change require 'rake/rdoctask' to require 'hanna/rdoctask' Is there any way...

Ruby split with regex - regex isn't doing what i want...

i have this string string = "<p>para1</p><p>para2</p><p>para3</p>" I want to split on the para2 text, so that i get this ["<p>para1</p>", "<p>para3</p>"] The catch is that sometimes para2 might not be wrapped in p tags (and there might be optional spaces outside the p and inside it). I thought that this would do it: string.split(...

How do I include an instance method inside a before_save callback in a plugin?

Hi there, I'm creating a plugin and am having a hard time defining a before_save filter that calls an instance method I've just defined. Here's a quick sample: module ValidatesAndFormatsPhones def self.included(base) base.send :extend, ClassMethods end module ClassMethods def validates_and_formats_phones(field_names = [...

Thread-safe external process in ruby, plus checking exitstatus

I want to run a thread-safe piece of script in Ruby that calls an external program, then checks the exit status of that external program. What's the best way to do it? So far, I've been checking $?, but I think I'm getting a race condition with other parts of the program. Here's some example code: Thread.new do `external_program` i...

If given Model.find(:all), how to get the name of the Model/Class from the given result?

m = Model.find(1); m.class_name would give you "Model" If we have: m = Model.find(:all); How do we get the name of the model from m alone? ...

bug or desired behaviour? couldn't identify form using string 'post' but 'POST'. html contains 'post' though (ruby,mechanize)

the code that didn't work: login_form = page.form_with(:method => 'post') and code that works login_form = page.form_with(:method => 'POST') I inspected the form object via puts page.forms.inspect and got [#<WWW::Mechanize::Form {name nil} {method "POST"} ....] html source: <form class="login" method="post"> <fieldset> <legend>...

Ruby on Rails: Seeding data for a user upon signup?

I'm trying to create default seed records for every user that signs up to the app. I'm thinking I could use the after_create method in my users observer model: def after_create(user) user.recipes.create(:name => "Sample Recipe", :description => "This is a sample recipe.") user.cuisines.create(:name => "Sample Cusine", :description =...

RoR app running on mongrel development but not production

Hello all, This is my first stab at Ruby on Rails. Just deployed a very simple app to Heroku. The thing is that my app runs flawlessly on mongrel development; When I run it with "mongrel_rails start -e production" however, I get the error "We're sorry, but something went wrong." For the life of me, I couldn't debug this. Heroku logs i...