ruby

getting firewatir to run on mac osx: jssh problems

I am trying to get firewatir to run on Mac OSX Leopard. I have Firefox 3.6rc2 installed but running the most simple script does not work: require 'rubygems' require 'firewatir' ff=FireWatir::Firefox.new ff.goto("http://mail.yahoo.com") i am getting the following error /usr/local/lib/ruby/gems/1.8/gems/firewatir-1.6.5/lib/firewatir/f...

Ruby - why can't I use mkdir with a Pathname object

Hi Ive recently been trying out the pathname lib in ruby and one thing I want to do is use mkdir to create some directories. Ive looked up the documentation and it says it wraps up mkdir but everytime I try to use it I`m getting this error irb(main):006:0> p = Pathname.new('/tmp') => #<Pathname:/tmp> irb(main):007:0> a = p.mkdir("123ada...

The ruby on rails can't manually delete records?

I have a database the have orders, and which order many order items, that kinds of thing. I deleted one product carelessly, and it is related to the order items, so it can't load successfully. So, I use the SQLite Database Browser to delete the orders and order items. But after I restart the server, it prompt me that : We're sorry, ...

Why does uniq! return nil if there are no duplicates

I'm just starting with Ruby and I personally find the following to be a violation of the "principle of least surprise". And that is, quoting from the documentation, that uniq! "removes duplicate elements from self. Returns nil if no changes are made (that is, no duplicates are found)." Can anybody explain this, which seems completely c...

Fork delayed job from the app server?

Here's my simple ideal case scenario for when I'd like delayed job to run: When the first application server (whether through mongrel or passenger) starts, it'll start my delayed job workers. When the last running application server terminates, it'll kill all the delayed job workers. The first part (starting) is doable, although I'm ...

Inheritance with MongoMapper: searching the parent class

does it make sense to save the class name in a field when using inheritance with mongomapper/rails? class Item include MongoMapper::Document timestamps! key :class, String # does this actually make sense? key :title, String end class Post < Item key :body1, String end class Page < Item key :body2, String end if a sear...

Ruby spreadshet gem, how can I center align a number

I'm using http://spreadsheet.rubyforge.org to generate a spreadsheet and I'm having trouble with something. I'm opening an existing workbook and adding data to it. I have manged to get number formatting working to some extent, at least excel is seeing this data as a number, but (very un-excel like) the client would like the number align...

Match only beginning of line in Ruby regexp

I have a string variable that has multiple newlines in it and I'd like to test if the beginning of the string matches a regular expression. However, when I use the ^ character, it matches against text that begins at each newline. I want this to match: "foo\nbar" =~ /^foo/ and I want this to not match "bar\nfoo" =~ /^foo/ I can't ...

str.each in Ruby isn't working

I'm Learning Ruby. I found the method String#each at http://ruby-doc.org/core/classes/String.html. When I try using it... irb(main):001:0> "hello\nworld".each {|s| p s} NoMethodError: undefined method `each' for "hello\nworld":String ...but I get that NoMethodError. I'm using Ruby 1.9.1p253 so I don't think I'm using an old versi...

accepts_nested_attributes_for and select tag

Hi, I've a Room model and a Person model. A room can have many person and a person can have one room. On the room creation screen I can 'link' n person to this new room So i want to have a variable amount of select tag which contains the list of all people I don't know how to build the select tag. Can anyone help thanks... I've th...

What's the easiest way to create a repeating sequence a la: ['a','b'][2]='a'

Hi there, I find myself wondering if there's a built-in Ruby method to get the nth number in a 12-element sequence, no matter how large 'n' is. For example, if I have a sequence (portrayed as an array below) that has 3 elements, and if I try to access the fourth element, it starts from the beginning. Here's a method that will do this, ...

Ruby: How to check if a string contains multiple items?

I'm familiar with Ruby's include? method for strings, but how can I check a string for multiple things? Specifically, I need to check if a string contains "Fwd:" or "FW:" (and should be case insensitive) Example string would be: "FWD: Your Amazon.com Order Has Shipped" ...

How to programmatically determine the installed version of IE from a script

We have an automated testing cluster based on selenium-grid. To manage the cluster, I have built a collection of Rake (Ruby) tasks which can start, restart, ping, and stop nodes. I'm testing our application across a number of browsers including IE6, IE7, and IE8. This means each node in the cluster has to be aware of which version of IE...

What work-arounds can be applied to thread-unsafe autoload in ruby?

As mentioned in this question, autoloading within a thread can cause problems. What work-arounds can be applied? ...

Ruby: Defining attributes by looping an array?

I'm trying to define attributes from an array like so: ["a", "b", "c"].each do |field| @recipe.field = "anything" end I want to end up with something like this: @store.a = "anything" @store.b = "anything" @store.c = "anything" Do you know what I should do with the @store.field above? I tried @store.send(field), but that is not wo...

Force Ruby Version

I just got burned because I used find_index on an array on my dev box (OSX with Ruby 1.8.7) and the deployment machine runs Ruby 1.8.6. (What's the difference between find_index and index? The latter works on 1.8.7 and 1.8.6) So that got me thinking: what's the best way to force Rails to run with a specific Ruby version? Since it's pr...

Ruby Style Question: storing hash constant with different possible values

This is more of a style question, I'm wondering what other people do. Let's say I have a field in my database called "status" for a blog post. And I want it to have several possible values, like "draft", "awaiting review", and "posted", just as an example. Obviously we don't want to "hard code" in these magic values each time, that w...

Converting binary IOstream into file

I am using rails server. i am sending core http request. in request.body contents a file which I want to be uploaded. This request.body is StringIo object. I want to upload this file to my server. ...

Word count in Rails?

Say I have a blog model with Title and Body. How I do show the number of words in Body and characters in Title? I want the output to be something like this Title: Lorem Body: Lorem Lorem Lorem This post has word count of 3. ...

"which in ruby": Checking if program exists in $PATH from ruby

Hiya, my scripts rely heavily on external programs and scripts. I need to be sure that a program I need to call exists. Manually, I'd check this using 'which' in the commandline. Is there an equivalent to File.exists? for things in $PATH? (yes I guess I could parse %x[which scriptINeedToRun] but that's not super elegant. Thanks! yann...