24 Ruby tips and tricks

Peter Cooper will share more tips in his book to be released later this year. Stay tune and don’t forget to leave your email address to get updates at http://rubyreloaded.com/trickshots/

Here are some of the tips in the video.

Generate random numbers within a given range

irb(main):019:0> rand(10..20)
=> 12
irb(main):020:0> rand(10...20) # works with exclusive range
=> 16

Dump your object using awesome_print

# Install the gem first
gem install awesome_print

irb(main):001:0> require 'ap'
=> true
irb(main):002:0> ap :a => 1, :b => 'greg', :c => [1,2,3]
    :a => 1,
    :b => "greg",
    :c => [
        [0] 1,
        [1] 2,
        [2] 3
=> {:a=>1, :b=>"greg", :c=>[1, 2, 3]}

Concatenating strings

irb(main):005:0> "abc" + "def"
=> "abcdef"
irb(main):006:0> "abc".concat("def")
=> "abcdef"
irb(main):007:0> x = "abc" "def"
=> "abcdef"

Include modules in a single line

class MyClass
  include Module1, Module2, Module3
  # However, the modules are included in reverse order. Confusing eh!

Instance variable interpolation

irb(main):008:0> @name = "greg"
=> "greg"
irb(main):009:0> "my name is #{@name}"
=> "my name is greg"
irb(main):010:0> "my name is #@name"
=> "my name is greg"

I still prefer the use curly braces.

Syntax checking

➜  ruby -c facu.rb 
facu.rb:12: syntax error, unexpected keyword_end, expecting $end

Zipping arrays

irb(main):027:0> names = %w(fred jess john)
=> ["fred", "jess", "john"]
irb(main):028:0> ages = [38, 47,91]
=> [38, 47, 91]
irb(main):029:0> locations = %w(spain france usa)
=> ["spain", "france", "usa"]
irb(main):030:0> names.zip(ages)
=> [["fred", 38], ["jess", 47], ["john", 91]]
irb(main):031:0> names.zip(ages, locations)
=> [["fred", 38, "spain"], ["jess", 47, "france"], ["john", 91, "usa"]]

Range into arrays

irb(main):034:0> (10..20).to_a  # what I used to do
=> [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
irb(main):035:0> [*10..20]
=> [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Using parameter as default value

irb(main):047:0> def method(a, b=a); "#{a} #{b}"; end
=> nil
irb(main):048:0> method 1
=> "1 1"
irb(main):049:0> method 1, 2
=> "1 2"

Put regex match in a variable

irb(main):058:0> s = "Greg Moreno"
=> "Greg Moreno"
irb(main):059:0> /(?<first>\w+) (?<second>\w+)/ =~ s
=> 0
irb(main):060:0> first
=> "Greg"
irb(main):061:0> second
=> "Moreno"

33 thoughts on “24 Ruby tips and tricks

  1. I all the time used to read piece of writing in news papers but now as I am a user of internet so from now I am
    using net for content, thanks to web.

  2. My partner and I absolutely love your blog and find almost
    all of your post’s to be precisely what I’m looking for.
    Does one offer guest writers to write content for you? I wouldn’t mind writing a post or elaborating on most of the subjects you write in relation to here. Again, awesome site!

  3. I believe that is among the such a lot vital information for me.

    And i’m happy studying your article. However should statement on few general issues, The web site taste is wonderful, the articles is truly nice : D. Excellent process, cheers

  4. I wanted to thank you for this great read!! I certainly
    loved every bit of it. I have got you bookmarked to
    check out new stuff you post…

  5. This is the perfect web site for anyone who would like to
    understand this topic. You understand so much its almost hard to argue with you (not that I
    personally would want to…HaHa). You certainly put a
    new spin on a topic that’s been discussed for years. Wonderful stuff, just wonderful!

  6. I love your blog.. very nice colors & theme.

    Did you create this website yourself or did you hire someone
    to do it for you? Plz reply as I’m looking to construct my own blog and would like to find out where u got this from. thank you

  7. I absolutely love your blog and find almost all of your post’s to be what precisely I’m looking for.
    Does one offer guest writers to write content available for you?

    I wouldn’t mind writing a post or elaborating on a number of the subjects you write in relation to here. Again, awesome weblog!

  8. The physical activity is the premier global non-profit trade association established to lead the growth of women’s MMA, something she sees every day as an instructor for children. Benn’s entourage sabotaged Eubank’s entrance music before their bout by causing it to stop abruptly, but Eubank emerged victorious on the night he fought Antonio Margarito. Exact dates for the debut of physical activity on broadcast television. The state Of Hawaii claims that this is the important part. All ways help build your overall endurance, sprint will give you a better fighter. 5 The race comprised of a 1.

  9. Hmm it seems like your blog ate my first comment (it was super long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly
    enjoying your blog. I too am an aspiring blog writer but I’m still new to everything. Do you have any tips for first-time blog writers? I’d genuinely appreciate it.

  10. Does your blog have a contact page? I’m having problems locating it but, I’d like to shoot you
    an e-mail. I’ve got some suggestions for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it expand over time.

  11. All the basics will usually be covered as well as some progressive skills according to the type of course along with the skill
    level of the operator. The good news is that training
    is supported by the employment agencies,
    and many employers also conduct forklift training on-site.
    For instance, incorporating 3- 4 tonnes of attachments to the class 1 carriages simply overburdens the trucks and turn out to be rather disadvantageous than being of any benefits.

  12. Have you ever considered creating an ebnook or guest authoring on other sites?

    I havge a blog based on the same ideas you discuss and would love
    to have you share some stories/information.
    I know mmy viewers would value ylur work. If you’re even remotely interested,
    feel free to shoot me an e mail.

  13. Humans want the things they can’t easily have and people hate to get rejected
    specially good looking girls who aren’t used to facing rejection.
    For some reason it seems to be a common misconception that taxicab drivers will
    sleep with their clientele when they get the chance. This is no doubt one of the best ways to
    get a girl attracted to you.

  14. Howdy, I do believe your web site could possibly be having web browser compatibility issues.
    Whenever I take a look at your site in Safari, it looks fine but when
    opening in Internet Explorer, it has some overlapping issues.
    I simply wanted to provide you with a quick heads up!
    Other than that, wonderful blog!

  15. Hello, i think that i noticed you visited my web site thus i came to go back the desire?.I’m attempting to in finding things to enhance my web site!I guess its good enough to use some of your ideas!!

  16. Thank you a lot for sharing this with all people you actually recognise what you are talking approximately!
    Bookmarked. Please also consult with my website =). We will have a link alternate contract between us

  17. Hey there! Quick question that’s completely off
    topic. Do you know how to make your site mobile friendly?
    My weblog looks weird when browsing from my apple iphone.
    I’m trying to find a theme or plugin that
    might be able to resolve this issue. If you have any suggestions, please share.
    Thank you!

  18. It is not my first time to go to see this website, i am visiting this web page dailly and obtain nice facts from here daily.

  19. The relation between two countries is not cordial but
    that air of antagonism has never stopped or forced any business from withdrawing any of
    their business initiatives. *Run or move the pearl gently along the sharp edge of front teeth.
    With this mobile phone you will be able to program it to recognize your own handwriting alone.

    Aw, this was a really good post. Spending some time and actual effort to
    produce a really good article… but what can I say… I procrastinate a lot and don’t manage to get nearly anything

  20. Superb website you have here but I waas curious if you knew
    of aany message boards that cover the same topics talked about here?
    I’d really like to be a part of group where I caan get advice from other
    experiuenced peoplpe that sare the same interest.
    If youu have anny suggestions, please let me know. Bless you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s