ruby

Regular expression to exclude special characters

Hi, I need a regex for a password which meets following constraints in my rails project: have a minimum of 8 and a maximum of 16 characters be alphanumeric only contain at least one letter and one number. My current regex is: /^(?=.*\d)(?=.*([a-z]|[A-Z])).{8,16}$/ This allows me all the restrictions but the special characters pa...

How do I tell from within a ruby script whether it has been run from the command line or from a non-interactive process?

How do I tell from within a ruby script whether it has been run from the command line, i.e. like this: > ruby myscript.rb or from a non-interactive process, e.g. cron? ...

returning an array that contains an array and hash in ruby method?

HI is it possible to return an array that contains an array and hash from a method in ruby? i.e def something array_new = [another_thing, another_thing_2] hash_map = get_hash() return [array_new, hash_map] end and to retrieve the array: some_array, some_hash = something() thanks ...

Prevent Rails from caching a password

I'm setting up an edit profile page. And the password is available for change on it. The only problem is that the password_field gets prepopulated with the password, and when they just want to change their name, they must also change their password as well. Does anyone know a common workaround to this? ...

Learning PHP - is there a php alternative to http://tryruby.org/?

I've got a friend trying to learn php (they've never used a programming/scripting language before). They have a minimal understanding of html. I've used tryruby.org a little bit once, and I'm hoping there's something similar for php. I learned using books, but I don't want them to have to go through setting up a server on their localhost...

Rails: Overriding const_missing within a module

Within my Rails application I have a module defined this way: module WhateverModule class WhateverClass ... end end This file (whatever_class.rb) is located under /app/models/whatever_module const_missing is being overriden by Rails and despite I did some workarounds, involving initializers, I wish I could make it in a better...

Customize Rails Hash.from_xml with custom conversion

I want to convert my XML document to Hash in Ruby/Rails. Actually, the default conversion by Hash.from_xml in Rails works for me except in one case. I have a list of items contained in <item-list> element, these items can be of different types though. For instance, standard-item and special-item, each of which has different set of child...

Can I use Ruby's OptionParser to accept an arbitrary argument pattern?

Let's say that I have a simple Ruby app where I want the first argument (if any) to specify the environment: TEST, DEVELOPMENT or PRODUCTION (with DEVELOPMENT being the default if no argument is given). For instance, ruby myapp.rb test would run it in TEST mode. Also, shorthands should be accepted, so that for instance ruby myapp.rb ...

MongoMapper Embedded Documents

I have products as an embedded document inside the category class as shown below: require 'mongo_mapper' class Category include MongoMapper::Document key :Name, String key :NumberOfProducts, Integer many :products end and here is the Product class: require 'mongo_mapper' class Product include MongoMapper::EmbeddedDocument ...

find class methods for every model.

How may i find class methods of the specific model. Like instance method ModelName.instance_methods ...

Why won't Bundler install vPim?

I have the following in my Gemfile: clear_sources bundle_path "vendor/bundler_gems" source "http://gemcutter.org" ... gem "vpim", "~> 0.658" # a whole bunch of other gems When I run gem bundle, I get everything installed except vpim. Running gem bundle --list | grep vpim returns nothing. I'm using bundler08 because t...

Rails: Resetting form fields with page refresh

I've got a Rails form that has an observe_field which performs an ajax request when a drop down list is changed. The ajax causes the form to be re-rendered (the form is in a partial) with an extra param that causes some text on the page to change. All that works fine, but when I refresh the page (I'm running firefox), the text is reset a...

Refactoring respond_to? method in if-elsif-else block in Ruby

How simplify this part of code: def value_format(value) if value.respond_to? :to_actor value.to_actor elsif value.respond_to? :to_subject value.to_subject elsif value.respond_to? :to_json value.to_json elsif value.respond_to? :to_hash value.to_hash else value.inspect end end Maybe is better answer than ...

Testing against an external UDP API. Where to start?

I'm writing a client library against an API that communicates using UDP through a socket connection. I'm trying to write tests as I go however I'm running into a few problems. Should I just build a testing server and start that up in my test helper? I don't want to hammer the external servers when running my tests. Or should I just moc...

Dynamically set method on before_save.

In my controller i want to dynamically bind my instance method to the before_save callbacks. Is there any ways we can dynamically bind methods to the callback from controller side.... EDIT : Controller This original code.. def amfupdate set_properties validate_record if params[:csv_header][:validate_record] == "Y" #On this cond...

RSpec: difference between "should == ..." and "should eql(...)"

In RSpec, what's the difference between using should == ... and should eql(...)? I noticed that the RSpec documentation always uses eql, but == is less typing and easier to read. What am I missing? ...

Is this a Bug in the Array.fill method in Ruby?

Should this be the case i.e. I am misunderstanding, or is it a bug? a = Array.new(3, Array.new(3)) a[1].fill('g') => [["g", "g", "g"], ["g", "g", "g"], ["g", "g", "g"]] should it not result in => [[nil, nil, nil], ["g", "g", "g"], [nil, nil, nil]] Any advice would be most helpful! Regards, ...

Ruby equivalent to python __main__

If in a ruby file I define a function like so: def tell_the_truth() puts "truth" end is there an equivalent to python's main? if __name__ == "__main__": tell_the_truth() Is it to simply call the function inside the file? tell_the_truth ...

How can I detect a user's input language using Ruby without using an online service?

I'm looking for a library or technique to detect the input language of blocks of text provided by users. Online lookups (like Google translate) won't work for this task as I'm writing an app which must run offline. Thanks. ...

What kind of data structure is this??

I am pulling recent commits from github and trying to parse it using ruby. I know that I can parse it manually but I wanted to see if there was some package that could turn this into a hash or another data structure. commits: - parents: - id: 202fb79e8686ee127fe49497c979cfc9c9d985d5 author: name: This guy login: tguy e...