ruby

Subscribe to newsletter form rails 3

Hi, I'm new to rails and I would like to know how to make a form that submits to a database. I have tried devise but it seems that it deals with logins/users and it's not working for my purpose. I don't want anyone to do it for me, I have to learn :). ...

Sinatra Mongoid String not valid UTF-8

Hi, I wrote this little application : require 'rubygems' require 'sinatra' require 'bson' require 'mongoid' Mongoid.configure do |config| name = "articles" host = "localhost" config.master = Mongo::Connection.new.db(name) config.persist_in_safe_mode = false end class Article include Mongoid::Document field :title field...

How can I extract the words from this string " !!one!! **two** @@three@@" using Regex in Ruby

In IRB, I can do this: c = /(\b\w+\b)\W*(\b\w+\b)\W*(\b\w+\b)\W*/.match(" !!one** *two* @@three@@ ") And get this: => MatchData "one** *two* @@three@@ " 1:"one" 2:"two" 3:"three" But assuming I don't know the number of words in advance, how can I still extract all words out of the string". For example, it might be " !!one** *two* @@...

Capture variable amount of matches with regular expressions?

How if at all can I use regex to match a string with a variable number of matches. The strings I want to parse look like: 'Every 15th of the month' 'Every 21st and 28th of the month' 'Every 21st, 22nd and 28th of the month' ad infinitum... I want to be able to capture the ordinal numbers (15th, 21st etc) The language I'm using is R...

`&:views_count` in `Post.published.collect(&:views_count)`

I saw the code from here Post.published.collect(&:views_count) I guess it equals to .collect { |p| p.views_count } But I never saw this usage before, does this have a name? Where can I find more information about it? ...

how do i access instance variable of one class into other class define in other file in ruby

Hi all, I have one file named class1.rb this is structure module Person class Amit def initialize @a=10 end end end another file named class2.rb module Person class Sumit < Amit def aos puts "#{@a}" end end end i am not able to acess variable @a in Sumit how do i do that?? ...

How can I simulate the browser back button in Capybara?

We have a issue on our e-commerce site where users occasionally hit "checkout" twice and have their card charged twice. It's a common enough bug and easy to fix, but I'd like to test the solution in our capybara setup. Once I've called click_button('checkout'), is it possible for me to pretend I'm a user hitting the browsers back button...

Why can't I install ruby 1.9.2 on Ubuntu via apt-get?

apt-get install ruby Installs ruby 1.8.7 When I install ruby 1.9.2 from sources via ./configure make install, ruby is not installed (ruby -v gives nothing). So how can I install ruby 1.9.2 on Ubuntu? ...

How to configure an extra/different migrations folder

Hello. Me and a colleage are working in different projects that share some models. So, we are sharing the models thru a git submodule. Additionally, we'd like to be able to also share migrations: In this way, my collegue's migrations would be in the folder db/migrate/other_db of my project. How can I configure rails migrations to also...

Cucumber and Seed data

can we load seed data when start cucumber? Please support me a way. ...

How to check the status between 2 servers ?

There are 2 servers, they need to know the status(live oe dead) each other. my method is a long tcp connecting, Is there any better method? thanks. ...

ruby metaprogramming - getting method names and parameter info for a class

Hi All, I want to get class methods in an object. Please see the following example I have a class "user.rb" class User def say_name end def walk(p1) end def run(p1, p2) end end and I wrote the following code require 'user.rb' a = User.new arr = a.public_methods(all = false) Above code will return the method ...

Testing ssh connection

Hi, an important part of my project is to log in into remote server with ssh and do something with files on it: Net::SSH.start(@host, @username, :password => @password) do |ssh| ssh.exec!(rename_files_on_remote_server) end How to test it? I think I can have local ssh server on and check file names on it (maybe it could be in my t...

Ruby: Find non-transparent PNG and convert into JPG

What is the smartest way to find out if a PNG has transparency or not in Ruby? Is it OK if I just check if there's an alpha-channel? If yes: How do I check for an alpha-channel? I'm writing a script that will convert all PNGs without transparency into smaller JPGs. Thanks a lot for your help! ...

Given a String and an array of Strings, how do I efficiently calculate the occurrences of the array in String?

Let's assume text is a String and contains a text. tags is an Array of Strings. text = <<-EOS Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis au...

creating Models with sqlite3 + datamapper + ruby

How do you build a model with the following associations (i tried but couldn't get it to work): each Order has: a Customer, a SalesRep, many OrderLine that each has an item. I tried: when I do: Customer.all(Customer.orders.order_lines.item.sku.like => "%BLUE%") the output is :[] instead of: '[#<"Customer @id=1 @name="Dan Kubb">]' Whe...

How to reference an embedded document in Mongoid?

Using Mongoid, let's say I have the following classes: class Map include Mongoid::Document embeds_many :locations end class Location include Mongoid::Document field :x_coord, :type => Integer field :y_coord, :type => Integer embedded_in :map, :inverse_of => :locations end class Player include Mongoid::Document ...

How do I monkey-patch ruby's URI.parse method

Some popular blog sites typically use square brackets in their URLs but ruby's built-in URI.parse() method chokes on them, raising a nasty exception, as per: http://redmine.ruby-lang.org/issues/show/1466 I'm trying to write a simple monkey-patch that gracefully handles URLs with the square bracket. The following is what I have so far: ...

How do I create this multidemsional Array, Hash Combo?

I have this data and i need an output like this type of output.. I basically need to have all the venues and their dates and all the songs associated to them ....if anyone can think of a better structure and how to achieve it I would be very thankful... { ["Vector Arena - Auckland Central, New Zealand" => { "2010-10-10" => ["...

Using the expect{}.to change, isn't passing, but works in console

I'm trying to test a method on my model. Running in the console works, but my rspec test does not pass. Checkout the code: Model: def import(master) hsh = master.attributes.delete_if { |k,v| k == 'id' } self.update_attributes(hsh) end Rspec test: describe "#import" do let(:master) { Factory(:sheet, :work_order => 'M1234', :sa...