ruby

vim/macvim: locate where a method/symbol is defined

I'm using macvim/vim for most of my Ruby + Ruby on Rails development. Is there currently a way to jump to where a method was defined within a project, even if it's not in the same file as where it's being invoked? Either a language agnostic way or a Ruby/Rails specific way works. ...

Using Net SSH to execute commands with sudo

I've been trying to write a small library using Thor to help assist me in quick creating new projects and sites. I wrote this small method: def ssh(cmd) Net::SSH.start( server_ip, user, :port => port) do |session| session.exec cmd end end to just assist me in running quick commands on remote servers when needed. The proble...

safe_concat error while using table_builder rails plugin

I am using table_builder plugin and I am getting the following error undefined method `safe_concat' for ActionView::Base:0x00000100a81ef0 What is safe_concat method and Can someone please tell me what am I doing incorrectly View Code <div id="event"> <% calendar_for @events do |calendar|%> <%= calendar.head('mon', 'tue',...

What does the ruby method "in" do?

It's a bit hard to search for it. This might actually be a Rails method. ...

How to create a 2D array of objects in Ruby?

I am creating a map for my roguelike game and already I stumbled on a problem. I want to create a two dimensional array of objects. In my previous C++ game I did this: class tile; //found in another file. tile theMap[MAP_WIDTH][MAP_HEIGHT]; I can't figure out how I should do this with Ruby. ...

Static methods in ruby modules?

Is it possible to declare static methods in a module in ruby? module Software def self.exit puts "exited" end end class Windows include Software def self.start puts "started" self.exit end end Windows.start The example above will not print out "exited". Is it only possible to have instance methods in a module...

Add whitespace before and after a string in ruby?

I want to add a whitespace before and after random strings. I have tried using "Random_string".center(1, " ") but it doesnt work. Thanks ...

how to sort file in ruby.

This is my file content. Receivables=Por cobrar Payables=Cuentos por pagar ytdPurchases.label=Purchases YTD validationError.maxValue=Value is too large, maximum value allowed is {0} i want to sort this content in alphabetic order ... how may i do that ?? Update: This code will sort my file. new_array = File.readlines("#{$base_prope...

Ruby modules and extend self

In what sort of situation is the code: module M extend self def greet puts "hello" end end more beneficial to use over say something like: module M def self.greet puts "hello" end end In the top, one is an instance method being extended, and the latter is just a class method, but when calling either method,...

Emedding Ruby into GTK+ application

Hi. I'm creating GTK+ application that has embedded Ruby interpreter, small logic functions w/o side effects (those who only return some values) are working ok already, but I would like Ruby scripts to interact with main programs widgets. I know there is ruby-gtk2 gem, but how do I tell Ruby that widget passed from C must be wrapped wit...

How could one block detect that its inside another block?

This is my code: def block puts "from block" yield end block do puts "from command line" block do end end Here is the output: from block from command line from block I wonder how the second block could detect that its inside another block (of the same method). So that the output will be this instead: from block 1 from...

How can I choose which version of a module to include dynamically in Ruby?

I'm writing a small Ruby command-line application that uses fileutils from the standard library for file operations. Depending on how the user invokes the application, I will want to include either FileUtils, FileUtils::DryRun or FileUtils::Verbose. Since include is private, though, I can't put the logic to choose into the object's init...

How to include a Module's constants and variables?

I have a Module with a constant and variable. I wonder how I could include these in a class? module Software VAR = 'hejsan' def exit @text = "exited" puts @text end end class Windows extend Software def self.start exit puts VAR puts @text end end Windows.start Is this possible? Thanks! ...

How do I handle errors or bad requests in my Rails REST API?

I have a Rails app that includes a JSON API interface. When values are properly specified, the controller handles the happy path just fine and JSON is rendered as output. However, if there's a problem with the input, an exception is raised and some templates in rescues are rendered instead. I'd really just like to return a JSON error al...

Is there an elegant way to exclude the first value of a range?

Let's say I have a range from 0 to 10: range = 0...10 Three dots mean, that the last value (10) is excluded: range.include? 10 => false Now, is there a similar and elegant way to exclude the first value? For the above example, this would mean to include all values that are bigger (>, not >=) than 0 and smaller than 10. ...

RSpec sees xits but no other tests?

So, whenever I run "rake spec" in my application directory, I see this: admin@nurvus:~/workspace/spec $ rake spec (in /Users/admin/workspace/) DEPRECATION WARNING: Rake tasks in vendor/plugins/abingo/tasks, vendor/plugins/delayed_job/tasks, vendor/plugins/funkytown/tasks, vendor/plugins/funkytown/tasks, vendor/plugins/git_helper/tasks, ...

How can I reset a factory_girl sequence?

Provided that I have a project factory Factory.define :project do |p| p.sequence(:title) { |n| "project #{n} title" } p.sequence(:subtitle) { |n| "project #{n} subtitle" } p.sequence(:image) { |n| "../images/content/projects/#{n}.jpg" } p.sequence(:date) { |n| n.weeks.ago.to_date ...

Prevent Rails app from responding to unintended AJAX request from jQuery

I've built out a fairly complex Rails (2.3.8) app with a lot of jQuery ajax requests. There is an occasional bug, which I have difficultly replicating, where a jQuery $.ajax({..}) request will request a page it shouldn't (like the dash page, which is never called with an ajax request)... What ensures is absolutely madness. Incredibly st...

How to build a Ruby hash out of two equally-sized arrays?

I have two arrays a = [:foo, :bar, :baz, :bof] and b = ["hello", "world", 1, 2] I want {:foo => "hello", :bar => "world", :baz => 1, :bof => 2} Any way to do this? ...

What`s the diff between ruby 1.8.7 and 1.9.1 in module require?

There is a module require 'iconv' module Escape def escape(string) return_value = Iconv.conv('ascii//translit//IGNORE', 'utf-8', string).to_s end end It`s work in 1.8.7 but not in 1.9.1 The error message is "NameError (uninitialized constant Escape::Iconv)" and the follow is work in 1.9.1,Why??????? (my rails is rails 3 in ...