post

Ruby conditions
Ruby conditions

Often the conditions of an if statement are dependent on more than one thing, and sometimes it’s of the form, “if A is true and B is false”.

Avoiding the details, let’s say I wanted to do something like this:

user.cache! if user.needs_cached? && !API.local?

Essentially that’s saying “cache the user if the user needs cached (A is true) and the API is NOT a local request (B is false). The && ! is fine and all, and it’s actually quite readable to most programmers, but were you aware that you could do this?

user.cache! if user.needs_cached? unless API.local?

Maybe I’m flying solo on this one, but I wasn’t aware that you could use if and unless to chain conditions together. And I’m sure this is one of those Ruby moments where I just need to take a step back and find the right perspective. Sometimes things aren’t always what they seem (like how 1+3 is actually 1.+(3)).

Comments
01
Lucas Efe
06 May 2008 11:38 AM

You blew my mind. I didn`t know abut that.

Question: And how is the resolution of thath condition? First the “user.needs_cached?” and then ” API.local?” or the other way around?

Probably the first API.local?

Anyway, you enlightenme. thanks

Lucas

02
06 May 2008 12:13 PM

I’m not sure how it actually resolves. My guess (and it’s really just a guess) is that it starts at the outer-most condition, working it’s way back to the actual statement to execute, so it’s essentially doing:

unless API.local?
  if user.needs_cached?
    user.cache!
  end
end

Either way, it sort of toys with your mind when you see it inline. I suppose I could download ParseTree to figure it out (maybe?), but I’ll probably just sit back in awe.

03
06 May 2008 04:45 PM

You are right about the calling order, Ryan. It works outside in. You can test this pretty easily at the command line.

def called_if?
  puts 'called_if'
  true
end

def called_unless?
  puts 'called_unless'
  false
end

x = 1 if called_if? unless called_unless?

called_unless
called_if
=> 1
04
06 May 2008 09:49 PM

Ah, yes. Good point.

05
09 May 2008 02:11 AM

The unless keyword? You mean like the one Perl and probably other languages have had for more than 18 years? That one?

RUBY FANBOI LEARNS NEW KEYWORD: BLAHGS ABOUT IT: MORE AT 11

06
09 May 2008 09:52 AM

ANONYMOUS COWARD READS POST AND HAS NO IDEA WHAT IT’S ABOUT: MORE RIGHT NOW

Obviously this wasn’t about discovering the unless keyword (which has been with Ruby since the early days, 1993 I believe), but I guess I can’t expect everyone to understand that. It was about using if and unless to handle two opposite conditions, chained together against the same statement, all inline, and reads like English.

Can Perl do that???

And anyway, even if it was solely about discovering the unless keyword, what was the point of that comment? To make you look knowledgeable at the expense of someone else? If so, that sort of backfired.

Maybe next time you should keep your comments to yourself, UNLESS you know what you’re talking about.

07
09 May 2008 12:11 PM

Whoa, you really showed this anonymous coward who’s boss around these parts of the intersphere. I just hope you post as interesting of a post when you find out about for loops.

Multiple post statement conditions: Ruby. Being able to run a site bigger than or as big as Twitter without falling over: Perl, PHP, Java, ASP.Net, C, Python

08
09 May 2008 02:09 PM

Must be interesting enough for you to have my feed(s), huh? Or do you just like to visit several times over a few hours? Either way, I’m surprised, considering how “boring” this site is for you. Maybe I could check with you before I post something from now on, so that I know it’s worthy. After all, I am just a leet haxor.

Bottom line: I don’t claim to be brilliant or a great programmer or any of the other things that you have somehow taken offense to. So what’s the point you’re trying to make, here? You think ASP.Net is better than Ruby? You’d rather code in C? Great for you!

I program in Ruby because I choose to. I actually happen to like it. I’m sorry to see that it makes you upset. And since I don’t work on sites as big as Twitter, I guess I’m safe from my applications falling over.

09
09 May 2008 08:03 PM

Oh don’t flatter yourself, I don’t “follow your feeds” or check “your site”. In between other things, I checked * my comments *, because Ruby fanbois amuse me.

YHBT, HAND. =D

10
Jay
10 May 2008 12:09 AM

That guy is a tool. I don’t understand people who get aroused by leaving meaningless, retarded comments. YHBT??? Did you get that from your BFF???

It seems that Ruby’s increasing popularity makes those who don’t understand it compelled to challenge, when in reality, it doesn’t matter at all. It’s very strange to me.

And to the “leet haxor”, we’re all glad Ruby “fanbois” amuse you. Your immaturity has amused us, so thanks for that. However, it’s time to let it go now. =D

11
10 May 2008 12:20 AM

Someone not realizing that YHBT means

YOU HAVE BEEN TROLLED

is even more fun than all of my previous comments here.

Please learn2internet at your next convenience.

12
12 May 2008 06:54 AM

I had some ‘troll’ comment me once. So I banned his IP. Just leave this invalid to practice his abbreviations on YouTube.

Anyway, thats a pretty nifty thing you’ve found. I remember the unless statement from that console you showed me a while back.

Incidientally, since I’ll probably have a look into Rails soon when I leave my job, know any good places to start mate? Tutorials, books, etc.

13
12 May 2008 08:10 AM

Well, while this may or may not be the best reference around, it’s the one I seem to use the most when I need to lookup something in the documentation: Noobkit.

Here’s a good Ruby on Rails Link Library that I came across a few days ago.

Of course, here’s the Pickaxe (book on Ruby, not Rails). And here’s the online version (it’s not updated, but still a good read for beginners).

After reading the Pickaxe, here’s the AWDWR book (third edition)—a must read for any Rails developer.

Here’s the RailsForum for when you’re stuck on something and ready to pull your hair out.

If you’re going to get into Rails, read a Ruby book first! I cannot stress that enough. Learning Ruby via Rails is not the way to go, as you’ll quickly become confused as to what is Ruby and what is Rails. Plus, not knowing Ruby will dramatically impact how good your Rails code is. Rails is awesome, but blindly depending on all that it does is, in my opinion, is wrong. It’s better to understand what it’s doing, and know how to work around it when things aren’t exactly what you need.

And of course, I’d be happy to help with any initial questions you might have. You can contact me via this site or via email. Hope that helps!

14
13 May 2008 04:54 AM

Cheers mate, thats exactly what I needed, and I’ll be sure to contact you with how it’s going once I get started :)




Please rewrite the image text in the SPAM field: Spam Protection

Preview

2008 by Ryan Heath | Get In Touch

flickr

DesolateInfinityLooking upDazedBlurred