ruby

Remove substrings in ruby

Given an array of strings, array1 = ["abcdwillbegoneabcccc","cdefwilbegokkkabcdc"] and another array of strings which consist of patterns e.g. ["abcd","beg[o|p]n","bcc","cdef","h*gxwy"] the task is to remove substrings that match any of the pattern strings. for example a sample output for this case should be: ["willbegonea","wilbeg...

How do I access options passed to an acts_as plugin (ActiveRecord decorator) from instance methods?

At the moment I store each option in its own class attribute but this leads to hard to read code when I need to access the passed options from instance methods. For example if I pass a column name as an option I have to use self.send(self.class.path_finder_column) to get the column value from an instance method. Notice I have prefixed ...

Can a Ruby script tell what directory it’s in?

Hello, everyone! Inspired by this question, what's the Ruby way to do this? ...

Elegant command-parsing in an OOP-based text game

I'm playing with writing a MUD/text adventure (please don't laugh) in Ruby. Can anyone give me any pointers towards an elegant, oop-based solution to parsing input text? We're talking about nothing more complex than "put wand on table", here. But everything needs to be soft; I want to extend the command set painlessly, later. M...

Getting Private Method error in Ruby on Rails

I have a controller: class StatsController < ApplicationController require 'time' def index @started = "Thu Feb 04 16:12:09 UTC 2010" @finished = "Thu Feb 04 16:13:44 UTC 2010" @duration_time = stats_duration(@started, @finished) end private def stats_duration(started, finished) time_taken = distance_o...

How to develop a gem in staging environment?

I am trying to hack through a forked gem (buildr). As such I cloned it from github and began to butcher the code. The official gem is installed on my system (under /usr/lib/ruby.../gems/buildr...). There is an executable which I need to use in my dev process - buildr. Now I want the buildr executable and the library to point to my forke...

undefined method error

Hey all, I edited this message do to sloppiness and changes. def student_test @student = Student.for_test.find(params[:id]) if params[:id] @student ||= Student.new run_sequence :testize end def test_finalize Student.transaction do if (params[:student]) and @student.update_attributes(params[:student]) @student.test! end room = Room...

Using Sinatra and ruby to interface to HTML buttons

Disclosure: I know nothing about web programming Background to problem: I have an environmental testing chamber testing embedded computers at various temperatures. It is controlled by a windows application. I can control the chamber via ruby and the Win32API interface of the control application. The chamber is far from my office and I w...

How do I test a crawler? ie: mock web requests

So I'm testing the individual parts of my crawler and now I've hit a stump: mocking the actual requests. I don't want to make a request every time I run a spec. Anyone ever tried starting a webrick server in the test helper? Something like myserver = SomeServerLib.start('localhost', some_port) myserver.serve_directory(a_directory_w...

Does Ruby support conditional regular expressions.

Just a language feature question, I know there's plenty of ways to do this outside of regexes (or with multiple regexes). Does ruby support conditional regular expressions? Basically, an IF-THEN-ELSE branch inside a regular expression, where the predicate for the IF is the presence (or absense) of a captured group in the expression. I...

Should I use ruby threads or just not use ruby altogether for threading?

I have a choice to develop an app which will rely heavily on threading (up to to 200). I know I can use other Ruby interpreters for threading such as JRuby. But there are 2 things: 1) Jruby doesn't support 1.9 yet, so that is a no. Is there any other non-green thread interpreter that supports at least 1.9 as that is a prerequisite for m...

Help Soap Response using ssl basic authentication and client certificates

Hello all I'm trying to issue a simple request to a protected wsdl and web service using ssl, client certificates and basic authentication. Here's the code require 'savon' client = Savon::Client.new "https://example.com/service?wsdl" client.request.http.ssl_client_auth( :cert => OpenSSL::X509::Certificate.new(File.read("cert.pem")), ...

Ruby On Rails - How to get the Object Method Caller

Let me explain my problem: I have 2 models: class User < AR::Base has_many :contacts end class Contact < AR::Base belongs_to :user belongs_to :user_contact_id, :class_name => "User", :foreign_key => "user_contact_id" # The "contact" is an ID from the table user. def self.is_contact?(user_contact_id) # CHECK IF THE RECORDS EXIST ...

Regex for matching all words between a set of curly braces

A simple question for most regex experts I know, but I'm trying to return all matches for the words between some curly braces in a sentence; however Ruby is only returning a single match, and I cannot figure out why exactly. I'm using this example sentence: sentence = hello {name} is {thing} with this regex to try and return both "...

What is this &:last Ruby Construct Called?

What are things like survey.map(&:questions).flatten.compact called, so I can find more information about them :). What problems does that &: solve, or what is it doing exactly? Is it used in other languages? ...

How do you overload dynamically extended methods in ruby?

I have some modules in an array. All the modules define a method called "process", and I'd like to call each of these process methods in sequence. The code I have looks something like this(assume the modules are all defined inside the Mod class): modules.each do |mod| extend Mod.const_get(mod) process(data) end This works fine for...

Ruby regular expression using gsub

Hi I'm new to Ruby and regular expressions. I'm trying to use a regular expression to remove any zeros from the month or day in a date formatted like "02/02/1980" => "2/2/1980" def m_d_y strftime('%m/%d/%Y').gsub(/0?(\d{1})\/0?(\d{1})\//, $1 + "/" + $2 + "/" ) end What is wrong with this regular expression? Thanks. ...

How do I run a system command in a cydget using objective c or javascript?

Hi! I am trying to run a Ruby script from a cydget (cydget is a framework made by Saurik for writing lockscreens on the iPhone, using cycript, which is a mix of javascript & objc - see http://www.cycript.org/ ) The script will first check to see if a WEBrick server is running, if so it will quietly exit, if not it will start the server...

Ruby: Checking if URI is HTTPS?

I would like to check if the URI will need SSL authentication: url = URI.parse("http://www.google.com") # [some code] if url.instance_of? URI::HTTPS http.use_ssl=true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end However, those few lines throw the following error.. /usr/lib/ruby/1.8/uri/common.rb:436:in `split': bad URI(is...

Shoulda on Rails - should render_with_layout deferred

Hi all, I would appreciate if someone could help/explain the following please. I am trying to test a controller with shoulda to see if a correct layout is used. The method should_render_with_layout throws a NoMethodError whilst should render_with_layout passes but says the function is deferred. Any help would be appreciated.. Thanks ...