ruby

What does class ClassName < ::OtherClassName do in Ruby?

Yesterday, I found the following code in RSpec(http://github.com/dchelimsky/rspec/blob/master/lib/spec/runner/option_parser.rb line2) class OptionParser < ::OptionParser What does this do? What is the difference between this and "class OptionParser < NameSpace::OptionParser"? ...

ruby on rails console processes in the background

Is there a way to run processes from rails script/console in the background? I have a onetime lib script that will take two or so days to execute and I want to set this to run in the background. Something like: script/console Then: >> load 'script.rb' & In the commandline I'd just do: $ command & I did find: http://backgroundrb...

rspec mock_model model never restored

I'm using rspec 1.3, and using rspec's mock_model to mock out some of the models in my controller tests. I create all the mocks in the before(:each) in the controller spec. I then run my model tests where I build real activerecord objects for setup in before(:each) instead of mocks. It seems that in my model tests, the model is still ...

Ruby Apache CGI Premature end of script headers

I'm trying to make a simple redirect work. I've gotten everything else to work just fine. For example, this works: #!/usr/bin/env ruby puts "Content-type: text/html" puts puts "<h1>blah</h1>" But this doesn't work (this is where I get the "Premature end of script headers" error): #!/usr/bin/env ruby puts "Status: 302 Found" puts ...

Difficulty aliasing `is_x?` to `has_role? x`

Each user has many roles; to find out whether a user has the "admin" role, we can use the has_role? method: some_user.has_role?('admin') Which is defined like this: def has_role?(role_in_question) roles.map(&:name).include?(role_in_question.to_s) end I'd like to be able to write some_user.has_role?('admin') as some_user.is_admin?,...

how do I add a second parameter to the tags in acts_as_taggable_on_steroids plugin

In my Rails application I am trying to use the acts_as_taggable_on_steroids plugin but it is falling short a little bit. I would like to add a second parameter to each tag that describes what it is tagging. Not the class, I understand that is recorded in the taggings join table. For example, if I were to tag an interview, I would give it...

Ruby: Binding a listening socket to a specific interface

I want to create a TCP socket that listens only on a specific interface (say, eth0). How can I do this? I've tried browsing through the Socket API, but I may not be understanding things properly. Here is my listen method so far: def listen socket = TCPServer.open($port) while $looping do Thread.start(socket.accept) do |server...

Given a class, see if instance has method (Ruby)

I know in Ruby that I can use respond_to? to check if an object has a certain method. But, given the class, how can I check if the instance has a certain method? Ie, something like Foo.new.respond_to?(:bar) But I feel like there's gotta be a better way than instantiating a new instance. What are your suggestions? Thanks. ...

Elegant Summing/Grouping/Etc in Rails

I have a number of objects which are associated together, and I'd like to layout some dashboards to show them off. For the sake of argument: Publishing House - has many books Book - has one author and is from one, and goes through many states Publishing House Author - Wrote many books I'd like to get a dashboard that said: How man...

How does Rails yield to multiple block in the erb templates?

How does rails get away with the following in an .erb file? <%= yield :sidebar %> <%= yield :other_bar %> <%= yield :footer %> How are they able to yield multiple times in the same context to different symbols? Is this some kind of rails magic? I'm totally familiar with: def some_method(arg1, arg2, &block) yield(:block) end To m...

Limit instances of rake task

Is there a way to limit the number of instances of a rake task? I have a rake task for reading emails that runs every 5 mins as a cron job. Sometimes the rake tasks takes more than 5 mins to complete and another rake task is launched before it finishes. There are hacky workarounds to check ps -Af inside the rake file but I am looking...

has_many :through, forms and validation

I have two quite nasty problems with join model. I think I am just going wrong way, so maybe someone can clear up my approch a bit. First there are some models with realtions like this: class Account < ActiveRecord::Base has_many :tickets, :through => :assigment has_many :assigments ... end accepts_nested_attributes_for :assigmen...

How to split to the right of a number?

I'm trying to use Ruby to split to the right of a number. For example: H2SO4 How do you do this? I'd like to output ["H2", "SO4"] x.split(/\d+/) yields: ["H", "SO"] x.split(//) yields: ["H", "2", "S", "O", "4"] Both cool but not exactly what I'm looking for. ...

How can I get cscope to properly handle comments for ruby files?

If I have a ruby file "test.rb" that contains the following: class Test ######################### #some pointless comment # ######################### def asdfasdf end end and I run cscope on it with (I have a mapping for this within vim): find . -iname '*.rb' -o -iname '*.erb' -o -iname '*.rhtml' | cscope -q -i - -b cs...

ActiveRecord: user-specific validations

I want to create user-specific validations. User has a column called "rule_values" which is a serialized hash of certain quantities. In a separate model, Foo, I have a validation: class Foo < ActiveRecord::Base belongs_to :user n = self.user.rule_values[:max_awesome_rating] #this line is giving me trouble! validates_presence_o...

Bundler creates a ? folder in the project's root directory

Hello, whenever I run the bundle install command, bundler creates a directory in my projects root with the name '?'. How can I prevent this from happening? I'm assuming the ? directory is due to the fact that some path has not been found, but which? And how could I tackle this issue? Best regards, DBA ...

issue with a seemingly simple nokogiri xml parsing problem

Hi all, I have an xml file as follows: <products> <foundation label="New Construction"> <series label="Portrait Series" startImg="img/blank.png"> <item_container nr="1" label="Firebed"> <item next="11" id="" label="Logs Black Brick">img/PortraitSeries/logs-black-brick.png</item> ...

Learning a new web-oriented programming language, where to begin?

A bit of background: I am familiar with PHP and Java, did some C, C++ and Lisp (gasp!) back in programming classes but never used them too much. So, I've been wanting to step out of PHP a bit for web development. I have a few reasons for this (in no particular order): there's a lot of amateurish code(rs) in PHP, which somehow makes me ...

Ruby on Rails - Which method should I override for Album.first.photos?

I've overrode the method find for ActiveRecord::Base and that went pretty well, now I can customize it nicely. def self.find(*args) # my custom actions super(*args) end then Album.find(1) #=> My custom result But now I would like to override this: Album.first.photos and I'm not sure what method exactly I should override to ...

ruby on rails 2.0+ scaffolding

My question is how do you find the "type" for a database column whats the difference between text / string. Is there anyway I can find this out? script/generate scaffold ModelName field1:type field2:type field3:type or script/generate scaffold Post title:string body:text category_id:integer ...