splat Questions
5
Solved
I am able to define a method like this:
def test(id, *ary, hash_params)
# Do stuff here
end
But this makes the hash_params argument mandatory. These don't work either:
def t(id, *ary, ha...
1
Solved
1
Solved
I was poking through the Rails code today and stumbled upon this snippet:
new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday))
What does the asterisk-double-colon (or splat-doub...
2
Solved
If I run the following code, the first two lines return what I expect. The third, however, returns a binary representation of 2.
2.to_s # => "2"
2.to_s * 2 # => "22"
2.to_s *2 # => "10"
...
Maice asked 1/10, 2012 at 7:51
3
This code seems to create an array with a range from a to z but I don't understand what the * does. Can someone please explain?
[*"a".."z"]
1
Solved
Given
a = [[:a, :b, :c]]
1) I understand this
a.each{|(x, y), z| p z} # => :b
that there are two variables (x, y) and z, so the third element :c is thrown away, and z matches :b. And I und...
Interbrain asked 23/8, 2012 at 14:20
1
Solved
If I have an array (of unknown length until runtime), is there a way to call a function with each element of the array as a separate parameter?
Like so:
foo = @(varargin) sum(cell2mat(varargin));...
Limitation asked 11/1, 2012 at 19:39
1
Solved
I'm trying to understand the difference between *(1..9) and [*1..9]
If I assign them to variables they work the same way
splat1 = *(1..9) # splat1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
splat2 = [*1..9] #...
3
Solved
In Python and Ruby (and others as well, I'm sure). you can prefix an enumerable with * ("splat") to use it as an argument list. For instance, in Python:
>>> def foo(a,b): return a + b
>...
2
Solved
If I have a method like:
def sum *numbers
numbers.inject{|sum, number| sum += number}
end
How would I be able to pass an array as numbers?
ruby-1.9.2-p180 :044 > sum 1,2,3 #=> 6
ruby-1.9...
3
Solved
NOTE: mischa's splat on GitHub has lots of cool interactive examples of * in action.
By googling, I found that one way to iterate over a range of numbers in Ruby (your classic C-style for loop)
f...
2
Solved
given e.g:
scala> def pipes(strings:String*) = strings.toList.mkString("|")
which I can call normally:
scala> pipes("foo", "bar")
res1: String = foo|bar
or with a splat:
scala> val ...
2
Solved
Splats are cool. They're not just for exploding arrays, although that is fun. They can also cast to Array and flatten arrays (See http://github.com/mischa/splat/tree/master for an exhaustive list o...
Volsci asked 22/4, 2009 at 9:58
2
Solved
I am using Ruby on Rails 3 and I would like to know what means the presence of a * operator near a function argument and to understand its usages in others scenarios.
Example scenario (this method...
Lungi asked 9/3, 2011 at 23:20
3
Solved
I'm trying to write some PowerShell functions that do some stuff and then transparently call through to existing built-in functions. I want to pass along all the arguments untouched. I don't want t...
Raffo asked 15/1, 2011 at 22:7
1
Solved
Possible Duplicate:
What is the * operator doing to this string in Ruby
Probably there is answer for that elsewhere, but I just don't know how to find it...
If I am right, the * mean...
3
Solved
Run the following code,
a = [1, 2, 3, 4, 5]
head, *tail = a
p head
p tail
You will get the result
1
[2, 3, 4, 5]
Who can help me to explain the statement head,*tail = a, Thanks!
1
Solved
Say I have a list of arguments:
> (setf format-args `(t "it's ~a" 1))
(T "it's ~a" 1)
How can I then "splat" or "unroll" this into a series of arguments rather than a single list argument, f...
Incapacitate asked 27/2, 2010 at 2:22
2
Solved
fruit = ["apple","red","banana","yellow"]
=> ["apple", "red", "banana", "yellow"]
Hash[*fruit]
=> {"apple"=>"red", "banana"=>"yellow"}
Why does the splat cause the array to be so ne...
3
Solved
Given the Ruby code
line = "first_name=mickey;last_name=mouse;country=usa"
record = Hash[*line.split(/=|;/)]
I understand everything in the second line apart from the * operator - what is it ...
2
Solved
require 'pp'
p *1..10
This prints out 1-10. Why is this so concise? And what else can you do with it?
© 2022 - 2024 — McMap. All rights reserved.