ruby

Immutable Active Record Relationships and Attributes (Setters)

I set out to solve a problem we have, where under certain circumstances; we may need to make an attribute or relationship immutable, that is once the attribute is written it is written forever. attr_readonly wasn't appropriate, as that didn't work for relationships, so I tried to prove the concept here: http://pastie.org/562329 This sh...

Natural language dates in ruby/rails?

I need to show natural dates like "few seconds ago" "21 minutes ago" Is there something built in to the rails? Or may be third party? This is not hard to implement, but I do not want to invent the wheel. ...

Source shell script into environment within a ruby script

If I'm writing a shell script and I want to "source" some external (c-)shell scripts to set up my environment, I can just make calls like this: source /file/I/want/to/source.csh I want to replace a shell script that does this with a ruby script. Can I do a similar thing in the ruby script? Update: Just tried it with test_script.csh: ...

Is it possible to use jruby to write an applet or is it better to use iron ruby for silverlight?

If I wanted to dabble with ruby, would it be easier to write an applet in Jruby or a Silverlight XAP with iron ruby? So far all I have is a vague idea that it might be possible. ...

problem using sql instead of named scope in rails

I have a method "search" in my model, which depending upon the various parameters passed runs an sql query in which i am joining seven tables. but when i am using this method with another named scope then error is shown "undefined method call for array". but when instead of this search method if i use group of named scope then it works ...

Nested resource with Atom Feed Helper

I'm trying to use the Rails Atom Feed Helper to generate a feed for a nested resource. My view template (index.atom.builder) is: atom_feed(:schema_date => @favourites.first.created_at) do |feed| feed.title("Favourites for #{@user.login}") feed.updated(@favourites.first.created_at) @favourites.each do |favourite| feed.entry(...

dynamic nesting of ruby blocks

Imagine i have some resource objects with a run method, which executes the block parameter under the lock held for that resource. For example, like this: r = Resource("/tmp/foo") r.run { ... } How can I write a ruby method which takes an array of resources and executes its block parameter under the lock held for all resources, like: ...

Column to row translation using sed

I'm writing a bash script to get data from procmail and produce an output CSV file. At the end, I need to translate from this: ---label1: 123456 ---label2: 654321 ---label3: 246810 ---label4: 135791 ---label5: 101010 to this: label1,label2,label3,label4,label5 123456,654321,246810,135791,101010 I could do this easily with a ruby s...

Is it possible to run code after each line in Ruby?

I understand that it is possible to decorate methods with before and after hooks in ruby, but is it possible to do it for each line of a given method? For example, I have an automation test and I want to verify that after each step there no error shown on the page. The error is shown as a red div and is not visible to Ruby as raise or a...

Security.framework interoperability

I have a situation where we have some server side data that is generated with a ruby script. That data is signed using the Ruby OpenSSL module like so: def sign_string(string) Base64.encode64(@private_key.sign(OpenSSL::Digest::SHA1.new, string)); end That data eventually ends up on an iPhone, where I would like to verify it using th...

Rails, validates_format_of - minimum 4 alphabetic characters

Hello, How do I validate a field - so that it contains at least 3 alphabetic characters. Valid: Something, Foobar 111. Invalid: ....... Best regards. Asbjørn Morell ...

RoR: What would be the best SEO structure, adding a blog feature?

Hello, I have migrated a old php/mysql site to ruby on rails, and had to keep the old link structure - not to break incoming links. The structure look like this: domain.com/artists/user1/seo-friendly-name-of-painting1 domain.com/artists/user1/seo-friendly-name-of-painting2 domain.com/artists/user1/seo-friendly-name-of-painting3 etc. ...

can you define a block inline with ruby?

Is it possible to define a block in an inline statement with ruby? Something like this: tasks.collect(&:title).to_block{|arr| "#{arr.slice(0, arr.length - 1).join(", ")} and #{arr.last}" } Instead of this: titles = tasks.collect(&:title) "#{titles.slice(0, titles.length - 1).join(", ")} and #{titles.last}" If you said tasks.collec...

Sending email with a Ruby script - 501 5.5.4 Invalid Address

Hi all, I'm trying to send email with a Ruby script, but my proof of concept isn't working. I can telnet to the mail server and send mail that way, but this script causes the mail server to raise an error: 501 5.5.4 Invalid Address #!/usr/bin/ruby require 'net/smtp' def send_email(to, subject = "", body = "") from = "[email protected]...

Implementing Re-connect Strategy using Ruby Net

Hi there I'm developing a small application which posts XML to some webservice. This is done using Net::HTTP::Post::Post. However, the service provider recommends using a re-connect. Something like: 1st request fails -> try again after 2 seconds 2nd request fails -> try again after 5 seconds 3rd request fails -> try again after 10 sec...

How to compute one's complement using Ruby's bitwise operators ?

What I want: assert_equal 6, ones_complement(9) # 1001 => 0110 assert_equal 0, ones_complement(15) # 1111 => 0000 assert_equal 2, ones_complement(1) # 01 => 10 the size of the input isn't fixed as in 4 bits or 8 bits. rather its a binary stream. What I see: v = "1001".to_i(2) => 9 There's a bit flipping opera...

Rails - Invalid Authenticity Token After Deploy

Hello, We're using EngineYard Cloud to deploy our Ruby on Rails application. We are running Rails v2.3.3. EngineYard Cloud deploys to AWS instances in a manner similar to Capistrano. After each deploy, we're running into Invalid Authenticity Token errors. Specifically, any user that has previously visited our application and then vi...

How do I generate RDOC for (all of) Rails?

I can do sudo gem rdoc activerecord --no-ri and sudo gem rdoc actionpack --no-ri both of which give me good docs. But sudo gem rdoc rails --no-ri gives me pretty much nothing, as the Rails gem itself is really just a holder for the others. How can I generate the equivalent of http://api.rubyonrails.org/? ...

Ruby win32 api interface

I need to access a few functions of the win32 library in ruby. I have found extremely sparse information on the Win32API class online, so I'm asking here. I know that you can do something like this: function = Win32API.new('user32','MessageBox',['L', 'P', 'P', 'L'],'I') But I can't seem to be able to call this function with the curre...

Native threads in Ruby 1.9.1, whats in it for me?

So, Ruby 1.9.1 is now declared stable. Rails is supposed to work with it and slowly gems are being ported to it. It has native threads.... and a global interpreter lock. Since a GIL is in place, do native threads offer any kind of benefit over green threads in 1.9.1? ...