ruby

How to use bound function as Proc in Ruby


I'm new to ruby, and I'm trying to read a simple CSV file. I can do this

File.read("info.csv")
    .split[1..]
    .map {|it|it.split(",")}

But I'd like to know if there's something like this to replace the last line

.map String.split.bind(',')

I've tried to find a .split function in the String class, but perhaps that only exists in class instances.

I've also heard about message passing, so I searched ruby pass message as proc, but I don't know enough nomeclature to get actually usefull results.


Solution

  • You can do something like this

    split_proc = ->(str) { str.split(",") }
    
    File.read("info.csv")[1..] # read file & skip header
        .split
        .map(&split_proc)