ruby

Ruby Syntax: How to write "a = h[:v] if !h[:v].nil?"

I find myself often writing statements equivalent to: deleted_at = Time.at(data[:deleted_at]) if !data[:deleted_at].nil? i'd like to be able to write this in a more concise way. Any suggestions? Sometimes I write this as: deleted_at = Time.at(i) if !(i = data[:deleted_at]).nil? But I think this makes the code harder to read so I...

few vim autoinserts for ruby needed

I want to try the following things in vim insert mode: to have closing bracket/parenthesis inserted (after the cursor) every time I type the opening one to have #{} inserted whenever I type # inside "" (optionally, inside %() too) I know it is possible, but my competence in this part of vim does not even reach the self-starter level....

How to add a C++ compiler flag to extconf.rb

I'm writing a Ruby extension, for which I have a standard mkmf configuration script, but I need to add a special include flag (--std=c++0x) to all of the C++ compilation steps. I don't want it in the C compilation steps, 'cause it throws warnings. How should I do this? require 'mkmf' create_makefile('thing') For instance, I tried $CXX...

Resolving a class name conflict in a Rails application

I have been building a Rails application that performs accounting functionality. As part of this, I have a model with the class name Transaction. So far so good, I have been building this functionality for a month or so, and everything is working as expected. Until now... I have just discovered some older reporting functionality that w...

RVM --default bug

I set the default ruby as, rvm --default 1.9.2 when I do ruby -v, I get 'ruby 1.9.1' when I do which ruby I get .rvm/rubies/1.9.2PathOfRuby Any idea how to fix this? When I run a sample program, printing the version of ruby i.e, print RUBY_VERSION it prints 1.9.1 :( ...

Fill sparse array

I have a sparse array, for example: rare = [[0,1], [2,3], [4,5], [7,8]] I want to plot a chart with these data, each pair are point coordinates. As you can see I don't have points for x=1, x=3 , x=5, x=6 I want to fill the array with the previous values, so for the above example I will get: filled = [[0,1], [1,1], [2,3], [3,3], [4,5...

RoR has too much choice? what is meant by that?

I read somewhere (I can't recall where exactly) that RoR has too many choices, I don't know much about RoR so I wondering if someone could explain this for me (or maybe its not true?). Where exactly in the framework are the choices? Can someone summarize the more popular plugins for RoR for me? I gather these 'choices' are in: ORM t...

How to do attribute matching in (thinking) sphinx extended2 mode?

I have this expression which is working as expected in Thinking Sphinx: Customer.search :with => {:signer_id => 23} but I need to write an expression with OR of some attributes, like signer_id is 23 or admin_level is 42, so I moved to extended match mode and wrote this: Customer.search "@signer_id 23", :match_mode => :extended2 whi...

Problem creating Rails 3 Engine

I'm working on creating my first Rails 3 engine and I'm currently getting the following error message is a Railtie/Engine and cannot be installed as plugin (RuntimeError) To give you a bit more background I'm not doing the Engine as a gem, but I've place the Engine files in the vendor/plugins folder. I know there is an issue with load...

Rendering an Edit partial while selected a defaulted value

I have a partial in my rails app that loads the vote form (It's just a select with numbers ranging from 1-5). I'm now making another partial that loads up if the user has already voted it's suppose to be an EDIt partial where you get selected the value that you voted and if you wanted to you could change it. But for some reason is not wo...

Initializing attributes in Ruby objects

I create a new object of type Spam, with attr_accessor to both hooray and name and I have the initialize method. I'm expecting when I create a new object the object is loaded with a var of name with "Whammy" and a empty array named hooray. Though I'm not getting this behavior. Can anyone explain, thank you in advance. module Test class ...

Ruby Instance Variable Scope Question

I come from C++ and JAVA, which with Scope there is either global or local. I'm now learning ruby-on-rails and with ruby there is local, instance and global. I've never really heard of instance till now. With or without rails, what is the understanding and usage of the instance variable? Global = Variable across all objects share I...

Bundler can't find Ruby gem that appears on website

I installed Bundler on a pre-Rails 3 application and am trying to use it to install gems. My Gemfile contains the following lines: source :rubygems [...] gem "RubyInline", "3.8.1" However, when I run bundle install I get this error: Fetching source index for http://rubygems.org/ Could not find gem 'RubyInline', required by 'memcache-...

How to DRY up a ruby conditional structure needed for Rails

I'm finding I often have to use a structure to avoid a Rails error of undefined method 'name' for nil:NilClass. The structure looks like this: if country.state country.state.name end It seems like a classic case of repeating oneself with country.state appearing twice in one simple block. Is there any way to DRY this up? ...

How to use some ruby with objective-c

I don't want to go the full rubyobjc, how would I be able to use a few ruby-terms in my cocoa/objective-c code? The reason I want to switch is because I really like ruby's syntax. It's awesome! If it's not possible, then I'm wondering if rubyobjc is good... Unfortunately I've heard that when using rubyobjc there isn't a nib/interface ...

Ruby on Rails: How would I handle 10 concurrent users? Do I need more CPU?

Sorry if this might seem obvious. I've monitored that a web request on my Rails app uses 30-33% of CPU every time. For example, if I load a web page, then 30% of CPU is used. Does that mean that my box can only handle 3 concurrent web requests, and will stall if there are more than 3 web requests (i.e. I'll get a 100% CPU)? If so, does ...

Why does Sunspot change the `self` in the search DSL block?

I noticed (and verified in the sunspot code) the following behavior class Foo < ActiveRecord::Base def bar search_str = "foo" Boo.search do keywords(search_str) p self.id p self end end end In the code above, the DSL block can access the variables defined in the context. But the self inside block, poin...

When run on Phusion passenger, Bundler cannot find the Rails directory

So this is my problem. I used Capistrano to deploy my Rails 3 app to an Ubuntu server, which has Phusion Passenger 3 installed. Everything should have worked normal, but I kept getting this error message. Could not locate Gemfile in /var/www/rails/releases/20100916074325. (Bundler::GemfileNotFound) The directory in the error messa...

Ruby on Rails. Bundler. Cucumber. rake aborted! Command failed with status (1)

Hi. I have a problem using Bundler and Cucumber with Rails 3. When I run $ rake cucumber I get the following output: bundle exec /usr/local/bin/ruby -I "/usr/local/lib/ruby/gems/1.9.1/gems/cucumber- 0.8.5/lib:lib" "/usr/local/lib/ruby/gems/1.9.1/gems/cucumber-0.8.5/bin/cucumber" --profile default (in /home/<username>/practice/rails/...

Clearing ActiveRecord cache

I'm building a command line application using ActiveRecord 3.0 (without rails). How do I clear the query cache that ActiveRecord maintains? ...