Ruby 1.9.2 and Rails 3 cannot open rails console

Apparently ubuntu and ruby don’t always catch dependencies like they should. From the first google hit (yeah, I clicked on this stack-overflow in place #2 before checking out the first result.) Navigate to the Ruby source and enter: sudo apt-get install libreadline5-dev cd ext/readline ruby extconf.rb make sudo make install So, if you’re on another … Read more

Is Sinatra multi threaded?

tl;dr Sinatra works well with Threads, but you will probably have to use a different web server. Sinatra itself does not impose any concurrency model, it does not even handle concurrency. This is done by the Rack handler (web server), like Thin, WEBrick or Passenger. Sinatra itself is thread-safe, meaning that if your Rack handler … Read more

Rails Object Relationships and JSON Rendering

By default you’ll only get the JSON that represents modelb in your example above. But, you can tell Rails to include the other related objects as well: def export @export_data = ModelA.find(params[:id]) respond_to do |format| format.html format.json { render :json => @export_data.to_json(:include => :modelb) } end end You can even tell it to exclude certain … Read more

How to do static content in Rails?

For Rails6, Rails5 and Rails4 you can do the following: Put the line below at the end of your routes.rb get ‘:action’ => ‘static#:action’ Then requests to root/welcome, will render the /app/views/static/welcome.html.erb. Don’t forget to create a ‘static’ controller, even though you don’t have to put anything in there. Limitation: If somebody tries to access … Read more

How to install therubyracer gem on 10.10 Yosemite?

gem uninstall libv8 brew install v8 gem install therubyracer gem install libv8 -v ‘3.16.14.3’ — –with-system-v8 this is the only way it worked for me on 10.10 (ruby 2.1.2) Or try gem install libv8 -v ‘XX.XX.XX’ — –with-system-v8 adding the version of the gem 🙂 UPDATE for Mac OS Catalina: brew tap homebrew/versions brew install … Read more

Difference between $stdout and STDOUT in Ruby

$stdout is a global variable that represents the current standard output. STDOUT is a constant representing standard output and is typically the default value of $stdout. With STDOUT being a constant, you shouldn’t re-define it, however, you can re-define $stdout without errors/warnings (re-defining STDOUT will raise a warning). for example, you can do: $stdout = … Read more

How do I compare two hashes?

You can compare hashes directly for equality: hash1 = {‘a’ => 1, ‘b’ => 2} hash2 = {‘a’ => 1, ‘b’ => 2} hash3 = {‘a’ => 1, ‘b’ => 2, ‘c’ => 3} hash1 == hash2 # => true hash1 == hash3 # => false hash1.to_a == hash2.to_a # => true hash1.to_a == hash3.to_a … Read more