ruby

Why do dynamic language newbies seem to default to Ruby instead of Python?

NOTE: This is NOT flame bait. I'm generally curious, so please, don't start any wars! Whenever I read a Ruby blog it goes something like: I used to be a devout PHP or Perl developer until one day I decided to start using Ruby. Now I love it. Considering that without Rails, Ruby would be just another obscure dynamic language,...

Using a string as a variable at run time in ruby on rails

Hi I have a string,which is been created at the run time I want to use this string as a variable to store some data into it. So how can i convert the string into variable name. Can ny one help..??? ...

How to watch a ruby class for method calls? (e.g. dependency injection, hooks, callbacks)

I have a Populater class for populating a database with random data. I would like to measure the populator's run method with the rtui progress bar. But instead of calling the progress bar object inside the Populater class, I would like to have a third class to increment the progress bar when certain methods in the Populater class are cal...

How do you prefer to define class methods in Ruby?

John Nunemaker recently blogged about the various ways to define class methods in Ruby, giving these three alternatives: # Way 1 class Foo def self.bar puts 'class method' end end # Way 2 class Foo class << self def bar puts 'class method' end end end # Way 3 class Foo; end def Foo.bar puts 'class method' e...

Generating Missing Spec Files for RSpec

Is there any command available for generating all missing spec files for existing models / controllers? I have a project that has several models that have been generated with out spec files. ...

Sorting an array of objects in Ruby by object attribute

Hi, I have an array of objects in ruby on rails. I want to sort this array by an attribute of the object. Is it possible? ...

Impementation of the Ruby <=> Combinator

Not infrequently, one wants to implement the <=> (comparison, or "spaceship") operator on a product data type, i.e., a class with multiple fields (all of which (we hope!) already have <=> implemented), comparing the fields in a certain order. def <=>(o) f1 < o.f1 && (return -1) f1 > o.f1 && (return 1) f2 < o.f2 && (return -...

Embedding Rake in a C++ app? Or is there a Lake for LUA?

I've found a couple of questions about embedding Ruby in a C++ app. Almost all of the top-voted answers suggest using Lua instead. Given that a project I have in mind would be better served by the grammar already expressed in Rake (it's a rules engine), is there any easy way to embed Rake in a C++ app, or is there a Rake-like module for...

Is it possible to access the content of a xml object using dot syntax?

Hello I am using LibXML to parse an rss feed and I am wondering if is possible to access the content within using dot syntax (or just as easy). So if I have: <post> <created_at>Sat Aug 09 05:38:12 +0000 2008</created_at> <id>882281424</id> <text>I so just thought the guy lighting the Olympic torch was falling when he began to r...

Can Ruby, PHP, or Perl create a pre-compiled file for the code like Python?

For Python, it can create a pre-compiled version file.pyc so that the program can be run without interpreted again. Can Ruby, PHP, and Perl do that same on the command line? ...

How to cap and round number in ruby

Hi, I would like to "cap" a number in ruby (on rails). For instance I have has a result of a function a float but I need an int. I have very specific instructions, here are examples: if I get 1.5 I want 2 if but if I get 2.0 I want 2 (and not 3) so doing number.round(0) + 1 it won't work. I could write this function but, I sure it a...

rails script/generate skip unnecessary files by default

script/generate became very annoying since i started using rspec etc. i dont need unit test files and fixtures anymore, but script/generate makes them anyway. is it possible to set --skip-fixtures and --skip-test to be default system-wide (or at least project-wide)? ...

xslt 2.0 and Ruby on OS X

I am attempting to parse an XML document against an XSLT 2.0 sheet. However, I am being told that the libraries on OSX 10.5.x only support XSLT 1.0 operations. When I look at xsltproc, I get this: hmasing$ xsltproc --version Using libxml 20616, libxslt 10112 and libexslt 810 xsltproc was compiled against libxml 20616, libxslt 10112 an...

ActiveMerchant/Spree - Is there a per-transaction/per-account commission to the creators?

This is probably a silly question but if you use ActiveMerchant (or spree), do the creators of ActiveMerchant (or spree) take a commission? Since it's open source I wouldn't expect them to. But at the same time, because it's such a large undertaking to create a processing gateway, it would not surprise me if they wanted some compensation...

"Evaluating Postfix Expressions" Program in Ruby

I tried to make a small script to evaluate post-fix expressions in Ruby. def evaluate_post(expression) my_stack = Stack.new expression.each_char do |ch| begin # Get individual characters and try to convert it to integer y = Integer(ch) # If its an integer push it to the stack m...

A concise explanation of nil v. empty v. blank in Ruby on Rails

I find myself repeatedly looking for a clear definition of the differences of nil?, blank?, and empty? in Ruby on Rails. Here's the closest I've come: blank? objects are false, empty, or a whitespace string. For example, "", " ", nil, [], and {} are blank. nil? objects are instances of NilClass. empty? objects are class-specific, and...

Using multiple controllers in one view in Rails

I'm working on something like a social networking mesh; I am using different API's from various websites, e.g. Last.FM, Delicious, Twitter, ... I've created one controller per each website (currently, there are 7). Sample views: localhost:3000/lastfm <- All datas i gathered from user's Last.fm account localhost:3000/twitter <- All da...

Having trouble populating a select box in rails for editing?

I have a form that has been split out into a partial so I can use it both in the new view and the edit view. I have several dropdowns(selects) that are populated from a static array generated in a model. In location model: def open_close_times @times = '','12:00 AM', '12:30 AM', '1:00 AM', '1:30 AM', '2:00 AM', '2:30 AM', '3:00 AM',...

How does MySQL's RENAME TABLE statment work/perform?

MySQL has a RENAME TABLE statemnt that will allow you to change the name of a table. The manual mentions The rename operation is done atomically, which means that no other session can access any of the tables while the rename is running The manual does not (to my knowedge) state how this renaming is accomplished. Is an enti...

Ruby blocks parameter names

Hello, Does anyone know if there is a way to access the names of the parameters passed in ruby blocks? E.g. def do_something() # method uses the names of the parameters passed to the block # in addition to their values # e.g. the strings "i" and "j" end do_something { |i, j| ... } It's a requirement for a dsl I'm writing, and...