I have two arrays like this:
keys = ['a', 'b', 'c']
values = [1, 2, 3]
Is there a simple way in Ruby to convert those arrays into the following hash?
{ 'a' => 1, 'b' => 2, 'c' => 3 }
Here is my way of doing it, but I feel like there should be a built-in method to easily do this.
def arrays2hash(keys, values)
hash = {}
0.upto(k...
I am trying to find a record in the database that has the lowest value of its datetime column. In simpler words, I want to find a record with the earliest time.
I can use minimum to find the lowest value, but that will only return the value itself, and not the whole record.
I suppose I could create another query, but I'm wondering if t...
I usually look to hivelogic.com for instructions on installing Ruby but I have run into problems trying to convert Dan's instructions for 1.8.7 to 1.9.1.
Any help?
...
Hey,
This is the example I keep seeing online as how to set cookies.
require "cgi"
cookie = CGI::Cookie.new("rubyweb", "CustID=123", "Part=ABC");
cgi = CGI.new("html3")
cgi.out( "cookie" => [cookie] ){
cgi.html{
"\nHTML content here"
}
}
I tried doing it this way and it sets the cookie and then comes up with a blank page.
#...
I am making a rails app and having a ridiculously hard time trying to get the Redbox plugin to work. I'm pretty sure I installed everything correctly and am using it properly. When I use one of the helper methods, it seems to generate javascript properly. For example when I type:
<%= link_to_redbox 'hello', 'test' %>
it generates:
<a...
Hi I'm using Alloy's Complex Form Example found here. The example he provides is a two level hierarchy and I'm trying to expand it to four.
He has javascript functions for adding nested items. Can someone show me how to expand for four nested layers?
'.add_nested_item': function(e){
el = Event.findElement(e);
template = eval(el...
Hello all
I'm writing a calendar application that needs to check for conflicts
between recurring entries. Each Entry object has a recurrences() method
which returns an array of ranges - each range contains the start and end
times of each future occurrence.
I need to check for conflicts between new and existing entries. I'm
doing this b...
Recently I changed a few nested resources in one of my applications to use shallow routing. It's working great and I've been able to simplify my views and controllers.
However, I've been using a path_prefix before:
map.with_options :path_prefix => "blog" do |blog|
blog.resources :posts do |posts|
posts.resources :comments
end
e...
I have a list of tasks (name, starts_at) and I'm trying to display them in a daily view (just like iCal).
def todays_tasks(day)
Task.find(:all, :conditions => ["starts_at between ? and ?", day.beginning, day.ending]
end
I can't figure out how to convert Time.now such as "2009-04-12 10:00:00" into the beginning (and end) of the da...
So I'm writing a small gem and I have a '/tasks' dir in it with some specific rake tasks. How do I make those tasks available automatically everywhere, where the gem is required? For example I wish I could run 'rake mygemrake:task' inside my rails root dir after I have the gem installed.
...
I seem to be unable to use :order_by for more than one column at a time.
For example, I have a "Show" model with date and attending columns.
If I run the following code:
@shows = Show.find(:all, :order => "date")
I get the following results:
[#<Show id: 7, date: "2009-04-18", attending: 2>,
#<Show id: 1, date: "2009-04-18", atte...
ActiveRecord objects of the class 'Location' (representing the db-table Locations) have the attributes 'url', 'lat' (latitude) and 'lng' (longitude).
Lat-lng-combinations on this model should be unique. The problem is, that there are a lot of Location-objects in the database having duplicate lat-lng-combinations.
I need help in doing ...
Does anyone know of a Ruby module that will take an integer and spell it out ( 1 => "one", 2 => "two", etc..)?
...
My Ruby on Rails application uses the following controller code to generate a sitemap.xml file:
class SitemapController < ApplicationController
layout nil
def index
headers['Content-Type'] = 'application/xml'
last_post = Post.last
if stale?(:etag => last_post, :last_modified => last_post.updated_at.utc)
respond_to...
I'm trying to write a helper method that accepts the name of a plural resource and returns a corresponding link. The essence of the method is:
def get_link(resource)
link_to "#{resource.capitalize}", resource_path
end
Clearly the resource_path part above doesn't work. What I'd like is to be able to pass foos to get foos_path and bar...
Need to convert the following code from Ruby to C#. However I'm kind of puzzled by the use of the yield keyword and the general syntax of Ruby. Can anyone that knows a little bit Ruby please help out and convert the code
class < < Cache
STALE_REFRESH = 1
STALE_CREATED = 2
# Caches data received from a block
#
# The difference between ...
I'm writing a little browser game as a project to learn RoR with and I'm quite new to it.
This is a little method that's called regularly by a cronjob.
I'm guessing there should be some way of adding elements to the potions array and then doing a bulk save at the end, I'm also not liking hitting the db each time in the loop to get the ...
I used to develop Java on the mac and it worked out well, combo of just using the terminal and IntelliJ. What are good tools that run on the mac for doing Ruby development
...
I'm having some beginner problems setting an FFI struct in Ruby. What I want to do is pass a pointer to a C string by setting a string property in an FFI::Struct object:
class SpSessionConfig < FFI::Struct
layout :api_version, :int,
:cache_location, :string,
:settings_location, :string,
...
I am storing a cost in my application. The cost is not formatted in the database. For example: 00.00 saves as 0, 1.00 saves as 1, and 40.50 saves as 40.5
I need to read these values from the database and convert them to strings for dollars and cents. For example: 0 --> cost_dollars = "00" & cost_cents = "00", 1 --> cost_dollars = "01...