Search code examples
rubydictionarytaskblockjruby

Unable to adjust Ruby block


I am fairly new to Ruby, JRuby, etc...

I started to work on migration of certain bash scripts into Ruby, now I used a block from a different pipeline, but here its unnecessary to use it in this format as there is no need to iterate through array:

 all_cf = %w(
        customers
    ).map do |table_name|
        schema("columns.#{table_name}.hbase.families").gsub(/'/,'')
    end.uniq.map{|s| "{ NAME => '#{s}', VERSIONS => 1 }" }.join(',')

Is there an easier way to replace that array iteration and just replace #{table_name} with customers?

I tried this:

all_cf = task do
     schema("columns.customers.hbase.families").gsub(/'/,'')
   end.uniq.map...

But that just throws error and tried couple of more forms of this, but I think I still don't have a right understanding of the Ruby grammar, as I come from the PHP background I'm still struggling with this, anyone any idea how, and maybe an explanation why?

Cheers...


Solution

  • This produces the result that your original script produced:

    "{ NAME => '#{schema("columns.customers.hbase.families").gsub(/'/,'')}', VERSION => 1 }"
    

    I'm not sure what task is, so I don't know whether you should expect your code to work. uniq is a method on Array which returns an Array with all duplicates removed:

    > [1,2,3,1,2,1].uniq
    # => [1,2,3]
    

    Similarly you can look up what map and join do.