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.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)

Related

Filed Under: Articles

Latest Articles

Experts Find Germany Makes Unsatisfactory Progress on AI Development

A panel of experts should prepare Germany for the use of artificial intelligence. The final report is still pending. But there is already criticism of the results. Düsseldorf, Munich, Berlin It should control machines, steer cars and develop new drugs: Artificial intelligence (AI) is one of the … [Read More...]

LCD Resolution and Scaling

LCDs follow a different set of rules than CRT displays offering advantages in terms of bulk, power consumption and flicker, as well as "perfect" geometry. They have the disadvantage of a much higher price, a poorer viewing angle and less accurate colour performance. While CRTs are … [Read More...]

MultiMediaCard (MMC) – SanDisk and Siemens NAND memory card for phones and smaller devices

Ten years after releasing the CompactFlash card and watching it become very successful, SanDisk had a go at repeating the trick, this time in collaboration with German company Siemens, releasing the MultiMediaCard (MMC). This was designed to take over from CompactFlash in most markets … [Read More...]

The Impact of Modern Technology on Relationships

Technology has changed dating in tremendous ways. It has gradually become more immersed in the modern quest for intimacy. In 1995, only 2% of … [Read More...]

Benefits of Instagram as a Powerful Marketing Tool for B2B Brands

The ever-growing popularity of the social networking app Instagram makes it a popular channel for businesses to launch their services, advertising, … [Read More...]

6 Simple Ways to Improve Security of Windows Computers

Millions of Windows PC users experience some form of cybercrime every year. According to one study, there were 2,953 reported cyberattacks between … [Read More...]

2021 PC Hardware Releases to Bolster Your Gaming

If you are a PC gamer, then chances are you are looking to upgrade your kit over the coming year. However, a lot of money can go into building the … [Read More...]

New Transfer Feature in Dropbox Enable Sharing files with Third Parties

Dropbox has been a popular P2P sharing platform for many years. They don't announce new features as often as other applications, since they have a … [Read More...]

Ransomware Operators Find Data Theft Profitable

How valuable is your data? That’s not a question that organizations or individuals have to ask themselves all that often. You might know the market … [Read More...]

Guides

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

Recent Posts

Windows Backup Installation

Unlike with the Professional Edition of the operating system, the Windows Backup utility is not included in the default installation of Windows XP … [Read More...]

WiFi Access

In the early 2000s, the momentum gained by IEEE 802.11b in the wake of the surge in home networking so as to allow the sharing of broadband Internet … [Read More...]

Hard Disk Compliment

The next step up in capacity, 500MB to 1GB, is enough to back-up a reasonably large disk partition. Most such devices also … [Read More...]

[footer_backtotop]

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