Ruby for Perl Programmers
G. Wade Johnson
My Ruby Experience
Dabbled with Ruby a couple of times.
Hired to be a Ruby programmer almost 2.5 years ago.
notes
Differences in Two Acts
- Language/Syntax Differences
- Culture/Mindset Differences
notes
Everything is an Object
- Even integers:
- Even strings:
"WADE".downcase
"hello".include? "lo"
- Even
nil
notes
Simple/Obvious Differences
- Methods can end in
?
, !
, or =
- Either
false
or nil
is falsey.
- Variables outside a function are not visible inside.
- Any capitalized identifier is a constant.
- Atoms start with
:
and are collapsed into one instance.
notes
Structure
Use begin
/end
or do
/end
instead of
{
/}
.
Most of the time.
notes
Classes
- Defined with a
class
/end
block
- Constructor is named
initialize
- Instance variables are spelled
@variable
- Define getter/setter methods with
attr_accessor
, attr_reader
, attr_writer
- Methods are defined using
def
- Classes are open, or subject to modification
notes
Modules
- Group of methods or namespace for modules and classes
- Including a module adds its methods to the class as instance methods
- Extending a module adds its methods to the class as class methods
notes
Class Example
Open example.rb
notes
Blocks are Major Feature
- Lots of methods use blocks
- Like a closure
- Specify arguments as part of declaration
- Similar to
map
or sort
in Perl
- Used to wrap functionality to be applied by the function
- Sometimes just for the sake of using a block
notes
Containers
Rich set of methods for dealing with each kind of container.
- arrays
+
, -
, []
, []=
,
any?
, bsearch
, collect
,
collect!
, count
, each
, first
,
grep
, grep_v
,
include?
, last
, map
, etc.
- hashes
[]
, []=
, any?
, default
,
dig
, each_key
, each_pair
, each_value
,
empty?
, has_key?
, has_value?
, etc.
- enumerables
all?
, any?
, chunk
, count
,
each_entry
, each_with_index
, find
,
include?
, member?
, etc.
notes
Example Code
Open blocks.rb
notes
Metaprogramming
- Used a lot by libraries/frameworks
- Used to create DSLs
- Used to do some impressive magic (think
Devel::Declare
)
- Beginning to see some push-back
notes
Gems
- Their modules are called gems.
- Major tools are
gem
and bundle
- bundle supports installing multiple versions of a gem on the same system
notes
Building Projects
rake
instead of make
- Rakefiles contain Ruby code (sometimes in the
rake
DSL)
notes
Culture/Mindset
Optimize for programmer happiness. is one of their mottos.
I don't find myself particularly happier working in Ruby.
- Some playful methods/names.
- Less ceremony than some languages.
- Power tools
notes
Ruby on Rails
Most seem to program in Ruby on Rails not in Ruby.
They also don't seem to recognize that fact.
Rails (libraries) extend Ruby in a lot of ways; often by monkey-patching standard classes.
notes
Speed
They realize that Ruby is not the fastest runtime around and are mostly okay with it.
notes
Neophiles
Many in the Ruby community seem attracted to new stuff.
- New JavaScript libraries.
- New JavaScript transpiler languages.
- Elixir
notes
Reminds Me of Perl From a Few Years Ago
- Ruby programmers sometimes act like they have invented tested code.
- Some gems are almost completely undocumented.
- Ruby programmers talk quite a bit about meta-programming and open classes.
- Lots of excitement for clever solutions.
notes
Two main figures in the community.
- Matz (Yukihiro Matsumoto) - Ruby
- DHH (David Heinemeier Hansson) - Rails
notes
Comparison to Perl
In many ways, they look down on Perl, while realizing that Ruby was influenced by Perl.
Mostly not as hostile as the Java community.
notes
Some Really Bright People
- Doing language/design/community work with Ruby.
- Pushing good design principles that are new-ish.
- Some (re)discovery of ideas that are already in other languages.
- Some discovery of ideas that have been around for years.
notes
Best Practices?
- Small methods
- Small classes
- Seem to avoid local variables
notes
Convention over Configuration
- Strong Rails influence
- Works pretty well, except when it doesn't
- They are willing to override the preference when necessary
- Can be a problem when the convention changes.
notes