ruby

Variables in ruby method names

I have the following code: for attribute in site.device_attributes device.attribute end where I would like the code to substitute the value of "attribute" for the method name. I have tried device."#{attribute}" and various permutations. Is this completely impossible? Am I missing something? I have considered overriding method_mis...

RoR: How do I ensure only one object is assigned to my object?

In my Ruby on Rails app, I have a User table and a Foo table. I also have a User_Foo table that stores the cross reference data. I have everything wired up for my views to work as I want, however, now I need to make sure that the same Foo doesn't get assigned to my User more than once. What's the best way to do this? I assumed that I ...

Using ruby to convert unsigned integers stored as signed back to the original value

A C-program is placing what it considers to be 64-bit unsigned integers into a column in a Postgres database that is typed as int8. To Postgres, int8 is always 'signed int8' (no such thing to it as 'unsigned int8'). So the Ruby program I have shows numbers retrieved from Postgres in the upper half of that space as negative. What is the ...

how to place YAML inside a YAML document

I am working with Rails fixtures for testing my rails application. It is all good except one of my database columns is supposed to hold YAML content. But, I am sure how to put the YAML markup I want to load into my database inside the YAML file. Here is an example: mvnforum: name: mvnforum abstraction_type: SVN url: src: ...

Which language is easiest and fastest to work with XML content?

We have developers with knowledge of these languages - Ruby , Python, .Net or Java. We are developing an application which will mainly handle XML documents. Most of the work is to convert predefined XML files into database tables, providing mapping between XML documents through database, creating reports from database etc. Which language...

How do you return a variable in a C -> ruby interface?

A follow up to an earlier question, showing the part that fails when I try to get the error message from my target library: require 'gt4r' @@test_environment = "INCLUDE=C:\\graphtalk\\env\\aiadev\\config\\aiadev.ini" @@normal_user = "BMCHARGUE" describe Gt4r do it 'initializes' do rv = Gt4r.gTD_initialize @@normal_user, @@norm...

Rails: How do I check if a column has a value?

How can I accomplish this? <% for agent in @broker.agents %> ... <% if agent.cell %><span class="cell-number">Cell: <%= agent.cell %></span><% end %> ... <% end %> I want to test to see if the agent has a cell number, and if so, display what's inside the conditional. What I have currently doesn't seem to work; it just displays ...

Can I define_method in rails models?

My rails model has code that is attempting to define_method(method_name) inside the model. I keep getting: NoMethodError: undefined method `define_method' What am I doing wrong? Am I doing this in the wrong place. I need this method attached to this model. Where else can I define this method? EDIT: For those asking to see the code: ...

Ruby 'Capitalize!' paradox

In Ruby we have the 'bang' method capitalize! which has the strange behavior of returning a nil if no changes to the string were made. That means I can't chain this commands with other since it effectively destroys the chain if it returns nil. What im trying to do is something like this: fname = fullname[0...fullname.index(' ')].capi...

what's the best way to parse a body of text against multiple (15+) regexes on each line?

I have a body of text that I have to scan and each line contains at least 2 and sometimes four parts of information. The problem is that each line can be 1 out of 15-20 different actions. in ruby the current code looks somewhat like this: text.split("\n").each do |line| #around 20 times.. .............. expressions['actions']...

Load Paths and Ruby C-Extensions

How do you allow a C-extension to use rb_f_require to require a file from outside the ext directory (e.g. requiring lib/foo/foo.rb from ext/foo.so). ...

Rails best practice question: Where should one put shared code and how will it be loaded?

The rails books and web pages I've been following have all stuck to very simple projects for the sake of providing complete examples. I'm moving away from the small project app and into a realm of non-browser clients and need to decide where to put code that is shared by all involved parties. The non-browser client is a script that run...

How do you spawn a child process in Ruby?

I want to offload a block of code in my main process to child process to make it run concurrently. I also want to have the PID of the spawned child process so I can monitor and kill it if necessary. ...

Rails Model has_many with multiple foreign_keys

Relatively new to rails and trying to model a very simple family "tree" with a single Person model that has a name, gender, father_id and mother_id (2 parents). Below is basically what I want to do, but obviously I can't repeat the :children in a has_many (the first gets overwritten). class Person < ActiveRecord::Base belongs_to :fath...

foo_url(mock_foo) sometimes doesn't work in rspec tests

I am trying to write an rspec test for a controller that accesses a model Group. @request.env['HTTP_REFERER'] = group_url(@mock_group) ### Line 49 I get this: NoMethodError in 'ActsController responding to create should redirect to :back' You have a nil object when you didn't expect it! The error occurred while evaluating nil.rewr...

How do I enable colour when running RSpec through RStakeout?

When using topfunky's RStakeout, the color in the result of the spec command is lost. This happens even when adding the --color flag. ...

What's the opposite of chr() in Ruby?

In many languages there's a pair of functions, chr() and ord(), which convert between numbers and character values. In some languages, ord() is called asc(). Ruby has String#chr, which works great: >> 65.chr A Fair enough. But how do you go the other way? "A".each_byte do |byte| puts byte end prints: 65 and that's pretty clo...

Stringify array for eval

I'm preparing a string that will be eval'ed. The string will contain a clause built from an existing Array. I have the following: def stringify(arg) return "[ '" + arg.join("', '") + "' ]" if arg.class == Array "'#{arg}'" end a = [ 'a', 'b', 'c' ] eval_str = 'p ' + stringify(a) eval(eval_str) which prints the string ["a", "b"...

Recursive directory listing using Ruby with Chinese characters in file names

I would like to generate a list of files within a directory. Some of the filenames contain Chinese characters. eg: [试验].Test.txt I am using the following code: require 'find' dirs = ["TestDir"] for dir in dirs Find.find(dir) do |path| if FileTest.directory?(path) else p path end end end Running the script...

Ruby statement chaining

Hey all, Given following Ruby statements: (Read input and store each word in array removing spaces between words etc) input = gets.chomp inArr = [] input.strip.each (" ") { |w| inArr.push w } inArr.delete_if {|ele| ele == " "} inArr.each {|w| w.strip!} I was wondering if anyone can suggest a way to optimize this code, maybe via chai...