Ruby Trick Shots: 24 Ruby Language Tips and Tricks

Peter Cooper of Ruby Inside and Ruby Weekly presents a collection of Ruby tips and tricks of both useful and useless varieties. He will release a larger set in a book later this year – learn more at http://rubyreloaded.com/trickshots

Also check out our Ruby course at http://rubyreloaded.com/

Comments

CooperPress says:

If something “in the syntax of the language” is by definition not a trick, then there would be no tricks to share, since anything possible in Ruby would not be a trick.

Nikolai Ruban says:

Oh, this is awesome, thanx.

Jaspreet Singh says:

Hi , did you make the master class for regex? not a lot of content on that topic

Christos Evaggelou says:

+CooperPress A great video, thanks! Regarding curious concatenation, it seems it’s part of the grammar. Here’s the definition of strings in Bison’s grammar file for version 1.9.3 (parse.y). It seems you can get a concatenated string by placing two strings together separated with whitespace.

string        : tCHAR
        | string1
        | string string1
            {
            /*%%%*/
            $$ = literal_concat($1, $2);
            /*%
            $$ = dispatch2(string_concat, $1, $2);
            %*/
            }
        ;

Shou Ya says:

Cool! I learnt a lot amazing things.

Charles Brown says:

How are you using gems in your irb session, how are you using ‘require ‘? Thanks in advance!

Keith Bennett says:

I put together a playlist with times for this video.  You can go directly to a point in time in the video by appending something like this to the URL:

&t=2m35s

as in:

Ruby Trick Shots: 24 Ruby Language Tips and Tricks

Here’s the playlist:

0:00  Intro
2:35  Random
3:21  Awesome Print
4:29  Curious Concatenation
5:41  Fiddle
7:06  Simple Presence of a Substring
7:33  Spaces as String Delimiters (don’t use this!)
8:02  Multiple Module Inclusion
9:08  Instance Variable Interpolation Shorthand
9:51  Ruby Syntax Checking at the Command Line
10:19 Ruby Syntax Analysis with Ripper
11:42 Chained If’s
11:57 Next as a “Return” for Procs and Blocks
14:20 Zipping Arrays Together
15:39 Exploding Ranges Into Arrays
16:41 JSON Output
17:31 __method__
18:23 Multiline Method Chaining
18:42 _ in irb
19:15 Checking Set Bits in Numbers
20:27 Parameters Defaulting to Other Parameters
20:51 Proc#source_location
22:31 Prepending to a String In Place
23:08 Storing Data in the Source File
24:35 Regex Matching Tips

dennisonicc says:

Tricks start at: 02:34

Rahul Raut says:

Very cool stuff really :)

Sean Humbarger says:

Great job!

Magnus K says:

What happend with the book? Also the link in the description doesn’t work.

Kal9871 says:

Very cool! I had no idea you could use _ to get the last returned value in IRB, that’s awesome. Also, another easy way to check if a string contains a substring is to use the #include? method: “abc”.include?(“b”) # => true

putshellothere says:

Some good tips… Thanks!

Keith Bennett says:

Peter –

Great stuff, thanks!  A couple of opinions…

—-
For the “Next as a “Return” for Procs and Blocks”, IMO in the case you illustrated it would be clearer to use a traditional conditional here, such as:

rand(5) == 0 ? 10 : bar * 2

—-
In __method__, I recommend naming the parameter ‘method_name(s)’ rather than ‘meth(s)’. I see this a lot, but it’s unnecessarily ambiguous when I need to infer from the context whether the object to which it refers is a name or the method itself.

…but I realize that’s subjective, and can see that in some cases your use of next would be better.

Thanks again for putting this together.

Aupajo says:

What editor are you using here?

Peter Cooper says:

@Aupajo Textmate. I use Sublime Text 2 more nowadays but TM gives me an ultra simple non-ugly text editor without any flashiness that’s ideal for teaching situations (I think confusing people with emac and vim keypresses while teaching other stuff is not the way to go :-))

hrmIwonder says:

Good video… zip is very handy, but imo, there’s a huge oversight with it. When you pass it a block, the final expression is nil. Example var = [1,23].zip([4,5,6]){|a,b| {x:a,y:b} }, makes var be nil instead of [{x:1,y:4},{x:2,y:5},{x:3,y:6}]. Easy enough to fix by writing your own zip…

Karim Naufal says:

Top content bro, please share the rest

Vlad Ivanovic says:

Gold as always

Brian Alonso says:

loved it =~

Rand Elder says:

can you make a video on “substring” from a String in Ruby???
Thanks

dasten123 says:

cool stuff!

Elad Ossadon says:

Awesome stuff! Thanks!

Write a comment

*

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.


Do you like our videos?
Do you want to see more like that?

Please click below to support us on Facebook!

Send this to a friend

▷ Other ReviewsVehicles▷ Show Cars▷ Motorbikes▷ Scooters▷ Bicycles▷ Rims & Tires▷ Luxury BoatsFashion▷ Sunglasses▷ Luxury Watches▷ Luxury Purses▷ Jeans Wear▷ High Heels▷ Kinis Swimwear▷ Perfumes▷ Jewellery▷ Cosmetics▷ Shaving Helpers▷ Fashion HatsFooding▷ Chef Club▷ Fooding Helpers▷ Coktails & LiquorsSports▷ Sport Shoes▷ Fitness & Detox▷ Golf Gear▷ Racquets▷ Hiking & Trek Gear▷ Diving Equipment▷ Ski Gear▷ Snowboards▷ Surf Boards▷ Rollers & SkatesEntertainment▷ DIY Guides▷ Zik Instruments▷ Published Books▷ Music Albums▷ Cine Movies▷ Trading Helpers▷ Make Money▷ Fishing Equipment▷ Paintball Supplies▷ Trading Card Games▷ Telescopes▷ Knives▷ VapesHigh Tech▷ Flat Screens▷ Tech Devices▷ Camera Lenses▷ Audio HiFi▷ Printers▷ USB Devices▷ PC Hardware▷ Network Gear▷ Cloud Servers▷ Software Helpers▷ Programmer Helpers▷ Mobile Apps▷ Hearing AidsHome▷ Home Furniture▷ Home Appliances▷ Tools Workshop▷ Beddings▷ Floor Layings▷ Barbecues▷ Aquarium Gear▷ Safe Boxes▷ Office Supplies▷ Security Locks▷ Cleaning ProductsKids▷ Baby Strollers▷ Child Car Seats▷ Remote ControlledTravel▷ Luggages & Bags▷ Airlines Seats▷ Hotel Rooms▷ Fun Trips▷ Cruise Ships▷ Mexico Tours