ruby

How to get code of unicode character using Ruby 1.8.6?

If I add this to the beginning of my script: $KCODE = 'UTF8' require 'jcode' then I can walk over every char of a word containing unicode characters. Imagine a word containing umlauts or something, and I iterate over them like this: word.each_char do |c| # do something with c end If c is a unicode character and I print it's size,...

Ruby - How to prevent wiping your hard drive when using delete file and directory commands in your code

I'm writing some code that at run time may create or delete directories within the project path. I haven't really used ruby for file processing so i'm really uneasy about having code that, with a few mistypes weeks down the line, could result in wiping other directories outside of my project path. Is there anyway to make it impossible ...

Proper way of testing gems

If a gem has rails dependencies, do you think it is better to write the gem tests in a way they can be run standalone or run them under a rails project? ...

Netbeans: Navigate -> Go to declaration?

I'm finding that with Ruby or RoR code, Netbeans' "go to declaration" doesn't show as lit up. Is there some way to turn this on? Also: Eclipse with RadRails seems to have this for Ruby? RubyMine seems to as well... surely Netbeans can't be missing this normalish feature. ...

ruby module as collection of methods

I have a rails app that loads lots of data from some java services. I'm writing a module that will allow me to populate some select boxes with this data and I'm trying to include these properly so I can reference them in my views. Here's my module module FilterOptions module Select def some_select return "some information"...

Embed webserver in desktop app: wxRuby and Sinatra

I would love to give my windows based desktop applications a web interface and vice versa. My desktop application is written in wxRuby and the webserver is Sinatra (using webrick). The simplest idea was just to mash them together, this does not work. This code does not work. The webserver and gui app do not run simultaneously. The...

What is the opposite of Ruby's include?

Hello, If I have a class instance, containing several included modules, can I dynamically un-include a specific module (In order to replace it)? Thanks. ...

In Rails, how do you render JSON using a view?

Suppose you're in your users controller and you want to get a json response for a show request, it'd be nice if you could create a file in your views/users/ dir, named show.json and after your users#show action is completed, it renders the file. Currently you need to do something along the lines of: def show @user = User.find( params...

Ruby Select method (for an array) problemos

I'm running the following method and I'm successfully passing two arguments (inventory, quantity) into the method. However I'm incorrectly using .first and .each methods. I'm attempting to replace .each with the .select to select for the cart item with the Inventory id: 6 possible .each replacement: (does not function) inventory_to_incr...

Multiple Block Parameters with Sinatra

I'm trying to get this Sinatra GET request to work: get '/:year/:month/:day/:slug' do end I know you can get one param to work with block parameters: get '/:param' do |param| "Here it is: #{param}." end But how can I use multiple block parameters with the first code block? I'm open to other methods. ...

Capturing Ctrl-c in ruby

I was passed a long running legacy ruby program, which has numerous occurances of begin #dosomething rescue Exception => e #halt the exception's progress end throughout it. Without tracking down every single possible exception these each could be handling (at least not immediately), I'd still like to be able to shut it down at ...

:autosave property of has_many associations broken in Rails 2.3.4?

Before I post this as a bug to the rails team, I wanted to see if I'm doing something wrong that may be causing this behavior. Specifically, the :autosave property of has_many associations doesn't seem to be working as per the docs. For reference, here is the most current API documentation: http://api.rubyonrails.org/classes/Acti … ati...

Symbolic Group Names (like in Python) in Ruby Regular Expression

Came across this handy regular expression utility in Python (I am a beginner in Python). e.g. By using the regexp (?P<id>[a-zA-Z_]\w*) I can refer to the matched data as m.group('id') (Full documentation: look for "symbolic group name" here) In Ruby, we can access the matched references using $1, $2 or using the MatchData object...

Rails + Ruby 1.9 "invalid byte squence in US-ASCII"

After upgrading to ruby 1.9 we began to notice pages failing to render from the rails template renderer when a user used a non-ASCII character. Specifically "é". I was able to resolve this issue on one of our staging servers, but I have not been able to reproduce the fix on our production server. The fix that seemed to work the first ti...

How to add gem dependencies that host on other site in a gemspec file?

In a gemspec file, is there a way to add dependency gem that hosts on other sites like github? I build a gem that has a dependency to http://github.com/mbleigh/mash. I checked the method add_dependency, but it seems it doesn't have that functionality. Could anyone show me how to do this? ...

Encoding Ruby on Rails code?

Are there any applications out there that will let me encode my Ruby on Rails code so others can't read it? I plan on selling a few small applications, but I really don't want everyone knowing my code. Thanks. ...

Change Ruby Logger output dynamically

Hi, I'd like to change dynamically the output used by the Logger. in the lib: @log = Logger.new(p, 10, 1024000) in the main class: mylib_instance.log.set_log(STDOUT) # something like that, but this does not work Mickael. ...

Search for text in files in the path using ruby

I need to search all the *.c source files in the path to find a reference to a *.h header to find unused C headers. I wrote a ruby script but it feel very clumsy. I create an array with all C files and an array with all the H files. I iterate over the header file array. For each header I open each C file and look for a reference to the ...

Ruby on Rails - Optional Associations?

I would like to allow users to write comments on a site. If they are registered users their username is displayed with the comment, otherwise allow them to type in a name which is displayed instead. I was going to create a default anonymous user in the database and link every non-registered comment to that user. Would there be a bette...

Ruby Convert If Statements to Case

Is it possible to use a case statement to replace these if statements? if (a%3 == 0) then puts "%3" elsif (a%4 == 0) then puts "%4" elsif (a%7 == 0 && a%13 == 0) then puts "%%" ...