ruby

ruby sort_by twice

Ruby has a sort_by method on Enumerables. Fantastic! So you can do something like entries.sort_by { |l| l.project.name } That would sort a bunch of entries by their project names. How could you work it so that within projects that had the same name, entries were sorted by their time? ...

How to determine the origin of a LocalJumpError?

How can I determine easily and programmatically whether a LocalJumpError arose from the caller's immediate failure to supply a needed block to a method, or from deeper within that method and others it invokes? By "easily," I mean I'd like to avoid string inspection/regexen on $!.backtrace. A solution applicable to 1.8 and 1.9 is also p...

What cool frameworks are there on Java/Ruby etc. that don't exist on .NET?

I was just reading a post from Justin Etheredge in which he wrote "... I get so jealous when I talk to the Java guys at work and see all of the freaking awesome tools that they have for free on their platform. Since I use the NUnit, NCover, NHibernate, NAnt stack when doing most .NET development, sometimes I feel like we are just a bunch...

Safely loading a hash in Ruby

I want to load a data structure into a Ruby script which maps a string to a triple which contains some combination of regular expressions, scripts and atoms. The file that it loads from needs to be human writeable. Currently I'm writing the file to contain a Ruby hash, loading that as a string and calling eval. Ie. Data file { "key1" ...

Accessing member variables of other object of same class in Ruby

In Java I can do: public boolean equals(Object other) { return this.aPrivateVariable == ((MyClass)other).aPrivateVariable; } This allows me to define equality without breaking the encapsulation of my class. How can I do the same in Ruby? Thanks. ...

Need help with polymorphic join table in rails

Hi gys i am trying to link two entities to one entity which is governing body, estate and repo_document, then a governing body can have repo_document that the estate can also have so i decided to create a join table named document_owner.. but i dont know what to write in their models..i have got this code in my document_owner model.. b...

Convert an arbitrary string to xml in ruby

If I have a string which may contain any characters (including '/', '&',etc...) how do convert it safely into XML that can be stored like this: <myelement>mystring</myelement> Does it need to be CDATA, or can I easily convert it using a ruby function? ...

ERB Ruby Templates -- Online Editor

Hi, I am aware of quite a few different JavaScript based online text editors for WYSISYG html editing, however I am trying to find something similar for ERB Ruby templates. Essentially it would be just like the other editors, however it would not garble or encode the <%= foo.to_s %> type code blocks. Is there anything out there which w...

Is there a function like String#scan, but returning array of MatchDatas?

I need a function to return all matches of a regexp in a string and positions at which the matches are found (I want to highlight matches in the string). There is String#match that returns MatchData, but only for the first match. Is there a better way to do this than something like matches = [] begin match = str.match(regexp) brea...

How do I get the ruby YAML implementation to not read beyond the YAML EOF(...)

In the YAML specification it says ... is the EOF If I do: YAML.load_documents("--- abc\n--- 42\n...\nerror") { |d| puts d } I should get abc 42 But I get abc 42 error Unfortenely there is not much documentation about the YAML parses. Do I have to tell the parses to honor the EOF, or does the parser not comply to the specs? ...

Code review conducted by an engineer who codes in a different language. Is it constructive?

I'm a Technical Support Engineer who recently discovered how fun programming can be. My boss noticed this interest and suggested that I learn Ruby as a first language since the company can benefit from it, it has a very elegant syntax, and we have no need for more Java/C/C++ programmers. Well I've written my first fairly large Ruby scri...

In Ruby on Rails is there a way to view the properties of a model

I am using NetBeans to create my first Ruby on Rails application. Is there a way by which I can view all of the properties of a model? Should I just be looking in the database? ...

Can tell ruby me if a given class wass defined in a given module.

Module M Class C end end What I need is something like: M.was_defined_here?(M::C) M.classes.include?(M::C) Does this exists somehow? I know I could parse M::C.name. But someebody could have the idea to change Module#name, to make it more astetic or something. I want a clean solution. ...

Name of class from its object

How to extract a name of the class from its object? For example I have a @list object which I know is surely an instance of List class. But how do I extract that directly in code? ...

How to send keyboard and mouse commands to the underlying operating system using Ruby?

Is there an operating system neutral way for Ruby to send keyboard and mouse events to the underlying operating system? One obvious (to me) approach is to use the Ruby/Java bindings and use java.awt.Robot, but that just seems silly. ...

Getting elements in the order they appear in the document

I have a document and want to extract a couple of elements which ar direct descendents of the parent element but leave out others. The problem is that I don't get the elements in the order they appear in the document. The reason might actually be that the CSS selector I am using is wrong... require 'rubygems' require 'nokogiri' require ...

Getting essid via ioctl in ruby

To avoid relying on the wireless tools I want to get the essid directly from the device with ioctl, in C this wouldn't be a problem, but in Ruby it's quite different. The problem is following struct from wireless.h that is used as input/reply of ioctl: struct iw_point { void __user *pointer; /* Pointer to the data (in user space) ...

Is it a bad idea to run WxWidgets and GTK?

I'm currently writing a media player in Ruby (called Shroom) with a GTK interface, but I use WxWidget's MediaCtrl for cross-platform audio playback abstraction. I don't want to use WxRuby for the interface as well, as I need to work with GTK's TreeView. What kind of performance problems may arise from this? Is it so bad to have two main...

[Ruby] How to match/extract domain name from messy user input?

There is a form field where users should input domain names in the form of "google.com". However, taking into consideration confused users, I want to be able to clean the input to the exact form of "google.com" in case they type in the following cases: http://www.google.com http://google.com google.com/blah www.google.com ..and other i...

Can't find rmagick gem when required in script

I've recently run into an issue with mod_passenger and gems. I have a couple of gems install and I can find them in my /opt/ruby-enterprise/lib/ruby/gems/1.8/gems directory. One is will_paginate and the other is rmagick. For some reason, when my rails app starts up, it finds will_paginate just fine, but says it can't find rmagick. I...