Use natural language in your Ruby class and method names.
Like this
space_class("Scorecard for a player").new("Picard").space_method("Calculate the score from a game", game)
or the shorter and fancier
ç("Scorecard for a player").new("Picard").ƒ("Calculate the score from a game", game)
instead of the mechanical and boring
ScorecardForPlayer.new("Picard").calculate_new_score(game)
or the not very informative
Scorecard.new("Picard").score(game)
Naming things is famously one of the hard things in computer science*, but programming languages aren't doing a very good job of helping us write fun and descriptive language.
What if instead of WritingLikeThis
, having_to_read_this
or BEING_SHOUTEN_AT
we could write and read regular language? Maybe it will help us write better programs? Or maybe it won't. But I thought it would be a fun experiment.
This gem requires Ruby 2.1 or newer.
Add this line to your application's Gemfile:
gem 'spaces_are_ok'
And then execute:
$ bundle
Or install it yourself as:
$ gem install spaces_are_ok
Ruby doesn't allow method calls with the keywords class
and def
, so we have to use slightly different syntax when defining and using our space friendly names. But it's not too bad.
require 'spaces_are_ok'
space_class("Greet someone") do
def initialize(name)
@name = name
end
space_method_def "Say hello", def _
"Hello, #{@name}"
end
end
greeter = space_class("Greet someone").new("Kim")
greeter.space_method("Say hello") # => Hello, Kim!
Since it gets rather tedious to write space_
all the time, I have included shorthand versions, so the above can be written like this:
require 'spaces_are_ok'
ç("Greet someone") do
def initialize(name)
@name = name
end
∂ƒ "Say hello", def _
"Hello, #{@name}"
end
end
greeter = ç("Greet someone").new("Kim")
greeter.ƒ("Say hello") # => Hello, Kim!
class LaunchSpaceShip
# Class definition
end
space_class("Launch space ship") do
# Class definition
end
ç("Launch space ship") do
# Class definition
end
LaunchSpaceShip.new
space_class("Launch space ship").new
ç("Launch space ship").new
#####Plain Ruby
module SpaceShipMotor
# Module definition
end
space_module("Space ship motor") do
# Module definition
end
ɱ("Space ship motor") do
# Module definition
end
include SpaceShipMotor
include space_module("Space ship motor")
include ɱ("Space ship motor")
def travel_to_a_planet(planet)
# Method body
end
space_method_def("Travel to a planet"), def _(planet)
# Method body
end
∂ƒ("Travel to a planet"), def_(planet)
# Method body
end
travel_to_a_planet(planet)
space_method("Travel to a planet", planet)
ƒ("Travel to a planet", planet)
If you're the adventurous type and don't want to worry about speling everything correctly all the time, then you don't have have to. Just turn on the dont_worry_about_spelling
-mode.
require 'spaces_are_ok/dont_worry_about_spelling'
space_class("Greet someone") do
def initialize(name)
@name = name
end
space_method_def "Say hello", def _
"Hello, #{@name}"
end
end
greeter = space_class("Greeet someone").new("Kim")
greeter.space_method("Say jello") # => Hello, Kim!
Probably not.
- I haven't been able to figure out a way of doing this for variable names. I would prefer
space_variable("Number of vowels")
overvwl_ctr
. Wouldn't you? - Patch Ruby so we could have this as a feature in the language. It could look something like this:
class “Weather forecast”
def ‘will it rain tomorrow?’
puts "Time will tell"
end
end
“Weather forecast”.new.‘will it rain tomorrow?’ # => Time will tell
- Fork it
- Install the dependencies (
bundle install
) - Create your feature branch (
git checkout -b my-new-feature
) - Make your changes
- Run the tests (
mtest test.rb
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request