Comments on: 15 Weird Things About Ruby That You Should Know http://www.blackbytes.info/2017/02/weird-ruby/ Ruby Programming Tutorials Mon, 27 Feb 2017 17:28:12 +0000 hourly 1 https://wordpress.org/?v=4.6.4 By: Jesus Castello http://www.blackbytes.info/2017/02/weird-ruby/#comment-627 Mon, 27 Feb 2017 17:28:12 +0000 http://www.blackbytes.info/?p=4604#comment-627 Thanks for your contribution Michael 🙂

]]>
By: Michael Kohl http://www.blackbytes.info/2017/02/weird-ruby/#comment-626 Mon, 27 Feb 2017 13:46:07 +0000 http://www.blackbytes.info/?p=4604#comment-626 Re 1: Way back in the day people used DATA sections (everything after __END__) at the end of Ruby scripts for storing data. Here’s a blog post on this: http://blog.honeybadger.io/data-and-end-in-ruby/

Re 2: This works because :: is used for scope resolution in Ruby: https://en.wikipedia.org/wiki/Scope_resolution_operator#Ruby

Re 6: The special variables become a lot less magic if you require 'English: https://ruby-doc.org/stdlib-2.3.3/libdoc/English/rdoc/English.html

Re 8: I mainly use them in code golf 😉 Or when I want to be really explicit about expecting a one character string.

Re 13 and 14: both the BEGIN and END block as well as the flip-flop operator are remnants of Ruby’s Perl legacy. Though it’s been a long time since I actually saw them in code.

Now for some of my personal favorites:

*) Inheritance: the right-hand side of < can be any valid Ruby expression as long as it resolves to a class.

class IdentityCrisis < [Array, Hash].sample ; end

*) Unary minus: to define a unary minus operation, you have to use -@.

Also if you wanna confuse your friends, make them guess what this does: (1..5).map(&:-@) 😉

Infinity:

Before Float::INFINITY was added in 1.9 we used to make our own: Inf = 1 / 0.0 #=> Infinity

Destructuring addendum: fairly obvious, but restructuring works for assignments as well:

]]>
By: Jesus Castello http://www.blackbytes.info/2017/02/weird-ruby/#comment-616 Sat, 25 Feb 2017 20:15:24 +0000 http://www.blackbytes.info/?p=4604#comment-616 Sorry I made a mistake when copying the code from my notes. Should be working now 🙂

]]>
By: Luis Flores http://www.blackbytes.info/2017/02/weird-ruby/#comment-615 Sat, 25 Feb 2017 19:48:22 +0000 http://www.blackbytes.info/?p=4604#comment-615 Number 5 does not work as you mention, it asigns 1 to ‘first’ and empty to ‘last’ and then 2 to ‘memo’ at least in my Ruby 2.3.0 on Mac

]]>
By: Jesus Castello http://www.blackbytes.info/2017/02/weird-ruby/#comment-613 Fri, 24 Feb 2017 22:44:31 +0000 http://www.blackbytes.info/?p=4604#comment-613 Thanks for your comments Josh! Very interesting additions 🙂

]]>