Is there a fast and clean way of returning a JSON hash back from any node in a Ruby on Rails' acts_as_nested_set without using recursion?
Here's the recursive solution for reference:
class Node < ActiveRecord::Base
has_many :products
def json_hash
if children.size > 0
children.collect { |node| { node.name => node.json_has...
I would like to execute an OS command from my ruby script but I want to add an argument from a ruby variable.
I know that's possible by using keyword system like that :
#!/usr/bin/env ruby
directory = '/home/paulgreg/'
system 'ls ' + directory
but is that possible by using the "backquotes or backticks syntax" ?
(I mean by using that ...
I'm in the process of maintaining a Ruby on Rails app and am looking for an easy way to find the hostname or IP address of the box I'm on (since it's a VM and new instances may have different hostnames or IP addresses). Is there a quick and easy way to do this in Ruby on Rails?
Edit: The answer below is correct but the clarification Cra...
Simple question, but one that I've been curious about...is there a functional difference between the following two commands?
String::class
String.class
They both do what I expect -- that is to say they return Class -- but what is the difference between using the :: and the .?
I notice that on those classes that have constants defined...
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
On Windows, when I do this:
gem install sqlite3-ruby
I get the following error:
Building native extensions. This could take a while...
ERROR: Error installing sqlite3-ruby:
ERROR: Failed to build gem native extension.
c:/ruby/bin/ruby.exe e...
As an exercise for myself, I was translating a sample program into various languages. Starting in C#, I had a visitor-pattern interface like so:
interface Visitor
{
void Accept(Bedroom x);
void Accept(Bathroom x);
void Accept(Kitchen x);
void Accept(LivingRoom x);
}
Moving to ruby (or python, ...
I was having a heck of a time figuring out how to login and logout using response objects from Rails. The standard blogs were ok, but I finally diagnosed it, and I wanted to record it here.
app.get '/'
assert_response :success
app.get '/auth_only_url'
assert_response 302
user = User.find(:user_to_login)
app.post '/signin_url',
...
Ruby setters -- whether created by (c)attr_accessor or manually -- seem to be the only methods that need "self." qualification when accessed within the class itself. This seems to put Ruby alone the world of languages:
all methods need self/this (like perl, and I think Javascript)
no methods require self/this is (C#, Java)
only setter...
I'm working on a Rails app and am looking to include some functionality from an earlier question I asked here. However, I'm having problems getting it to work. I was under the impression that I should just make a file in the lib directory, so I named it 'get_ip.rb', with the contents:
require 'socket'
module GetIP
def local_ip
or...
Quite a few methods exist for passing data into a Flex binary from a Rails application. Right now, I'm using the old e4x resultFormat with a xml.erb template. I've done AMF before, but I feel like inlining parameters into the embed itself is a better solution because you don't have to wait for the browser to load a swf binary and the b...
I'm planning on creating a social networking + MP3 lecture downloading / browsing / commenting / discovery website using Ruby on Rails. Partially for fun and also as a means to learn some Ruby on Rails. I'm looking for a social networking framework that I can use as a basis for my site. I don't want to re-invent the wheel.
Searching th...
For a project I am working on in ruby I am overriding the method_missing method so that I can set variables using a method call like this, similar to setting variables in an ActiveRecord object:
Object.variable_name= 'new value'
However, after implementing this I found out that many of the variable names have periods (.) in them. I ha...
I have a string, say '123', and I want to convert it to 123.
I know you can simply do some_string.to_i, but that converts 'lolipops' to 0, which is not the effect I have in mind. I want it to blow up in my face when I try to convert something invalid, with a nice and painful Exception. Otherwise, I can't distinguish between a valid 0 an...
Ruby has two different exceptions mechanisms: Throw/Catch and Raise/Rescue.
Why do we have two?
When should you use one and not the other?
...
So. I'm playing with the routing.rb code in Rails 2.1, and trying to to get it to the point where I can do something useful with the RoutingError exception that is thrown when it can't find the appropriate path.
This is a somewhat tricky problem, because there are some class of URLs which are just plain BAD: the /azenv.php bot attacks,...
I want to get the base 10 logarithm of a Fixnum using Ruby, but found that n.log or n.log10 are not defined. Math::log is defined but uses a different base than 10.
What is the easiest way to get the base 10 logarithm of a Fixnum?
...
How does one reliably determine a file's type? File extension analysis is not acceptable. There must be a rubyesque tool similar to the UNIX file(1) command?
This is regarding MIME or content type, not file system classifications, such as directory, file, or socket.
...
Could any one recommend some good UML/modeling tools for Ruby and Ruby on Rails?
Edit: would be nice to generate code from UML diagrams as well as generate diagrams from existing code.
...
I've been doing c# for a long time, and have never come across an easy way to just new up a hash.
I've recently become acquainted with the ruby syntax of hashes and wonder, does anyone know of a simple way to declare a hash as a literal, without doing all the add calls.
{ "whatever" => {i => 1}; "and then something else" => {j => 2}};
...
I'm trying to use the Optiflag package in my Ruby code and whenever I try to do the necessary require optiflag.rb, my program fails with the standard no such file to load -- optiflag message. I added the directory with that library to my $PATH variable, but it's still not working. Any ideas?
...