Spaces Are Ok
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)
Why
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.
Installation
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
Usage
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!
Summary of functionality
Defining a class
Plain Ruby
class LaunchSpaceShip
# Class definition
end
Spaces Are Ok
space_class("Launch space ship") do
# Class definition
end
Spaces Are Ok shorthand
ç("Launch space ship") do
# Class definition
end
Using a class
Plain Ruby
LaunchSpaceShip.new
Spaces Are Ok
space_class("Launch space ship").new
Spaces Are Ok shorthand
ç("Launch space ship").new
Defining a module
#####Plain Ruby
module SpaceShipMotor
# Module definition
end
Spaces Are Ok
space_module("Space ship motor") do
# Module definition
end
Spaces Are Ok shorthand
ɱ("Space ship motor") do
# Module definition
end
Using a module
Plain Ruby
include SpaceShipMotor
Spaces Are Ok
include space_module("Space ship motor")
Spaces Are Ok shorthand
include ɱ("Space ship motor")
Defining a method
Plain Ruby
def travel_to_a_planet(planet)
# Method body
end
Spaces Are Ok
space_method_def("Travel to a planet"), def _(planet)
# Method body
end
Spaces Are Ok shorthand
∂ƒ("Travel to a planet"), def_(planet)
# Method body
end
Calling a method
Plain Ruby
travel_to_a_planet(planet)
Spaces Are Ok
space_method("Travel to a planet", planet)
Spaces Are Ok shorthand
ƒ("Travel to a planet", planet)
You don't have to care about spelling
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!
But should I use it?
Probably not.
Ideas for improvement
- 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
Contributing
- 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