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...
Merger asked 19/12, 2012 at 20:32

1

Solved

A splat on a hash converts it into an array. [*{foo: :bar}] # => [[:foo, :bar]] Is there some hidden mechanism (such as implicit class cast) going on here, or is it a built-in primitive featu...
Varela asked 13/1, 2013 at 12:35

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...
Voss asked 6/1, 2013 at 23:27

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"]
Expediency asked 27/10, 2010 at 8:9

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] #...
Nostology asked 26/9, 2011 at 15:23

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 &gt...
Dowland asked 14/8, 2011 at 1:13

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...
Bigeye asked 12/7, 2011 at 20:41

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...
Serenata asked 12/5, 2011 at 17:12

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 ...
Duel asked 13/4, 2011 at 12:44

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...
Boxcar asked 12/11, 2010 at 23:53

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!
Astto asked 1/9, 2010 at 13:43

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...
Smarmy asked 22/9, 2009 at 4:23

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 ...
Merrie asked 27/5, 2009 at 23:17

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?
Carbonization asked 2/4, 2009 at 5:23

© 2022 - 2024 — McMap. All rights reserved.