ruby

How to get a Ruby substring of a Unicode string?

I have a field in my Rails model that has max length 255. I'm importing data into it, and some times the imported data has a length > 255. I'm willing to simply chop it off so that I end up with the largest possible valid string that fits. I originally tried to do field[0,255] in order to get this, but this will actually chop trailing ...

Create Proc with current value of variable

I've been having a problem with a pesky little function in a class in a library that I did not create (and thus, cannot edit). Here's a simple class with the annoying behavior isolated: class Foo # This is a class I cannot def setmyproc(&code) # safely edit. @prc = Proc.new # Due to it being in a end ...

Writing Cucumber features for accessing SVN

I'm developing a tool that accesses SVN by running the command line SVN to get info and export. I'd like to write some Cucumber features so I don't have to keep manually testing but I'd like to mock out the SVN accesses. Should I switch to using a Ruby SVN library and if so, which one? Or is there a fake svn.exe that will provide pr...

SizeOf function

I want to get the number of bytes occupied by a variable or type in Ruby. Is there an equivalent SizeOf() function in Ruby on Rails? ...

How do I send Hex data?

I am trying to communicate with a modbus slave via either modbusTCP or modbus serial. The manuf. (partlow) has an ASCII communications manual (http://www.partlow.com/uploadedFiles/Downloads/1160%20ASCII%20Comms%20Manual.pdf) which, looks like it differs from the standard communication methods (http://en.wikipedia.org/wiki/Modbus). A lot ...

DataMapper, how to create multiple records?

My example datamapper class class Simple include DataMapper::Resource property :id, Serial property :uid, Integer end And I have an array id uid's I would like to add. items=[1,2,3,4,5,6,7,8,9,1,1,1,2,2,2,3] If I wanted to search for any of the id's in the array I would do something like Simple.all(:uid.in=>items) Is th...

Named_scope at least one in has_many association

I have a User model that has_many :posts. If I wanted to make a named_scope for finding users with at least one post would this be correct? named_scope :at_least_one_post, :joins => :posts, :group => "users.id" or should I take it a step further and do named_scope :at_least_one_post, :joins => :posts, :group => "users.id", :hav...

Chain of Responsibility and alias_method problems in Ruby

I'm trying to implement the chain of responsibility pattern in Ruby and ActiveRecord for a polymorphic object. I'm having a few problems. Sometimes I get an error that a method is not defined when I try to alias_method it, I think this is because the class isn't loaded or something so I explicity do a send to get the method I get a bu...

Rails 2 Namespace and Shallow Routes Issue

Hello. I've written the admin area of a particular rails app and now I'm ready to set it as own section within the website. Therefore, it will be /admin However, I didn't want to have it as /admin within the route itself I wanted to have something less common, so I added a couple of hyphens before and after it. So the route is /-admi...

Using curb gem and libcurl on Windows

I am trying to install the curb gem, which is libcurl bindings for Ruby, and of course I need to have "A working (lib)curl installation, with development stuff" installed on my computer. So, I went to the cURL Download Wizard and downloaded this package. But adding the bin into my PATH does not produce improvement and I still get an er...

how to append data to json in ruby/rails?

Say i have this short code: item = Item.find(params[:id]) render :json => item.to_json but i needed to insert/push extra information to the returned json object, how do i do that? Lets say i need to insert this extra info: message : "it works" Thanks. ...

rails - redirect when user manually inputs wrong url

How can I redirect a misspelled url to some default url in case a user mistypes a url? ...

deep nesting or different collection mongodb ?

hi all I am trying to work out the best way to store my daat using mongodb and mongomapper. I have category and each category can be described by many attributes so length in mm, weight in kg etc. I want each user to be able to create their own attributes to descibe a category of products. So forexample: user A wants to store his c...

Prawn gem: How to create the .pdf from an *existing* file (.xls)

Hi there, Can anybody show me (maybe copy/paste a simple code example) how to create the .pdf file from an existing (.xls) file, using the Prawn gem? (Basically, I'd need the command that "opens" the existing file.) (I'm asking because the Prawn documentation (http://prawn.majesticseacreature.com/docs/) seems to be gone since quite a w...

Rails: Using named_scope which triggers a MySQL "in"

PROBLEM: I want to run a query which would trigger something like select * from users where code in (1,2,4); using a named_scope. WHAT I TRIED: This is for a single code: named_scope :of_code, lambda {|code| {:conditions => ["code = ?", code]}} I tried something like named_scope :of_codes, lambda {|codes| {:conditions => ...

count quotes in a string that do not have a backslash before them.

Hey I'm trying to use a regex to count the number of quotes in a string that are not preceded by a backslash.. for example the following string: "\"Some text "\"Some \"text The code I have was previously using String#count('"') obviously this is not good enough When I count the quotes on both these examples I need the result only to...

Is auto-initialization of multi-dimensional hash array possible in Ruby, as it is in PHP?

I am so used to work in PHP with multi-dimensional arrays, where I can assign and initialize a hash by unset($a); // just to show that there is no variable $a $a['settings']['system']['memory'] = '1 Gb'; $a['settings']['system']['disk space'] = '100 Gb'; Is there a way to do similar thing in Ruby? Or I need to initialize all dimension...

How to perform Triple DES calculations in Ruby in hexadecimal?

Hi Guys, I'm trying to do some triple DES encryption in Ruby. I'm trying to replicate the results from this page: http://da.nmilne.com/des.html I'm trying to replicate those result in Ruby. I suspect the problem is the key is supposed to be a string, but I need to pass in a Hexadecimal key. Either that or the string being encrypted...

multilingual mongodb mongomapper

hi all I have a category collection and each category has a array of hashes containing attributes names and units to create a inout form with, to add a product of that category. for example category car fields - {name : length, unit : mm}, {name : weight, unit : kg}. Problem is i would this site to be multi-lingual and therefore need ...

Which testing tools do what in Ruby?

What I mean by that is, which tools are for unit testing, which for BDD, is there even a clear divide? I'm just learning Ruby from C#, and the tools I'm familiar with there are xUnit-style for pure unit testing/TDD, and Cucumber/SpecFlow for BDD/integration testing. Of course, you can use unit testing tools for integration testing if y...