ruby

Ruby Switch Between File and Standard Input

How would you create a variable that could be read. It would read from a certain file if it exists, otherwise it would read from standard input. Something like: input = File.open("file.txt") || in This doesn't work, but I think this should be done pretty often, but I can't find a nice way to do it. ...

Ruby tutorial for experienced programmers

I'm looking for a Ruby tutorial which would be usable for Java programmers with 8+ years of experience. I don't need another tutorial which explains basic programing/OOP/OOD concepts (inheritance, polymorphism, encapsulation, classes, constructors, hashes, etc.), just a fast-track tutorial (or even a reference?) which could tell us how t...

multiline matching with ruby

I have a string variable with multiple lines: e.g. "SClone VARPB63A\nSeq_vec SVEC 1 65 pCR2.1-topo\nSequencing_vector \"pCR2.1-topo\"\nSeq_vec SVEC 102 1710 pCR2.1-topo\nClipping QUAL 46 397\n I would want to get both of lines that start with "Seq_vec SVEC" and extract the values of the integer part that matches... string = "Clone V...

Has anyone ever written a clean MVC based ruby-gnome2 application?

Do you know of any serious and clean, (by clean I mean rails like) mvc based ruby GUI application with GTK. Actually, if there are any ruby gui applications that are clean and mvc based, I would be delighted, no matter what toolkit. What I am looking for are basically some good open source apps, where I can look at the code, for inspir...

Ruby Convert String to File

Is it possible to convert a string to a file without writing it to disk? I would like to work ubiquitously with either a string of a file: input = "123" if (ARGV.length == 1) input = File.open(ARGV[0]) #do stuff with input Can I create a file from a string (without writing to disk)? Otherwise, I would not be able to do input.read...

How to modify the :confirm or add a new method to link_to UrlHelper?

I use this code to delete record: <%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %> When I click the "Destroy", it will have an alert box prompt out and ask me to destroy or not. Can I modify the :confirm to an AJAX call? or can I add a :myConfirm in to the link_to tag to implement my own :myConfirm? T...

Ruby String Integer Scanning

Is there a Ruby equivalent of the Java Scanner? If I have a string like "hello 123 hi 234" In Java I could do Scanner sc = new Scanner("hello 123 hi 234"); String a = sc.nextString(); int b = sc.nextInt(); String c = sc.nextString(); int d = sc.nextInt(); How would you do this in Ruby? ...

Can I set the default string encoding on Ruby 1.9?

This might sound minor, but it's been driving me nuts. Since releasing an application to production last Friday on Ruby 1.9, I've been having lots of minor exceptions related to character encodings. Almost all of it is some variation on: Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8 We have an ...

Editing Text in a Nokogiri Element or Using Regex

Is there a way to edit the text of a nokogiri element? I have a nokogiri element that contains a list element (<li>) and I would like to remove some characters from the text while preserving the <li> html. Specifically, I want to remove a leading ":" character in the text if it exists. It doesn't look like there's a text= method for n...

Is there a ruby function to do !(val).nil? ?

I frequently find myself doing this: !(val).nil? My code would be prettier if there was a method like val.exists? Is there something like that? ...

Ruby : `&:symbol` syntax

Possible Duplicate: Ruby/Ruby on Rails ampersand colon shortcut Seen in Railscast #167 : def tag_names @tag_names || tags.map(&:name).join(' ') end What is this &:name syntax, what does it do ? I understand what a symbol is, but what other kinds of objects this & can be prepended to ? Duplicate question : http://stac...

Link_to instead of button to submit a form

Hi, I'm new at ruby on rails, and I wanted to accomplish the following: I've got a list with check boxes(one per row) and I'd like to have a link button(not a common button or submit) so when I click it I call an action from a controller. My questions are: How can I call a controller action with the link_to? How do I get the checkbo...

Deploy to only one role / server with capistrano

I'm trying to set up multiple roles, one for live, and another for dev. They look like this: role :live, "example.com" role :dev, "dev.example.com" When I run cap deploy, however, it executes for both servers. I've tried the following and it always executes on both. cap deploy live cap ROLE=live deploy What am I missing? I know I ...

Nokogiri Element Removal Using Regex in Ruby

This seems like the hardest problem I have had yet, but maybe I am making it harder than it needs to be. I need to remove an unknown number of nested elements that may or may not be at the beginning of a sentence. The span elements contain a number of words in parentheses. So in the sentence: (cryptography, slang) An internet firewa...

Ruby "instance variable not initialized" warning.

In writing some "learning the language" code in ruby, as part of a linkedList implementation, I came across this warning: In the "add" method, the head is created if it doesn't already exist, i.e. def add(value) new_node=LinkedListNode.new(value) if !@head @head=new_node else self.find {|node| node.next ==nil }.ne...

Url class loading in Ruby

Is there a simple way to load a class from a URL in Ruby? I'm looking for something analogous to Java's URL class loader. Example: require 'http://github.com/outoftime/sunspot/blob/master/lib/light_config.rb' or - require 'http://www.codehost.com/application.tgz' (signed archive containing multiple files?) I'm considering adding a...

Sinatra enable :sessions not working on passenger/apache

Hi guys, Am having trouble getting enable :sessions to persist for a simple Sinatra app hosted on passenger/apache. I'm storing the state of session[:authorized] in a cookie. It works locally when hosted on Rack::Handler::Mongrel but I can't seem to get same behaviour on passenger. I've tried two methods for enabling sessions, both of ...

Error installing Ruby 1.9 with RVM under Snow Leopard

Hello I've got some problems to get Ruby 1.9.2 compiling with rvm (Ruby Version Manger) under Snow Leopard. I want everything possibly to be 64-Bit so I already added ARCHFLAGS="-arch x86_64" to my .bash_profile. Then, when running: rvm install 1.9.2 -C --enable-shared I get an error during compiling. Here is the make.error.log: ...

problems installing thin on Windows

Hi all, I'm trying to install Thin on Windows (ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]) but get an error message: C:>gem install thin Building native extensions. This could take a while... ERROR: Error installing thin: ERROR: Failed to build gem native extension. C:/rails/ruby/bin/ruby.exe extcon...

Why can't I access some library classes when I'm in a thread?

Why does the following require "bio" threads = (1..2).map do Thread.new do seqs = ["gattaca"] * 5 alignment = Bio::Alignment.new(seqs) end end threads.each {|th| th.join} ; nil give this error message? NameError: uninitialized constant Bio::Alignment from (irb):6 from (irb):10:in `join' from (irb):10 fro...