ruby

Make a string "regexp-safe"

I wan't to make a string regexp safe in Ruby. I have: comment = "Just a comment someone makes" Word.find(:all).each do |word| comment.gsub!(%r{#{word}\s*}," ") end This replaces all words I've stored in the model Word with an empty space. The problem is if word contains for instance a left parenthesis "(" it will fail. Is there a...

How to make a Ruby string safe for a filesystem?

I have user entries as filenames. Of course this is not a good idea, so I want to drop everything except [a-z], [A-Z], [0-9], _ and -. For instance: my§document$is°° very&interesting___thisIs%nice445.doc.pdf should become my_document_is_____very_interesting___thisIs_nice445_doc.pdf and then ideally my_document_is_very_interest...

What is not "Least Surprise" in Ruby

Matz said : I designed Ruby to minimize my surprise. I wanted to minimize my frustration during programming, so I want to minimize my effort in programming. But sometime we get (bad) surprise in ruby practice. As beginner in ruby, I found some example : Exception Thread do not produce any immediate traces by default, we must do...

Reading MP3 audio data, or calculating the checksum thereof

Is there a Ruby library that will allow me to either calculate the checksum of an MP3 file's audio data (minus the metadata) or allow me to read in an MP3's audio data to calculate the checksum myself? I'm looking for something like this: mp3 = Mp3Lib::MP3.new('/path/to/song.mp3') mp3.audio.sha1sum # => the sha1 checksum of _only_ th...

Generate full url to javascript in rails (similar to javascript_path, but url)

Hi, How is it possible to generate an absolute link to the javascript file. I assume there should be something like the one below (which unfortunately does not seem to be available): javascript_url 'main' # -> 'http://localhost:3000/javascripts/main.js' instead of: javascript_path 'main' # -> '/javascripts/main.js' I need the a...

More elegant way to do this in Ruby

I've started with Ruby and am finding new, shorter, elegant ways to write code everyday. In solving Project Euler problems, I've written a lot of code like if best_score < current_score best_score = current_score end Is there a more elegant way to write this? ...

Concatenating string with number in ruby

I am total begineer in ruby so its very novice question. I am trying to concatenate a string with a float value like follows and then printing it. puts " Total Revenue of East Cost: " + total_revenue_of_east_cost total_revenue_of_east_cost is a variable holding float value, how i can make it print? ...

No colors when using autospec

When I'm using autospec to test a non-Rails Ruby project, I always have trouble getting my tests to show up red or green. When I run a regular 'spec spec/ --color' command I get normal red/green responses. I've tried putting all sorts of commands in the .autotest file and I can't get that to work. Also, in my Rails projects, I don't ge...

How can I improve this ruby code and my use of Hashes?

The point here is to browse the array docfiles and return two arrays (temporary_file_paths and temporary_file_names). I decided to return a Hash, but I feel I could get rid of the 2 temporary arrays but I'm not sure how... def self.foobar docfiles temporary_information = Hash.new temporary_file_paths = [] tem...

Given the session key and secret, how can we decrypt Rails cookies?

Hello all. I've got a question about how Rails handles cookie encryption/decryption. I've got this in my config/environment.rb config.action_controller.session = { :session_key => [some key], :secret => [some secret] } And this in config/environment/production.rb et al.: ActionController::Base.session_options[:session_...

Rails ActionMailer problems on Mac

I've been working on learning to use Rails the last couple days and I've run into something that I haven't been able to solve with Google. So I'm just creating a basic contact form that sends an email. Everything seems to be working ok in testing, which tells me that the form is working, and ActionMailer was implemented correctly, howev...

Where is the right place to put _changed? methods in Rails?

I have a user model that checks to see if a value has changed before_save (by running User.zipcode_changed?). The idea is that this will queue up a delayed job if it has. Problem is, when I migrate the app from scratch I get an error: An error has occurred, all later migrations canceled: undefined method `postcode_changed?' for #<Use...

Local installation of ruby / rubygems with no root access

Hi, I have a machine at work from wich I'd like to run a script that gathers some information about other machines. I want to do it in Ruby, since it's what I know best, but I've ran into some problems, all apparently due to the same reason: I don't have root access in this machine. So what I did was: Download ruby source, configure (w...

Cucumber tests and captcha: how to handle that?

We are considering using Cucumber for testing web applications (not in rails, most of them are asp.net actually). The applications are in production, our main goal is to test if everything is fine with the services, from time to time, infra people would run it. We have two questions: 1) Is this a good use for cucumber? Do community pe...

Ruby 1.9: Regular Expressions with unknown input encoding

Is there an accepted way to deal with regular expressions in Ruby 1.9 for which the encoding of the input is unknown? Let's say my input happens to be UTF-16 encoded: x = "foo<p>bar</p>baz" y = x.encode('UTF-16LE') re = /<p>(.*)<\/p>/ x.match(re) => #<MatchData "<p>bar</p>" 1:"bar"> y.match(re) Encoding::CompatibilityError: incompa...

Parsing a structured text file in Ruby

How can i easily parse a document which has this structure description some line of text another line of text more lines of text quality 3 47 88 4 4 4 4 text: type 1 stats some funny stats description some line of text2 another line of text2 more lines of text2 quality 1 2 4 6 7 text: type 1 stats some funny stats . . . Ideall...

can i include blank field in select_tag?

is it possible to add an option like :include_blank => 'Please Select' in a method select _ tag like it is posible with the select method? it seems not to work. is there any substitute for this for select _ tag method? thank you for your answers. ...

Simple 'require' problem in Ruby

Hello, I'm following along with 'Why's Poignant Guide to Ruby' in order to learn Ruby and I am having some problem when he first introduces the 'require' method(?) in his tutorial. Essentially I make a file called 'wordlist.rb' which contains: code_words = { 'starmonkeys' => 'Phil and Pete, those prickly chancellors of the New Rei...

Rails find and sort, using data from a related table

I have associated two models Businesses and Ratings. A business can be rated many times, and each rating stores an integer from 0 to 5. I would like to create a "Hall of Fame" page, where I list the top 10 businesses, by sorting businesses based on average ratings (which would use the Ratings model) and limiting the results to 10. I'm ...

Uninitialized constant error using Ruby in RubyMine

Has anyone used RubyMine who could help me out? I am new to RubyMine, and when I create my first project and add a few classes and wire them together for a simple meaningless application I am getting this error: "uninitialized constant RubyApp (NameError)" But when I take all the classes and put them in one file then run it, it runs fi...