Ruby 2.4 changes overview
Unified Fixnum and Bignum into Integer It’s backward compatible too. $ irb irb(main):001:0> RUBY_VERSION => "2.3.1" irb(main):002:0> (2**100).class => Bignum irb(main):003:0> 2.class => Fixnum irb(main):004:0> Fixnum => Fixnum irb(main):005:0> Bignum => Bignum irb(main):006:0> Bogonum NameError: uninitialized constant Bogonum from (irb):6 from /home/spotter/.nix-profile/bin/irb:11:in `<main>' irb(main):007:0> quit $ …
Ruby Idioms, Part 7: Last argument options Hash
This idiom I have seen a little more in Rails than I have seen in Ruby, but I am putting it in this Ruby Idioms series anyway. First off, most of you will know by know that you can "transparently" provide a method in Ruby a Hash. What do I mean by "transparently"? Well have a look at the code example below: user = User.find(:first, :include => :preferences) The last argument is actually a Hash even though you do not see the curly braces at all.
Ruby Idioms, Part 6: Postfix conditional flow
Tonight I remembered couple more idioms in Ruby and felt compelled to share. In Javafied Ruby code (below) we often see something like the following: if something for role in user.roles return true if @@roles.include? role end end Now when I first started writing Ruby code almost 3 years ago, I thought looping through a collection like above was the nicest way. I quickly found Ruby like the following snippet in code I inherited:
Ruby Idioms, Part 5: Ranges
Some of you may say this isn't strictly an idiom, but it is dependent on Ruby's core API and classes, so I have included it. Ranges are a very nice low-level abstraction and can save Ruby developers a lot of time for certain coding needs. In a previous idiom example on this blog I had a list of positive odd numbers under 7. Well typing this out by hand is hardly consuming so I typed [1, 3, 5] by hand.
Ruby Idioms, Part 4: The splat
To splat or not to splat, that is the question. In Java to unpack an array's values into separate variables you would need to do something like the following: burgers = [:hamburger, :bocaburger, :gardenburger, :turkeyburger] t = burgers[0] u = burgers[1] v = burgers[2] w = burgers[3] In the Ruby mindset this would look more like the following: burgers = [:hamburger, :bocaburger, :gardenburger, :turkeyburger] t, u, v = *burgers # t=>:hamburger, u=> :bocaburger, …
Ruby Idioms, Part 3: Nested attribute retrieval
Now, probably the biggest difference after the syntax, and the non-static nature of Ruby for Java heads to get used to is that Ruby is an "expressionist" language. Meaning almost all statements in Ruby evaluates to a value, i.e. everything that reasonably can be is an expression in Ruby is. This is definitely not the way Java thinks or works. So this will take some adjusting to. Don't worry I will show you an example.
Ruby Idioms, Part 2: Unless
Now the "idiom" we will use is not specific to Ruby, since I am pretty sure Perl has one of the constructs we will use to solve the stated problem, but it is still idiomatic, since the majority of popular OO languages (static, dynamic or otherwise) do not have it (as far as I know). Also some consider this to be a sub-optimal idiom, because there is a terser way to do something like this.
Ruby Idioms, Part 1: Array#include?
With more people moving to Ruby everyday from languages like Java, I wanted to create blog entries on the most common Ruby idioms (especially ones frequently found in Rails core or Rails application code) and briefly explain how to convert your Java mindset to Ruby (over time). This is the first installment. First let us inspect what we might do in Java (converted to Ruby syntax) when determining if a value passed in equals various fixed values:
Rubyisms: reopening classes
Ever wished you could write code in a very expressive way like the following examples? expiration_date = 3.years.from_now birth_date = 32.years.ago Now you can, in Ruby at least. Two years ago I was a very happy Python developer that thought no other language could compete with Python for developer productivity. My Python indoctrination occurred after 7 solid years of proclaiming Java was the ultimate language that would reign supreme. Prior to my Java phase I was a C++ and …
Rubyisms: naming conventions
Unveil the world of Ruby naming conventions with a captivating symphony of clarity and productivity. Discover implicit and explicit rules as we explore class and module names, variable names, question-marked methods, slightly dangerous methods, instance variables, global variables, and constants. Each convention is infused with examples from the Ruby Standard Library (RSL), highlighting the harmony between readability and developer efficiency. Embrace the expressive nature of …
Rubyisms: forwardables
Recently some Java friends of mine have decided to taste the juicier fruits in Ruby-land with my assistance. So below are some excerpts from an email conversation I had with one about Ruby's standard library forwardable features. Nested object path decoupling Suppose we have the following model classes defined for a simple CRM system: Customer, Address, PhoneNumber, Name, etc. Now in Java-land we would have written something that looks like the following Ruby code (except …