Are strings mutable in Ruby?

Yes, << mutates the same object, and + creates a new one. Demonstration: irb(main):011:0> str = “hello” => “hello” irb(main):012:0> str.object_id => 22269036 irb(main):013:0> str << ” world” => “hello world” irb(main):014:0> str.object_id => 22269036 irb(main):015:0> str = str + ” world” => “hello world world” irb(main):016:0> str.object_id => 21462360 irb(main):017:0>

Using layouts in HAML files independently of Rails

You’re mixing up two distinct Rails feature: partials (using render) and layouts (using yield). You can add a rails-like version of either (or both) of them to a Haml only program. Partials In a rails view, you can use render :partial_name to cause the file _partial_name.html.haml to be rendered at that point in the containing … Read more

Running another ruby script from a ruby script

require “b.rb” will execute the contents of b.rb (you call leave off the “.rb”, and there is a search path). In your case, you would probably do something like: a.rb: require “b.rb”; b(“Hello”, “world”) b.rb: def b(first, second) puts first + “, ” + second end Note that if you use require, Ruby will only … Read more

Why is ruby so much slower on windows?

I would guess there are a few possible options, and they probably all add up: Ruby being mainly developed on Linux, it ends up mechanically optimised for it. The code is regularly tested for Windows and everything works, but the result is still that developer will spend more time optimising for Linux than Windows. To … Read more

Include jekyll / liquid template data in a YAML variable?

Today I ran into a similar problem. As a solution I created the following simple Jekyll filter-plugin which allows to expand nested liquid-templates in (e.g. liquid-variables in the YAML front matter): module Jekyll module LiquifyFilter def liquify(input) Liquid::Template.parse(input).render(@context) end end end Liquid::Template.register_filter(Jekyll::LiquifyFilter) Filters can be added to a Jekyll site by placing them in the … Read more

Why do two strings separated by space concatenate in Ruby?

In C and C++, string literals next to each other are concatenated. As these languages influenced Ruby, I’d guess it inherits from there. And it is documented in Ruby now: see this answer and this page in the Ruby repo which states: Adjacent string literals are automatically concatenated by the interpreter: “con” “cat” “en” “at” … Read more

Rails.cache error in Rails 3.1 – TypeError: can’t dump hash with default proc

This might be a little verbose but I had to spend some time with the Rails source code to learn how the caching internals work. Writing things down aids my understanding and I figure that sharing some notes on how things work can’t hurt. Skip to the end if you’re in a hurry. Why It … Read more

Rspec: expect vs expect with block – what’s the difference?

As has been mentioned: expect(4).to eq(4) This is specifically testing the value that you’ve sent in as the parameter to the method. When you’re trying to test for raised errors when you do the same thing: expect(raise “fail!”).to raise_error Your argument is evaluated immediately and that exception will be thrown and your test will blow … Read more

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

tech