ruby

Dynamically requiring in ruby

All, I was wondering if anyone has an intermit enough knowledge of rubys 'require' to tell me if the following is valid ruby or not; class Something def initialize(mode) case mode when :one then require 'some_gem' when :two then require 'other_gem' end end end s = Something.new If so, would the require plac...

Passthrough Methods

Does anyone know of a way of providing pass-through methods to provide something like the following but with added DRY? class Cover @some_class = SomeClass.new def some_method @some_class.some_method end end ideally I would like to dry it up to something like this: class Cover @some_class = SomeClass.new passthrough...

Is there any way to easily link a file in RDoc?

I want to link to a documentation of a file in RDoc. but the only way I could do is with the following markup: configuration.rb[link:files/configuration_rb.html] I would like to do it in a better way, something like this: <file>configuration.rb</file> Is there any existing markup rule to do this? EDIT: of course I've tried without...

How to create click events by ruby.

Hi. I wanna to create mouse click events by ruby. left-click, right-click is there some library to do this? thanks for your concerns. ...

Regex to strip symbols but not foreign characters in Ruby

Does anyone have a good regex for stripping all symbols (';.,_\$@!% the carriage return etc) from a string, without damaging any foreign characters (é 多 فا etc)? Non-regex would be even better, I suppose, but I don't see any Ruby or Rails methods that do this. ...

How do I write and test password changes when using Authlogic?

An application I inherited has the following action for updating a user's profile: class UsersController < ApplicationController # ... def update @user = current_user if @user.update_attributes(params[:user]) flash[:notice] = "Successfully updated profile." redirect_to root_url else flash[:error] = "Hrm...

How to dynamically create arguments for a method?

Being still somewhat new to Ruby, I'm not sure how to do this... Let's say I have a method that takes a variable number of arguments: def mytest(*args) puts args.to_json end Obviously I can call it with whatever I like, such as: mytest('one', 'two', 'three') No problem. But what I need to do is call it with a dynamically-created se...

Ruby - clean up url

Is there a quick way to clean up a url that is malformed with a 2nd question mark instead of ampersand? i.e. http://google.com?x=1?y=2 thx ...

Ruby lambda arguments

This code works as expected (does nothing, even doesn't produce warning/errors): l = lambda {|i|} l.call(1) This code produces warning (warning: multiple values for a block parameter (0 for 1)): l = lambda {|i|} l.call And this code fails with error (ArgumentError: wrong number of arguments (0 for 2)): l = lambda {|i, y|} l.call ...

Ruby MatchData class is repeating captures, instead of including additional captures as it "should"

Ruby 1.9.1, OSX 10.5.8 I'm trying to write a simple app that parses through of bunch of java based html template files to replace a period (.) with an underscore if it's contained within a specific tag. I use ruby all the time for these types of utility apps, and thought it would be no problem to whip up something using ruby's regex sup...

Sort a collection of objects by number (highest first) then by letter (alphabetical)

I'm building a widget to show medal counts for the Olympics. I have a collection of "country" objects, where each has a "name" attribute, and "gold", "silver", "bronze" for medal counts. List should be sorted: 1. First by total medal count 2. If same medals, sub-sort by type (gold > silver > bronze, ie. two golds > 1 gold + 1 silver) 3....

How do you extend class level methods to a separate module in Rails?

Given a situation such as: module Extension def self.included(recipient) recipient.extend(ModelClassMethods) end module ModelClassMethods def self.msg puts 'Hi from module' end end end class B include Extension end Why is B.msg not available? >> B.msg NoMethodError: undefined method `msg' for B:Class ...

Is there any difference between these two 'belongs_to' statements

belongs_to :keeper, :class_name => "Staff" belongs_to "staff", :foreign_key => "keeper_id" In my basic tests, these seem to be doing the exact same thing. Are they indeed the same? Is one better than the other? ...

Liquid Templates Not Parsing!

Im trying to use Liquid Template Language in my Rails Application, i've watched Ryan Bates' video over at rails cast, i pretty much follow the instructions but it just doesnt seem to work! When I try something like @template = Liquid::Template.parse("Hi {{name}}") @template.render('name' => 'toby') I get hi toby but when i try so...

Perform sum with nested controllers

I have two models of concern, "Order" and "Kit"; each order has_one :kit Each "Kit" has a 'cost' value. Within a controller I want to be able to sum together the costs for each 'order'. Logically I thought this would make sense (but it doesn't work): @revenue = Order.Kit.sum(:cost) Any help would be appreciated. Thanks. Example:...

editor with plugins in ruby?

Hi. I'm looking for extensible linux editor(GUI) that may be extended with plugins written in ruby. Editor shouldn't be written in java or ruby. Any ideas? ...

What does string * '' mean in Ruby?

I was looking through some Rails source code and came across # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/builder/css.rb, line 129 129: def target! 130: @target * '' 131: end What does the * '' do? Is that multiplication by an empty string...? And why would you do that. ...

Workling worker cannot find newly added record

Hello, I'm using Starling and Workling to process background tasks in my application, a Swoopo-style auction site. In this case the background task is a notification system that monitors auctions and notifies the winner. The monitor is invoked upon creation of the auction object. My problem is that my monitoring code can't find the auct...

Open a SHIFT_JIS file in Ruby 1.8.7

This is one of those things that seems like it should be laughably easy but I'm stuck... I need to open a CSV file that is stored in SHIFT_JIS encoding and decode it to Unicode and also encode in UTF-8. It sounds like this is pretty straightforward in ruby 1.9 but I'm not feeling particularly adventurous on my production Rails app, so I...

How can I retrieve a document by _id ?

Hi, I'm trying to retrieve a document when I have an object id - however, the query does not work. @collection = @db.collection('Mylist') @result = @collection.find({"_id" => params[:id]}) I've tried variations of the query - it always yields empty - however when I try a query on the collection such as below, that would work. @resul...