ruby

Ruby - convert from symbol to variable

How can I convert :obj back into a variable called obj inside the def? def foo(bar) bar.some_method_call end foo :obj UPDATE: The final code is more elaborate than this but... I like to be able to say foo :obj instead of foo obj I working on some DSL-like syntax. And this one change would let things read a little clearer. ...

Phusion-Passenger seems to create MANY (140+) orphaned processes.

Hi there, We're running 3 Apache Passenger servers sharing the same file system, each running 11 Rails applications. We've set PassengerPoolIdleTime = 0 to ensure none of the applications ever die out completely, and PassengerMaxPoolSize = 20 to ensure we have enough processes to run all the applications. The problem is that whe...

Ruby - test for array

What is the right way to: is_array("something") # => false (or 1) is_array(["something", "else"]) # => true (or > 1) or to get the count of items in it? ...

Where are MSG_ options defined for ruby sockets?

In the documentation for Ruby class Socket::recv, there is a mention of a second option parameter "flag" which is said to be zero or more of MSG_ options. I checked a few different sites and wasn't able to find what the MSG_ choices are. Can anyone point me to the documentation for these flags? ...

Ruby on Rails: "find_create_by_user"

Hello, I'm wondering why this is not working for me: Recipe.find_or_create_by_user_id(current_user.id, :name => "My first recipe") That creates the recipe fine if one does not exist by the user's id, but the name ("My first recipe") isn't included into the newly created entry. Is there something I'm doing wrong? I can't quite figure ...

Operators as method parameters in C#

I don't think it's possible to use operators as a parameters to methods in C# 3.0 but is there a way to emulate that or some syntactic sugar that makes it seem like that's what's going on? I ask because I recently implemented the thrush combinator in C# but while translating Raganwald's Ruby example (1..100).select(&:odd?).inject(&:+)....

How can I check whether a parameter isa Symbol?

The question is in the title. My parameter can be either a string or a symbol and depending upon which it is I want to perform different actions. Is there a way to check for this in Ruby? ...

Precedence of operation in ruby

I am very new to Ruby, so please accept my apologies if this question is wierd I tried puts 5-8.abs which returned -3, and then I tried puts (5-8).abs which returned 3. What is happening exactly when I try puts 5-8.abs, it seems like abs is ignored? ...

ruby hash question

I was looking at example code from the O'Reilly book on Ruby on Rails and ran across this: def label_for(method, options={}) extra = "" if options[:required] extra = " <span class='required_mark'>*</span>" end label(:label || method) + extra + "<br />" end I understand that options is a hash, but how is it abl...

Ruby Maths Function Memoization

Hi All, I wrote some code that looks like this: def get(x, y) @cachedResults.set(x,y, Math.hypot(x, y)) if @cachedResults.get(x,y).nil? @cachedResults.get(x,y) end Where @cachedResults contained a 2D Array class i wrote (in a few minutes) and the purpose of this function is to make sure that I never have to call Math.hypot twic...

When to stop DRYing up the code?

So DRYing up code is supposed to be good thing right? There was a situation in one of the projects I was working on where there were certain models/entities that were more-or-less the same except the context in which they were being used. That is, Every such entity had a title, descriptions, tags, a user_id etc and some other attributes....

Debugging a scripting language like ruby

I am basically from the world of C language programming, now delving into the world of scripting languages like Ruby and Python. I am wondering how to do debugging. At present the steps I follow is, I complete a large script, Comment everything but the portion I want to check Execute the script Though it works, I am not able to deb...

Ruby methods equivalent of "if a in list" in python?

In python I can use this to check if the element in list a: >>> a = range(10) >>> 5 in a True >>> 16 in a False How this can be done in Ruby? ...

OAauth gem in rails problem.

I'm trying out OAuth for twitter api authentication and ran into a brick wall when my code spits out the message: (instance of OAuth::Consumer needs to have method `marshal_load') My code: @consumer=OAuth::Consumer.new( "token","secret", { :site=>"http://mysite.com/" }) @[email protected]_request_token session[:reque...

Ruby XML to JSON Converter?

Is there a library to convert xml to json in ruby? ...

Convert latin1 string to utf8?

Hello, how can I convert a string, that contains latin1 characters to utf8? The string is a document, that is opened by open-uri and that contains these special characters. Best regards ...

Update attributes unless blank?

I have an existing Project record, and I'm importing a CSV file to update the associated Project attributes. However, often the CSV will contain blank fields and I don't want to overright exisiting attributes if the related CSV field is blank. Something like this: project.update_attributes(:name => row.field('project_name') unless row...

require library returns 'missing file' message

I have a small application which I created using jeweler. The jeweler generates a lib/ directory, where I suppose to write my code. This gem I'm creating depends on the httparty gem, so, in my Rakefile I put Jeweler::Tasks.new do |gem| gem.add_dependency('httparty', '>= 0.4.5') ... end in my implementation file I put r...

How to extend a class from an initializer and have it reload in development environment?

I am extending a class (which is in a plugin) by including a module, this is done in an initializer. require 'qwerty/core/user' User.send :include, Qwerty::Core::Extensions::User However in development before every request (and after reload! is called in the console) all models are reloaded but because the initializers are not run ag...

Copying data from a running ruby script

Hi, I have a long running ruby script, in Linux, which has some interesting data stored in a hash table. The program does not have any persistence mechanism and I am interested in copying the data from it's hash table. Is there any way to copy data from the memory of a running ruby script? raj ...