Ruby * operator before array [duplicate] - ruby

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Understanding ruby splat in ranges and arrays
could anyone tell me what the * does in the following piece of code?
line = "name=yabbi;language=ruby;"
Hash[*line.split(/=|;/)]
Thanks.

* is the splat operator. It is used to split an array into a list of arguments.
line.split(/=|;/) returns an array. To create a Hash, each element of the array must be passed as an individual parameter.

it's a splat operator Read about it. Often times you see it used when you want to split up an array to use as parameters of a function.

Related

What does the `&:symb` do? [duplicate]

This question already has answers here:
What does map(&:name) mean in Ruby?
(17 answers)
Closed 4 years ago.
I've seen an example of how to sort a string. To sort case insensitively:
str.chars.sort(&:casecmp).join
#=> "ginrSt"
I'm curious about (&:casecmp). I found that for example:
arr.map(&:name)
is shorthand for
arr.map(&:name.to_proc)
which is same with
arr.map{|el| el.name}
I know the & (ampersand) tries to convert symbol to proc, and pass it as a block to a method. I do not understand how this would work for sort method, which is supposed to compare two values. Would it be as follows?
str.chars.sort{|a, b| a.casecmp ;b.casecmp}.join
It wouldn't be helpful since soft needs a block to return an integer and casecmp needs an argument. (Or is it called parameter in that case?) To me, it looks more like this:
str.chars.sort{|a, b| a.casecmp(b)}.join
How does &:casecmp know to take one of |a, b| as a caller and the other one as an argument? I wouldn't guess it that it is an option.
If more than one parameter is passed to your block, the proc created by Symbol#to_proc uses the additional block parameters as parameters to the method call.
http://phrogz.net/symbol-to-proc-with-multiple-arguments
So, what's really happening is, sort(&:casecmp) is converted to:
sort {|a,b| a.casecmp(b) }
because sort takes two parameters.

Ruby code what does "&:" do here [duplicate]

This question already has answers here:
What does map(&:name) mean in Ruby?
(17 answers)
Closed 5 years ago.
def reverse_words(s)
s.split.map(&:reverse).join(' ')
end
This code reverses each word in a sentence. But I do not understand "&:" in the code. Can someone explain that to me?
map expects a code block that takes one argument. What you would normally do is to call reverse on that argument:
map {|elt| elt.reverse }
With the & syntax you can shorten this to
map(&:reverse)
The colon is there to make a symbol out of the name reverse.
The & means that reverse is referencing a function, not a block.
This method assumes that the caller will pass it a String object.
First the method #splits the string on whitespaces into an array (if the string has no whitespaces it creates an array with the string as the only element)
Calls the Array#map method on the new array and passes it a reference to the String#reverse method.
The & tells the map method that the input is a reference to a method and not a standard block

How would make this more elegant [duplicate]

This question already has an answer here:
How can I more elegantly remove duplicate items across all elements of a Ruby Array?
(1 answer)
Closed 8 years ago.
I have this piece of code in an application and was told to make it more elegant but have no idea how to make it better
self.join(" ").split(" ").uniq
Any suggestion will be much appreciated.
self is an array
flat_map(&:split).uniq
flat_map runs a block over an array, and concatenates all the resulting arrays.
flat_map(&:split) is equivalent to calling s.split on every argument, which happens to do the exact same thing as s.split(' '), (unless you redefine $;, but please don't do that).
We don't need self, so we omit it.

Bash: Send associative array to the function [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to pass an associative array as argument to a function in Bash?
I declare my hash array:
declare -A some_array
And I declare my function:
some_function() {
..
}
How can I send the array as an argument to the function in order to access it?
I know that I can use it as a global variable, but it's not the way out when I have a lot of hash arrays I want to use with some function.
If there is no way to do it, how can I assign to the one hash array value of other?
Access it as a global variable (simply refer to it by name inside your function). There is no array passing in Bash. There are awkward techniques that try to do this, but I recommend avoiding the mess.
Other options include writing your entire script in a language such as Python or Perl which supports passing arrays, hashes or their references.
In Bash 4.3 or later you can use name references, but there are caveats.

What does a single splat/asterisk in a Ruby argument list mean? [duplicate]

This question already has an answer here:
naked asterisk as parameter in method definition: def f(*)
(1 answer)
Closed 10 years ago.
I was poking through the Rails 3 ActiveRecord source code today and found a method where the entire parameter list was a single asterisk.
def save(*)
I couldn't find a good description of what this does (though I have some ideas based on what I know about splat arguments).
What does it do, and why would you use it?
It means it can have any number of arguments (including zero) and it discards all those arguments.

Resources