Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Understanding Ruby - Enumerable - Coercion
Brandon Weaver
Brandon Weaver

Posted on

     

Understanding Ruby - Enumerable - Coercion

Introduction

Enumerable. Debatably one of, if not the, most powerful features in Ruby. As a majority of your time in programming is dealing with collections of items it's no surprise how frequently you'll see it used.

Difficulty

Foundational

Some knowledge required of functions in Ruby. This post focuses on foundational and fundamental knowledge for Ruby programmers.

Prerequisite Reading:

Enumerable

Enumerable is an interface module that contains several methods for working with collections. Many Ruby classes implement theEnumerable interface that look like collections. Chances are if it has aneach method it supportsEnumerable, and because of that it's quite ubiquitous in Ruby.

Note: This idea was partially inspired byLamar Burdette's recent work on Ruby documentation, but takes its own direction.

Coercion

Perhaps you don't even want anEnumerable at all, there are even methods for changing it over to another type, or methods to coerce them rather.

#to_a /#entries

to_a will coerce a collection into anArray. This isn't very useful for anArray, sure, but remember that other collection types haveEnumerable likeHash andSet:

require'set'Set[1,2,3].to_a# => [1, 2, 3]{a:1,b:2,c:3}.to_a# => [[:a, 1], [:b, 2], [:c, 3]]
Enter fullscreen modeExit fullscreen mode

Its use is very much "what's on the box". If you need something to be anArray useto_a

#to_h

to_h is very similar, it coerces a collection to aHash:

[[:a,1],[:b,2],[:c,3]].to_h# => [[:a, 1], [:b, 2], [:c, 3]]
Enter fullscreen modeExit fullscreen mode

One interesting thing you can do is to use something likeeach_with methods to do nifty things:

%i(a b c).each_with_index.to_h# => {:a=>0, :b=>1, :c=>2}%i(a b c).each_with_object(:default).to_h# => {:a=>:default, :b=>:default, :c=>:default}
Enter fullscreen modeExit fullscreen mode

While that alone is interesting, there's a pattern you may recognize from Ruby in days past:

%i(a b c).map{|v|[v,0]}.to_h# => {:a=>0, :b=>0, :c=>0}# Remember, `map` returns an `Array`{a:1,b:2,c:3}.map{|k,v|[k,v+1]}.to_h# => {:a=>2, :b=>3, :c=>4}
Enter fullscreen modeExit fullscreen mode

Guess what elseto_h does? It takes a Block Function:

%i(a b c).to_h{|v|[v,0]}# => {:a=>0, :b=>0, :c=>0}{a:1,b:2,c:3}.to_h{|k,v|[k,v+1]}# => {:a=>2, :b=>3, :c=>4}
Enter fullscreen modeExit fullscreen mode

...which is pretty nifty. GrantedHash hastransform_values now as well, which is probably a better idea for that specific transformation, but it gets the point across.

to_h is great, much liketo_a, when you need aHash. It has some more power behind it for getting there, and a lot of neat things you can do with it as a result.

Wrapping Up

With that we're done with our section onEnumerable, and we'll be moving on to new subjects!

Want to keep up to date on what I'm writing and working on?Take a look at my new newsletter: The Lapidary Lemur

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Senior Staff Eng at One Medical. Autistic / ADHD, He / Him. I'm the Lemur guy.
  • Location
    San Francisco, CA
  • Work
    Senior Staff Eng at One Medical
  • Joined

More fromBrandon Weaver

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp