Ruby FAQs (this title must be over 15 characters!)

(Adré Du Toit) #1

Is there something about Ruby that you know is commonly accepted, but you just don’t know why? Maybe it’s a best-practise that you’ve accepted, but in the back of your mind you know that you don’t know why it’s the best way. Maybe it’s a pattern that everyone uses or a gem which everyone else avoids, but you weren’t copied in on the memo telling you why.

If you’ve got these types of niggling questions then I want to hear from you! I’m putting together a list of FAQs which we’ll then discuss at the West Midlands Ruby User Group (WMRUG).

Add your question to this thread and I’ll compile them. As soon as we’ve got a reasonable number of talking points I’ll compile the list and schedule a talk at an upcoming WMRUG meeting. Remember, this isn’t the place to discuss the questions, that happens later over pizza!

(Daniel Hollands) #2

This post is a Wiki post. Rather than posting a reply, you’re able to edit this post and add your question. Just add a new bullet point to the list below, and include your question and username.

  • Why is default_scope bad? - @LimeBlast

    • messing with the default behaviour can be confusing for other developers
    • code is more descriptive, for example User.deleted vs User.all
  • What’s the difference between a Proc, a Block and a Lambda and when should we use each? - @maxehmookau

    • Proc and block are very similar, imagine the block as like an anonymous function
    • The difference between Proc’s and Lambda’s is the way that return works. If you return in a proc, it will immediately return from the method that calls it. If you return in a lambda, it will only return from the block.
    • Procs also don’t require you to pass all the parameters, i.e. it will default arguments to nil.
    • For the two reasons above it’s almost always best to use lambdas.
    • Additionally, there is a different kind of Proc, the stabby Proc, which works in a similar way to the above but in Ruby 1.9 was the only way to have a proc where you could have default values for the variables.
    • Here’s a presentation @joonty gave at WMRUG in 2013 on the subject:
  • What are the differences between strings and symbols? - @elaptics

    • In <= 2.1 Rubies, the only difference is GC behaviour, Strings are garbage collected, whereas Symbols are not.
  • What are the differences between using single or double-quoted strings? - @elaptics

    • Cannot interpolate in single quotes
    • ALWAYS USE DOUBLES unless you need to use singles
  • Why does everyone use 2-space intending? - @LimeBlast

    • 4 would be ridiculous
  • Why does the styleguide suggest a trailing blank line at the end of a ruby file? - @jsrn

  • What’s wrong with block comments? - @jsrn

    • Nothing, vim is shit
    • It doesn’t look anything like a comment
  • When and when not to use inject (or its aliases) - @elaptics

    • Alias is reduce
    • Use cases are sometimes summing or building a hash
      • [2,3,4,5].inject {|mem, a| mem += a } #=> 14
      • %w(foo foo foo bar baz bar foo bar baz).inject( {|mem, a| mem[a] += 1; mem } #=> {"foo"=>4, "bar"=>3, "baz"=>2}
  • Why use something like Redis for sessions (rather than the built in system)? - @LimeBlast

    • redis over activerecordstore, the main benefit is speed
    • redis over cookiestore, expiring people’s sessions because of an application change

(Adré Du Toit) #3

Nice one, get that ball rolling.

Of course I realise that any people asking questions in this thread already know the answers and are just trying to promote greater understanding the the Ruby community.

(Max Woolf) #4

I won’t be able to make it, but here’s a question.

What’s the difference between a Proc, a Block and a Lambda and when should we use each?

(Andy Henson) #5

How about the differences between strings and symbols and what are the differences between using single or double-quoted strings.

(Daniel Hollands) #6

How about why does everyone use 2-spaces for indenting?

(Daniel Hollands) #7

I’ve converted the second post into a wiki post, and added all the questions which have been asked so far. If you’ve got any more, feel free to add them directly to the second post.

(Andy Henson) #8

I just wanted to note that I’m not sure I’m a fan of this wiki post. Editing it doesn’t raise the visibility of the post so in this case here, it’s too easy for it to be buried so I suspect that some people may not have even seen this thread.

(Daniel Hollands) #9

I had noticed that I was getting notifications in the top bar when people had edited the post (not sure if this is because the post was originally mine, or just because I’m subscribed to the thread), but you make a good point about visibility.

(Andy Henson) #10

I wasn’t getting notifications when it was edited so it must be because it was your post.

(Adré Du Toit) #11

No, I wasn’t either. Chalk that one up to experience.

Thanks everyone for your contributions. I think we have enough there to use for tonight’s meetup, which is great.

It would be nice to make this a recurring thing so I’ll have to think about how we can best do that.

(Daniel Hollands) #12

Good session last night, cheers guys :smile: