ruby

Ruby: watching method (re)definition in runtime

I'm looking for a way (library or metaprogramming trick) to hook into method definition, so that I could extend certain classes and catch their method (re)definition "events". ...

What IDE do you recommend for Ruby on Rails on Windows with Cygwin?

I want to be able to use this IDE to step though code and debug. So far I have found: "e" Text Editor (http://www.e-texteditor.com/) $34.95 Arcadia (http://arcadia.rubyforge.org/) Has anybody ever tried either of these and how are they? Or anything else you can suggest? ...

How do I introspect things in Ruby?

For instance, in Python, I can do things like this if I want to get all attributes on an object: >>> import sys >>> dir(sys) ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', 'api_version', 'argv', 'builtin_module_name...

Ruby - How to remove a setter on an object

Given a class like this: class B class << self attr_accessor :var end end Suppose I can't modify the original source code of class B. How might I go about removing the setter on the class variable var? I've tried using something like B.send("unset_method", "var="), but that doesn't work (nor does remove_method, or ov...

Add save callback to a single ActiveRecord instance, is it possible?

Is it possible to add a callback to a single ActiveRecord instance? As a further constraint this is to go on a library so I don't have control over the class (except to monkey-patch it). This is more or less what I want to do: def do_something_creazy message = Message.new message.on_save_call :do_even_more_crazy_stuff end def do_e...

Using ASP.NET c# with Sketchup Ruby Extension

I'm an ASP.NET c# developer using VS2010 as my development platform. I am interested in accessing Sketchup models using a web form interface. Has anyone done this? I am looking for a simple "Hellow World" web application that uses the Ruby Extension model. ...

Rails way for querying join table in has_and_belongs_to_many

I have a user model and a role model with a has_and_belongs_to_many reliationship. The join table is roles_users (two columns - the PK of the user and the role) and has no corresponding model. I want to have a method that returns all users with a given role. In SQL that'd be something like SELECT u.id FROM role.r, roles_users ru WHERE ...

Force ruby to use dbi Gem instead of dbi in site_ruby

I'm using: Windows 7 Ruby 1.8.6 One-Click Installer DBI version 0.4.3 installed using RubyGems What I see when executing these commands: C:>ruby -v ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32] C:>gem -v 1.3.1 C:>ruby -r rubygems -r dbi -e "puts DBI::VERSION" 0.2.2 C:>gem list dbi *** LO...

Define a method that is a closure in Ruby

I'm re-defining a method in an object in ruby and I need the new method to be a closure. For example: def mess_it_up(o) x = "blah blah" def o.to_s puts x # Wrong! x doesn't exists here, a method is not a closure end end Now if I define a Proc, it is a closure: def mess_it_up(o) x = "blah blah" xp = Proc.new {|| p...

What does class_eval <<-"end_eval", __FILE__, __LINE__ mean in Ruby?

I'm learning how to use class_eval in modules (I'm somewhat familiar with class_eval) and came across this helpful class in resource_controller. In there they have things like this: class_eval <<-"end_eval", __FILE__, __LINE__ def #{block_accessor}(*args, &block) unless args.empty? && block.nil? args.push block if block_gi...

Trouble with array of dictionaries, ruby

I am getting the following error. game.rb:46:in `play': undefined method `[]' for nil:NilClass (NoMethodError) from game.rb:45:in `each' from game.rb:45:in `play' from game.rb:56 with this code, def play() currentTile = nil @tiles.each do |tile| if(tile['Name'] == 'Starting Square') ...

Rails app w/ 2.2.0 embedd how to use ruby 1.8 vice 1.9

Hello all, I have just downloaded an app that is based on Rail 2.2.0 which is included in the app. I have just upgraded to Ruby 1.9 and I still have Ruby 1.8 on my machine. Is there a way I can tell this app to use Ruby 1.8 vice 1.9? Would it be easier to decouple the app from Rails 2.2.0 and upgrade it to Rails 2.3.6? If changing the ...

How to set up my belongs_to and has_many reference

Hi, I have an ExpenseType object that I have created with the following migration: class CreateExpenseTypes < ActiveRecord::Migration def self.up create_table :expense_types do |t| t.column :name, :string, :null => false t.timestamps end end I can see the table name is the pluralised expense_types. My questio...

Reading a file N lines at a time in ruby

I have a large file (hundreds of megs) that consists of filenames, one per line. I need to loop through the list of filenames, and fork off a process for each filename. I want a maximum of 8 forked processes at a time and I don't want to read the whole filename list into RAM at once. I'm not even sure where to begin, can anyone help me...

Recommended twitter gem for ruby-on-rails

I've found a couple of different twitter gem (for ruby-on-rails) out there: http://twitter4r.rubyforge.org/ http://twitter.rubyforge.org/ But I'm wondering if someone can rate them and provide a recommendation of either one or a new one. Thanks ...

Problem with converting form_tag in rails

I am new to ruby and rails and I am having a problem from Beggining Ruby on Rails Ecommerce. (Yes, it's an old book). I have these 2 code sets for a view: new.html.erb: <%= form_tag :action=> 'create' do -%> <%= render :partial => 'form' %> <%= submit_tag 'Create' %> <%= end -%> <% link_to 'Back', :action => 'index' %> _form.html...

cant do port forwarding ro my webrick server to access it from the net!!

i am developing a facebook app and i am using for that facebooker plugin and webrick server. i have configured correctly my router to froward ports to my machine for 2 ports (80 and 3000) the apache server can be accessed from the net http://ip:80 amd the webrick server can http://ip:3000 , i dont understand why , please help me. ...

How do I do a grouping by year?

I have a books model with a date type column named publish_date. On my views I'm iterating through the books and I want to group the books by year such that I have a heading for every year and books that were published on that year to be listed below the year heading. So by starting with "2010" all books published on 2010 would be liste...

Modeling multiple polymorphic relationships using Hibernate

Ruby on Rails has polymorphic relations which are really useful for implementing functionality such as commenting, tagging and rating to name a few. We can have a comment, tag or rating class which has a many to one polymorphic relationship with a commentable, taggable and rateable object. Also, a given domain object can choose to imple...

Dynamically defined setter methods using define_method?

I use a lot of iterations to define convenience methods in my models, stuff like: PET_NAMES.each do |pn| define_method(pn) do ... ... end but I've never been able to dynamically define setter methods, ie: def pet_name=(name) ... end using define_method like so: define_method("pet_name=(name)") do ... end Any ideas? Thanks in adv...