ruby

How do I convince Rubygems (1.3.3) that I really do want it to install dependencies?

For any gem that has dependencies, I get the following (with the names changed as applicable): Attempt 1: sudo gem install mojombo-jekyll -s http://gems.github.com/ ERROR: Error installing mojombo-jekyll: mojombo-jekyll requires RedCloth (= 4.1.0, runtime) Attempt 2: sudo gem install mojombo-jekyll -s http://gems.github.com...

How does the putspecialobject opcode in the RubyVM work?

I'm working on an implementation of the RubyVM and I've searched all over for some documentation on the opcodes, but to no avail. If anyone knows specifically how the putspecialobject opcode works, or even just a link to some fullish documentation I'd much appreciate it! ...

What is the best way to have long string literals in Javascript?

Possible Duplicate: Multiline strings in Javascript In Ruby you can do something like this temp = <<-SQLCODE select * from users SQLCODE This way you have very long string literals in your code without have to escape lots of characters. Is there something similar in JavaScript? Currently I have javascript code like this, an...

In Ruby are there any related applications of the syntax: class << self ... end

class << self attr_accessor :n, :totalX, :totalY end The syntax above is used for defining class instance variables. But when I think about what syntax implies, it doesn't make any sense to me, so I'm wondering if this type of syntax is used for any other types of definitions. My point of confusion here is this: class << self The ap...

Rails - fragment cache not expiring

This one has me stumped. I have a view with a cached fragment: - cache :key=>"news" do %h2 News - etc I have a sweeper that uses: def expire_home_cache puts "expire_home_cache" expire_fragment(:key => "news") end The sweeper is called as I can see "expire_home_cache" in the console output. But the fragment is not...

Don't the Ruby methods instance_eval() and send() negate the benefits of private visibility?

w = Widget.new # Create a Widget w.send :utility_method # Invoke private method! w.instance_eval { utility_method } # Another way to invoke it w.instance_eval { @x } # Read instance variable of w Looking at the example above which relates to the Widget class (below), the send and instance_eval methods violate all of the protections pro...

Extending ruby object; extend_object callback prevents instance methods from being extended

Having some trouble extending an object instance with a module, specifically when I define an extend_object callback in the Module class. My understanding is that when you do something like: (s = String.new).extend SomeModule The SomeModule extend_object callback is called. This seems to be the case, but when I include a callback, no...

favorite ruby/rails ide for linux

Possible Duplicate: What IDE / Editor do you use for Ruby on Linux? what do you consider the best ide for developing ruby and rails on linux? thanks. ...

CommandLine::Application swallows my exceptions in main, how to avoid?

Example: require 'commandline' class App < CommandLine::Application def initialize end def main raise 'foo' end end results in $ ruby test.rb ERROR: foo And here the problem starts: during development there will always be Exceptions thrown deep somewhere in my code and I need to see the stacktrace and not so...

Ruby on Rails: How to run things in the background?

When a new resource is created and it needs to do some lengthy processing before the resource is ready, how do I send that processing away into the background where it won't hold up the current request or other traffic to my web-app? in my model: class User < ActiveRecord::Base after_save :background_check protected def background_...

Really Cheap Command-Line Option Parsing in Ruby

Sometimes I want to very cheaply hack some command line options into a simple script. A fun way to do it, without dealing with getopts or parsing or anything like that, is: ... $quiet = ARGV.delete('-d') $interactive = ARGV.delete('-i') ... # Deal with ARGV as usual here, maybe using ARGF or whatever. It's not quite the normal U...

Ruby Keyboard event handling

Hello im using curses to develop a small console application. I have a main loop section wich waits for user input, it uses the getstr function, of course this waits for the user to press enter. I would like to capture the up and down and tab keypresses. I suppose this can't be done with getstr. Anyone have any idea how to do this? ...

How should I sanitize user input before passing it to %x (executing it)?

I am taking an input string from a user and using that as the parameters for a command line back-end program. What is the best way to ensure that this input is "safe"? Aka they haven't inserted "; cd /; rm -rf" or some other ugliness into field? Without any sanitizing I have... @query = params[:query] @result = %x( mycommand #{@query}...

Ruby DSL (Domain Specific Language) repositories, examples

I am seeking excellent examples of Ruby DSLs (Domain Specific Languages). Which repositories, projects do you know of that are worth a read? Why is it (or: are they) great examples? I am particularly interested in more complex examples that are well thought-out and designed. ...

Is there a faster way to get a random record from a MySQL table through ActiveRecord?

In a Rails application, I'm using this to get a random row from a MySQL table: contact = Contact.find(:all, :limit => 1, :order => 'RAND()')[0] Even though the table only has about 20,000 rows it takes a couple seconds. Does anyone know of a faster way? Update Also tried just running the SQL in the console... SELECT * FROM `conta...

What's the "ruby way" to parse a string for a single key/value?

I am trying to parse a multi line string and get the rest of the line following a pattern. text: hello john your username is: jj thanks for signing up I want to extract jj, aka everything after "your username is: " One way: text = "hello john\nyour username is: jj\nthanks for signing up\n" match = text[/your username is: (.*)/] va...

If I have jRuby on Rails installed can I use only Ruby?

What I mean by this is if I install jRuby on Rails, can I use only the ruby language to build my application? I wanted to install my rails application with jRuby in case I wanted to use Java in the future, though I don't need it now. Or do I not understand? Is jRuby ruby that has access to java libraries? ...

Need alternative to filters/observers for Ruby on Rails project

Rails has a nice set of filters (before_validation, before_create, after_save, etc) as well as support for observers, but I'm faced with a situation in which relying on a filter or observer is far too computationally expensive. I need an alternative. The problem: I'm logging web server hits to a large number of pages. What I need is a...

How to understand the difference between class_eval() and instance_eval() ?

Foo = Class.new Foo.class_eval do def class_bar "class_bar" end end Foo.instance_eval do def instance_bar "instance_bar" end end Foo.class_bar #=> undefined method ‘class_bar’ for Foo:Class Foo.new.class_bar #=> "class_bar" Foo.instance_bar #=> "instance_bar" Foo.new.instance_bar #=> undefined method ‘inst...

View in Rails doesn't call overridden attribute accessor

I have a model like this: class Transaction < ActiveRecord::Base def amount self[:amount].abs end def transaction_type read_attribute(:amount) > 0 ? :credit : :debit end def transaction_type=(type) if type == :credit || type == 'credit' self[:amount] = amount.abs elsif type == :debit || type == 'debit' ...