ruby

How does Ruby evaluate math expressions to true/false?

I am going through the Ruby tutorials from Learn to Program, and I am having a problem with a while loop. This is what I want to do: while y_now % 4 y_now += 1 puts y_now % 4 end gets I'm using the gets just to pause execution so I can see the output. (This is looking for a leap year, btw) Anyways, when y_now % 4 = 0, the wh...

Render not rendering right page

This might be impossible to answer since there are probably too many variables here, but I thought I'd give it a shot since I always find other answers here. I am still fairly new to rails. So, I have a bills model/controller/view. I want to create a new bill. I will be editing out the things that shouldn't matter much, but if they are ...

String#split in Ruby not behaving as expected

File.open(path, 'r').each do |line| row = line.chomp.split('\t') puts "#{row[0]}" end path is the path of file having content like name, age, profession, hobby I'm expecting output to be name only but I am getting the whole line. Why is it so? ...

Read and write xml file using Nokogiri

I'm newbie to Nokogiri ruby gem. I'm wondering how to read and write back to an xml file. The requirement is that I parse xml file, make some changes, and save it. f = File.open("elevate.xml") xml = Nokogiri::XML(f) query = Nokogiri::XML::Node.new "query", xml query["text"] = "bank" query.parent = xml.root f.close This above code doe...

RabbitMQ/AMQP unhandled channel error - NOT_FOUND

I'm trying to publish messages to RabbitMQ, from a Ruby script (using Bunny) and consume them from a node.js server (using node-amqp). The first message arrives successfully, but then an error is logged inside node.js, and the connection closes, and no further messages are received: mu:charlie-node tom$ node charlie.js 22 Jul 09:11:04...

XML Response Parsing

Sorry if my question is a little repetitive, but I did not find an answer for my question so I post it here. So, here is this URL which I use to generate a security-token: api.sandbox.inrix.com/Traffic/Inrix.ashx?Action=GetSecurityToken&vendorId=1043016094&consumerId=94ce0781-b32f-4da5-b80b-8ca00cfb2194 The response of typing the abov...

What is the modern way to structure a ruby gem?

Has much changed with the release of Bundler? Is there a template that can be used as a base? What are the best practices? ...

how to send a post request and get the response in ruby

how to send a post request and get the response in ruby request is, name=$name_val URL is http://example.com/a/2 how do i do this in python or ruby? ...

How to store arrays in a file in ruby?

I am new to ruby. Can anyone tell me how can we store arrays in a file? ...

Overriding instance variable array’s operators in Ruby and scoping

I have a test class and a box class, in the test class i have a var called boxHolder, which is an array, i want to override the << method for this array. Inside the singleton how can i access moski_call ? class Test attr_accessor :boxHolder def initialize() super self.boxHolder = Array.new class << @boxHolder def <<...

Why does this method for pluralizing category names not work in all cases?

PLURALIZATION_EXCEPTIONS = "hardware",'memory' def pluralize_category_name(name) category = name.split(' and ') exceptions_to_exp = "" category.map! { |e| if e.match(/^[A-Z]+$/) and !e.match(/^[A-Z]+S$/) e = e.pluralize end (PLURALIZATION_EXCEPTIONS.include?(e.downcase) || e.match(/^[A-Z]+S$/) || e.match...

LocalJumpError ( No Block Given ) in Rails

I'm running this method, and this works perfectly on my local machine but does not work on my remote server. I get that it's looking for a block, but I'm not sure 'where' or 'how' to place it. Here's my broken method: def generate_csv if params[:print_envelopes] @signups = CardBatch.find(params[:id]).card_signups.each.reject { |a...

How do I read the content of an Excel spreadsheet using Ruby?

I am trying to read an Excel spreadsheet file with Ruby, but it is not reading the content of the file. This is my script book = Spreadsheet.open 'myexcel.xls'; sheet1 = book.worksheet 0 sheet1.each do |row| puts row.inspect ; puts row.format 2; puts row[1]; exit; end It is giving me the following: [DEPRECATED] By requiri...

How to set a content-type for a specific file with Rack?

I want to have Rack serve a specific file with a specific content type. It's a .htc file and it needs to be served as text/x-component so that IE will recognize it. In apache I would just do AddType text/x-component .htc How can I achieve this with Rack? Currently the file is served by Rack::Static, but I didn't find an option to set ...

Rails: base64 and character escaping problem

In my app I need to encode a string via base64, escape it's possible special characters and put it into a URL. I do the following: string = "[email protected]" enc = OpenSSL::Cipher::Cipher.new('DES-EDE3-CBC') enc.encrypt('dummy_salt') encoded = URI.escape(Base64.encode64(enc.update(string) << enc.final)) The problem is, that ...

Programming Ruby with Vim on Windows

Hi, I'm just starting learning Ruby and decided "wth, let's pick up vim and get used to it in the process". That sounded like a great idea until I actually downloaded gvim and started ... staring at it. Ok, I'm not a total noob, I know how really basic things are done in vim, what I'm looking for is how to setup vim and what plugins I n...

Ruby - How to reproduce the pointers/references behavior ?

Hey :) I am currently trying to create an engine for a new application. Design is quite complex and i realy need a way to create a reference or a pointer or what ever way to remotely modify and access the datas. Datas must be kept in a single place and must not be duplicated. Is there a way do get an object by reference ? For example ...

How Do I Mix 2 Rails Models Into a Single Find?

I have two models that are not related (in the db sense) but have some common columns (Name, Email, etc.). Class Account < ActiveRecord::Base end Class LegacyAccount < ActiveRecord::Base end For various reasons, I cannot merge these into a single model or do STI. But I would like a simple view that displays all records from both m...

Ruby formating '1' as '1st', '2' as '2nd' etc..

Pretty trivial question, I'm just looking to find out if anything is baked into ruby or rails to handle this. ...

How to extend Ruby ERB for handling %= tags as well?

I am using ERB for metaprogramming of some math language. If I could extend ERB functionality to handle %= tags, it would allow me to simplify my sources significantly. I simply want to get output of the line in analogy with <%= %>. I have tried to dig into /usr/lib/ruby/1.9.1/erb.rb file, but got lost very quickly. May be you can help w...