pctechguide.com

  • Home
  • Guides
  • Tutorials
  • Articles
  • Reviews
  • Glossary
  • Contact

Mastering the Art of Using Blocks as a Ruby Programmer

Blocks are an important element of the Ruby programming language. You need to learn more about them to be a proficient Ruby programmer. Let’s delve into some more details on them.

THE BLOCKS IN RUBY DEVELOPMENT

In Ruby, handling blocks of code is very interesting. The methods you write can receive a block of code. From there, they do some very interesting things. Before we move on to this from receiving blocks in a method, I would like to give an example of a block:

5.times do |val|

  puts val

end

The block in the example above starts with do and ends with end. Blocks can also be delimited with the curly brackets { }, although we do this usually when the block is a single line:

5.times { |val| puts val }

The most interesting thing about this syntax is that the methods of your objects can receive blocks. You would need to type the following:

def cool_method

 yield

end

cool_method do

  puts “Hello world”

end

Running the above script will result in a “Hello World” screen print. What happened is that when Ruby finds the keyword “yield”, it runs the block of code that was sent to the method. In this case the screen print of “Hello world”. What’s up? This has N cases in which it can be useful to you, infinity.

This was just an example, but there are many more interesting things. You can pass parameters to the block and you can validate that your method received a block.

METAPROGRAMMING

I know that many languages have this characteristic, Ruby is one of them, and it’s something very interesting. We can define meta programming as the ability of a language to modify a program at runtime, which means auto generating code. A much vaguer definition is to say that with a language that supports metaprogramming we can write code that generates code. For example:

[ “user” , “editor” , “admin” ].each_with_index do |user_level,index|

  define_method(“is_#{user_level}?”) do

    return true if user.permission_level == index  

  end

end

The snippet of previous code defines the methods is_user?, is_editor?, is_admin? without us writing them. The method defines_method does just that. It will define a method. That’s why we say it’s code that generates code.

This part of the language usually comes once you’ve learned many other things from Ruby, and of course, it’s not limited to the example I gave earlier, that’s just a part.

CONCLUSION

This is an article with 4 interesting features of a language that was designed to *make programmers happy, seriously. We wanted to give you a better overview of Ruby blocks, so that you can use them to get the most of your programming experience with Ruby.

The last feature I want to mention is that the best thing about Ruby is the community. It’s a big community, willing to help. There are a lot of very good Open Source projects (like Rails). There are a lot of forums with solutions to a lot of doubts and there are a lot of resources to learn how to use Ruby.

If you were interested in the language I invite you to follow the Ruby course of Easy Code or if you do not have problems with English. Follow the RubyMonk exercises where you can learn from the most basic to meta programming, advanced handling of blocks and things like that with exercises in the browser 🙂  You can also try Ruby in TryRuby where you learn the basics of the language in your browser.

Anyway, you can leave your suggestions, doubts and clarifications in the comments section, I hope you liked the post.

Filed Under: Articles

Latest Articles

Top Computer Video Games in 2020

The PC gaming market is maturing, but it is also very robust. The market will be worth $30.71 billion by 2022. It is growing at a pace of 2.2% a year, but the sluggish growth reflects the fact that it is already so large. New gamers are often looking for new games to try. Some people like the … [Read More...]

BitDefender Antivirus Pro 2011 Review

BitDefender Antivirus Pro 2015 Review

Pros : Has a ton of new useful features over previous versions. One of the best antivirus protection systems around. Offers a secure browsing to protect online shopping and banking experiences. Builds a rescue mode boot partition in case system does get compromised Cons : There are less expensive … [Read More...]

How to Use Print Screen in Windows

There are times where you would like to show something on your screen to someone. I use this all the time in my tutorials and you may be wondering how I do it. One of the easiest ways to accomplish this is using the Print Screen feature in Windows 7. This short article will show you how you can save … [Read More...]

20 Cool Creative Commons Photographs About the Future of AI

AI technology is starting to have a huge impact on our lives. The market value for AI is estimated to have been worth $279.22 billion in 2024 and it … [Read More...]

13 Impressive Stats on the Future of AI

AI technology is starting to become much more important in our everyday lives. Many businesses are using it as well. While he has created a lot of … [Read More...]

Graphic Designers on Reddit Share their Views of AI

There are clearly a lot of positive things about AI. However, it is not a good thing for everyone. One of the things that many people are worried … [Read More...]

Redditors Talk About the Impact of AI on Freelance Writers

AI technology has had a huge impact on our lives. A 2023 survey by Pew Research found that 56% of people use AI at least once a day or once a week. … [Read More...]

11 Most Popular Books on Perl Programming

Perl is not the most popular programming language. It has only one million users, compared to 12 million that use Python. However, it has a lot of … [Read More...]

10 Exceptional Books on ChatGPT that Will Blow Your Mind

ChatGPT is a powerful new AI tool that is taking the world by storm. You are going to find a lot of amazing books that will teach you how to make the … [Read More...]

Guides

  • Computer Communications
  • Mobile Computing
  • PC Components
  • PC Data Storage
  • PC Input-Output
  • PC Multimedia
  • Processors (CPUs)

Recent Posts

Best DNS Providers of 2021 – Part 3

Here is the last installment of the best DNS providers of 2021. We hope it helps! DNS.Watch Okay, all that stuff about adding layers of … [Read More...]

USB 2.0 Intefaces

While USB was originally designed to replace legacy serial and parallel connections, notwithstanding the claims that they were complementary … [Read More...]

Win98 Installation Planning

A clean install means that you're going to completely wipe your C: drive early in the procedure. You need to be sure that you've figured out how … [Read More...]

[footer_backtotop]

Copyright © 2025 About | Privacy | Contact Information | Wrtie For Us | Disclaimer | Copyright License | Authors