I think I’m really starting to get Ruby. At least I can read Ruby and tell you what’s happening. I haven’t read a single tutorial on RSpec, the testing framework we use at Chatwoot for the Rails backend. I didn’t want to spend too much time in tutorial hell, and I’m sure that I should read more about it soon. I’ve written about Ruby blocks before, but I think it bears repeating. This is a method call with a block as an input. 1 2 3 perform "input_value" do puts "I'll get called with that function" end Don’t worry about how the function is implemented for now. I’m hand-waving this so that you can notice something. This is also a method call with a block as an input. 1 it("can do something") { puts "The cake is a lie" } But here’s something else that’s a method call with a block as an input. In lib/calculator.rb: 1 2 3 4 5 class Calculator def add(a, b) a + b end end In spec/calculator_spec.rb: 1 2 3 4 5 6 7 8 require_relative "../lib/calculator" RSpec.describe Calculator do it "adds two numbers" do calc = Calculator.new expect(calc.add(2, 3)).to eq(5) end end The highlighted lines include a method call to it with a block. Look at that again. It’s a method call. That is crazy. It is so sublime that I can’t explain how excited this makes me. If I have to teach Ruby to a Pythonista I’d ask them to ensure they see what this is. Until you grok this, it won’t matter how much you try to understand Ruby. Ruby’s readability comes from this feature. I’m still not sold on RSpec though, but I am open to learning it because it is, ultimately, Ruby. Block Magic# Let’s really see what you can do with blocks. Building Your Own Language# 1 2 3 4 5 6 7 3.times do puts "Hello" end 5.times { puts "World" } 10.downto(1) { |i| puts i } These are all method calls on integers. times is a method. downto is a method. And every method takes blocks. But we can also take this to the monke. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Integer def seconds self end def minutes self * 60 end def from_now ...
First seen: 2025-10-18 08:56
Last seen: 2025-10-18 20:58