{|_, E| E.Length>1} What Is the Use of Underscore ( _ ) in Ruby

How can I turn underscore into spaces in printing CSV in Ruby on Rails?

Try with String#split and String#capitalize

> attributes.map{|e| e.split("_").map(&:capitalize).join(' ')}
#=> ["Employee Name", "Location Assignment", "Job Grade Position", "From Institution", "From Cluster", "From Region", "From Area", "From Unit", "To Institution", "To Cluster", "To Region", "To Area", "To Unit", "Status", "Initiated By", "Start Date", "Approved By", "Applied At"]

Note: String#humanize will split words but first word's only first letter capitalize not other word's. For eg:

> "employee_name".humanize
#=> "Employee name"

But as per your eg: you want last_name to "Last Name".

> "last_name".split("_").map(&:capitalize).join(' ')
#=> "Last Name"

Why Array.new(10) { |e| e = e + 2 } produce [0 2 4 6 8 10 12 14 16 18]?

This can be written as:

Array.new(10) { |index| index * 2 } #=> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Which matches the final form in the documentation to which you linked:

new(size) {|index| block }

Where an array of the given size (10) is created. Each element in this array is created by passing the element’s index to the given block and storing the return value.

The value you assign to e in your example is discarded after each element is initialised.

ruby: how to find non-unique elements in array and print each with number of occurrences?

puts a.uniq.
map { | e | [a.count(e), e] }.
select { | c, _ | c > 1 }.
sort.reverse.
map { | c, e | "#{e}:#{c}" }

What does the underscore mean in literal numbers?

Underscores are ignored. You can put them in to make them more readable.

Ruby count adjacent duplicate elements in array

%w[a b c a a b b c c c].chunk{|e| e}.map{|_, v| v.length}.max #=> 3

How to group Ruby enumerable/array by more than one field?

Hash.new{|h, k| h[k] = Hash.new{|h, k| h[k] = 0}}
.tap{|h| data.each{|e| h[e.action][e.date] += 1}}

result h is:

{
"action1" => {"1/8/2014" => 3, "8/8/2014" => 1},
"action2" => {"1/8/2014" => 1, "2/8/2014" => 2}
}

or,

data.each_with_object(Hash.new{|h, k| h[k] = Hash.new{|h, k| h[k] = 0}}) do
|e, h| h[e.action][e.date] += 1
end

Specifying minimum length when using FFaker::Internet.user_name

From what I see you try to use FFaker in your factory. Why overcomplicate things for your specs, when you could define sequence

sequence(:username) do |n|
"username-#{n}"
end

But the question is valid and you may have some legitimate needs to use ffaker, and there are many ways to do it. You can just concatenate username twice, why not?

username { FFaker.username + FFaker.username }

Or keep looking for a username that length is of minimal lenght:

username do
do
name = FFaker.username
while name.length < 5
name
end

Or monkeypatch ffaker and implement it yourself https://github.com/ffaker/ffaker/blob/0578f7fd31c9b485e9c6fa15f25b3eca724790fe/lib/ffaker/internet.rb#L43 + https://github.com/ffaker/ffaker/blob/0578f7fd31c9b485e9c6fa15f25b3eca724790fe/lib/ffaker/name.rb#L75

for example

class FFaker
def long_username(min_length = 5)
fetch_sample(FIRST_NAMES.select {|name| name.length >= min_lenght })
end
end

Use underscore as variable in Parallel Assignment

In parallel assignment, we sometimes do two things :

  • ignore one element ( taking care of _)

You can reuse an underscore to represent any element you don’t care about:

a, _, b, _, c = [1, 2, 3, 4, 5]

a # => 1
b # => 3
c # => 5
  • ignore multiple elements ( taking care of * )

To ignore multiple elements, use a single asterisk — I’m going to call it a ‘naked splat’ for no better reason than that it sounds a bit amusing:

a, *, b = [1, 2, 3, 4, 5]

a # => 1
b # => 5

Read this blog post Destructuring assignment in Ruby to know more other related things.



Related Topics



Leave a reply



Submit