ruby

authlogic openid + recaptcha + confirmed?

Hi all. Could you show me live code examples of integration authlogic-openid and recaptcha? I want to prevent users from registering using they own openid-servers to create multiple accounts and use it for spam. Also could you help me plz to understand - how to check confirmed? field on creation UserSession for openid registered users?...

(ruby) does one escapse using h() in view files only for security, or in controller files as well?

I receive input in the form of URL strings (aka controller/action?example=yes), and I'm wondering if I need to escape the content of the string for security. For example, if I assign the param to a variable: example = params[:example].to_s do I need to escape anything? or do I only apply h() when I put the value of :example back in t...

Are there good reasons for 'private' to work the way it does in Ruby?

It took me a while to understand how private methods work in Ruby, and it really strikes me as being very awkward. Does anyone know if there are good reasons for private methods to be handled the way they are? Is it just historic reasons? Or implementation reasons? Or are there good solid logical reasons (ie. semantic)? For example: c...

What's the most efficient way to partition a large hash into N smaller hashes in Ruby?

The Problem I'm working on a problem that involves sharding. As part of the problem I need to find the fastest way to partition a large Ruby hash (> 200,0000 entries) in two or more pieces. Are there any non O(n) approaches? Is there a non-Ruby i.e. C/C++ implementation? Please don't reply with examples using the trivial approach ...

Easy way to determine leap year in ruby?

Is there an easy way to determine if a year is a leap year? ...

How to make the class constructor private in Ruby?

class A private def initialize puts "wtf?" end end A.new #still works and calls initialize and class A private def self.new super.new end end doesn't work altogether So what's the correct way? I want to make new private and call it via a factory method. ...

What is the <appname>.rb file in /lib typically used for in a Ruby project?

In reference to this question: http://stackoverflow.com/questions/614309/ideal-ruby-project-structure I noticed that appname.rb is in lib, and is top level. I was reading through a little of the Rake source code on Github, and I noticed their project structure is pretty much the same. They have a top level 'rake.rb' file in /lib, but I'...

using Kernel#fork for backgrounding processes, pros? cons?

I'd like some thoughts on whether using fork{} to 'background' a process from a rails app is such a good idea or not... From what I gather fork{my_method; Process#setsid} does in fact do what it's supposed to do. 1) creates another processes with a different PID 2) doesn't interrupt the calling process (e.g. it continues w/o waiting f...

Access to current_user from within a model in Ruby on Rails

I need to implement fine-grained access control in a Ruby on Rails app. The permissions for individual users are saved in a database table and I thought that it would be best to let the respective resource (i.e. the instance of a model) decide whether a certain user is allowed to read from or write to it. Making this decision in the cont...

escaping the .each { } iteration early in Ruby

code: c = 0 items.each { |i| puts i.to_s # if c > 9 escape the each iteration early - and do not repeat c++ } I want to grab the first 10 items then leave the "each" loop. What do I replace the commented line with? is there a better approach? something more Ruby idiomatic? ...

Recursive chmod in ruby on mac OS X?

I need to delete an application (MyApp.app), which has read only permissions in all its enclosed folders. Before I delete it, I should change the permissions on all enclosed files/directories to 0644. How do I do this recursively? I've tried begin; FileUtils.chmod(0644, '#{config.appPath}'); rescue; end begin; FileUtils.rm_r('#{config....

Is MySQL able to return mutiple result set with one query?

I have the following (returning two separate result sets) being successfully executed from a proc but cannot do the same when executing this as a basic query. SELECT * FROM persons; SELECT * FROM addresses; Possible? What's the syntax? EDIT: I am using Ruby's DBI library: dbh.query("SELECT * FROM persons; SELECT * FROM addresses;"...

Get inf-ruby to use ruby version manager (rvm)

I have the ruby version manager installed and use the ruby implementation installed by RVM set to the default so that 'which ruby' shows '~/.rvm/ruby-1.8.6-p383/bin/ruby' yet when I open an inf-ruby buffer in emacs, it uses the ruby installed in /usr/bin. Is there a way to get emacs to respect the path for ruby the same way the she...

Thread Locking in Ruby (use of soap4r and QT)

[EDIT NOTE: Noticed I had put the mutex creation in the constructor. Moved it and noticed no change.] [EDIT NOTE 2: I changed the call to app.exec in a trial run to while TRUE do app.processEvents() puts '." end I noticed that once the Soap4r service started running no process events ever got called again] [EDIT NOTE 3: Cr...

Finding words from random input letters in python. What algorithm to use/code already there?

I am trying to code a word descrambler like this one here and was wondering what algorithms I should use to implement this. Also, if anyone can find existing code for this that would be great as well. Basically the functionality is going to be like a boggle solver but without being a matrix, just searching for all word possibilities from...

Writing Byte Arrays to serial with Ruby

I'm not clear on how to write simple byte code arrays with ruby, more-so I'm absolutely stumped on how to use the Ruby SerialPort library, well to be honest I have it working pretty well however I have only been successful in sending ASCII over the serial port. For example it's really simple to write ASCII: @sp = SerialPort.new "/dev/...

How do you say something happened "x minutes ago" or "x hours ago" or "x days ago" in Ruby?

If I have a time variable in Ruby, how could I say that it refers to an event that happened one of the following: "x minutes ago" or "x hours ago" or "x days ago" Obviously if something happened 2 days ago, I would not want to say it happened such-and-such minutes ago. ...

Can the array be reinvented in Ruby?

This is just a hypothetical question, if you wouldn't have the Array and the Hash class, would there be any way of implementing an Array class in pure Ruby? How? ...

Should I refactor with hobo?

Hello, I have a created a userdriven gallery with Ruby on Rails.The site is using a few plugins to create friendly links, permissions, pageless pagniation etc. The application controllers and views has gotton quite complex and I find it difficult and very time consuming to work with. So I thought about rebuilding the app with hobo, as i...

Rake (Ruby) catch error at end of build

I'm using Ruby and Rake to do our builds at the moment for .Net projects. I call a few commandline tools, such as NCover to check the coverage is high enough. When NCover returns and exit (fail) code though, Rake exits immediately stops. Is there a hook, like on_exit, that I can use. I basically want to output "Build FAILED" in nice re...