ruby

In Ruby, how can I initialize instance variables in new objects of core classes created from literals

class Object attr_reader :foo def initialize @foo = 'bar' end end Object.new.foo # => 'bar' ''.foo # => nil //.foo # => nil [].foo # => nil I want them all to return 'bar' Am aware that you can do this already: class Object def foo 'bar' end end But I specifically want to initialize a state variable. Also note th...

Unmarshalling exception in wsdl2ruby SOAP::FaultError detail attribute

I've used wsdl2ruby to generate a client for a web service. When a service method raises one of the exceptions defined in the WSDL I'd like to access the exception object's attributes. From what I can tell the exception object has been marshalled into a SOAP::Mapping::Object that's referenced by the detail attribute of the SOAP::FaultErr...

do not include required files into vim omnicompletion

If I try to autocomplete smth in a Ruby file, that has require 'xxx' statement, it starts to scan all files required (and files required by required files as well). and it does that every freakin time! Is it possible to make vim autocomplete to NOT scan required files or just files in particular path (e.g. app/ only)? ...

How to connect from ruby to MS Sql Server

Hi Crowd! I'm trying to connect to the sql server 2005 database from *NIX machine: I have the following configuration: Linux 64bit ruby -v ruby 1.8.6 (2007-09-24 patchlevel 111) [x86_64-linux] important gems: dbd-odbc (0.2.4) dbi (0.4.1) active record sql server adapter - as plugin ruby-odbc 0.9996 (installed without an...

RegExp Counting System

I'm trying to create a system where I can convert RegEx values to integers and vice versa. where zero would be the most basic regex ( probably "/./" ), and any subsequent numbers would be more complex regex's My best approach so far was to stick all the possible values that could be contained within a regex into an array: values = [ "...

How do I easily parse a URL with parameters in a Rails test?

I have a some code that embeds a return_to URL into a redirect (like OpenID) that I want to test: def test_uses_referrer_for_return_to expected_return_to = 'http://test.com/foo' @request.env['HTTP_REFERER'] = expected_return_to get :fazbot # @response.redirected_to looks like http://service.com?...&return_to=[URI-encoded ver...

Why can't I access the controller object in RSpec when testing a controller?

I'm using rspec-rails, version 1.2.6. In a controller test describe WebsController do ... I don't seem to have access to the controller object in order to stub methods. For example, the following won't work: before :all do @feed = mock_model(Feed) controller.should_receive(:feed_from_params).and_return @feed end I g...

In Ruby what does the "receiver" refer to?

I'm reading a document that talks about a method having a receiver. What's a receiver? ...

"k.send :hello" - if k is the "receiver", who is the sender?

In the example below, why do we say "k.send :hello" instead of "k.receive :hello" if, as stated elsewhere, k is actually the receiver? It sounds like k is the sender rather than the receiver. When we say "k.send :hello" who is sending, if not k? (Are you as confused as I am?) class Klass def hello "Hello!" end end k = Klass....

Returning from ruby blocks outside of their original scope

I wanted to make something that looks like this: def make_wrapped_block(&block) puts "take_block:before" func = lambda do puts "Before calling make_wrapped_block's passed block" block.call puts "After calling make_wrapped_block's passed block" end puts "take block:after" func end def make_block make_wrapped_bloc...

Is the "caller" in Java the same as the "receiver" in Ruby?

If I say x.hello() In Java, object x is "calling" the method it contains. In Ruby, object x is "receiving" the method it contains. Is this just different terminology for expressing the same idea or is there a fundamental difference in ideology here? Coming from Java I find Ruby's "receiver" idea quite baffling. Perhaps someone coul...

Does Ruby have an equivalent of Python's twisted framework as a networking abstraction layer?

From my understanding, Python's twisted framework provides a higher-level abstraction for networking communications (?). I am looking for a Ruby equivalent of twisted to use in a Rails application. ...

Ruby - share logger instance among module/classes

Working on a little Ruby script that goes out to the web and crawls various services. I've got a module with several classes inside: module Crawler class Runner class Options class Engine end I want to share one logger among all those of those classes. Normally I'd just put this in a constant in the module and reference it like ...

how to write nslookup programmatically?

instead of using exec in our script to do an nslookup, is there an easy way to write it programmatically in PHP, Python, or Ruby? ...

What is the best way to duplicate a calculation in Ruby & JavaScript?

I have a Ruby on Rails application and I need to duplicate some computations in both Ruby and JavaScript. Ruby is used in the server side but I also need to compute a students grade on the browser using JavaScript. My first thought is to build up a JavaScript function using strings, convert it to JSON, ship it to the browser where it i...

What is "main" in Ruby?

If I run this file as "ruby x.rb": class X end x = X.new What is the thing that is calling "X.new"? Is it an object/process/etc? ...

How do I target a specific commit SHA with capistrano deploy

I am wondering how I can target a specific commit SHA in Git for deployment, using Capistrano? It should be something like cap deploy --version=<sha targeted> Can't seem to find the answer to this after a lot of searching. ...

Reuse cucumber steps

I want to reuse some cucumber steps but can't seem to find the right way. I want to write a step like: Given /^I login with (.*) credentials$/ |type| # do stuff with type being one of "invalid" or "valid" end But then have another step like: Given /^I login successfully$ # call "Given I login with valid credentials" end So in ...

What is the * operator doing to this string in Ruby

Given the Ruby code line = "first_name=mickey;last_name=mouse;country=usa" record = Hash[*line.split(/=|;/)] I understand everything in the second line apart from the * operator - what is it doing and where is the documentation for this? (as you might guess, searching for this case is proving hard...) ...

Would Ruby be a bad first language to teach to High-Schoolers with no programming experience?

I'm planning on teaching a programming elective next year at my high-school. The students would be from 9th to 12th grade with minimal programming experience in ti-basic at best. I've yet to decide on what language to use in it. I fear that if I use something ultra-interpretive and abstract like Ruby or Python I'll be doing them a disser...