Questions tagged [splat]

The `*` operator as used in Ruby. So called because it looks like an insect that's splatted on a windscreen.

The * operator as used in Ruby. So called because it looks like an insect that's splatted on a windscreen.

166 questions
0
votes
2 answers

Ruby - Reduce array of numbers in ruby returns strange result

I'm trying to write a method called calculate which determine to add or subtract numbers depending on a keyword argument passed to it. Here're the methods: def add(*num) num.inject(:+) end def subtract(*num) num.reduce(:-) end def…
Rafael Adel
  • 7,673
  • 25
  • 77
  • 118
0
votes
2 answers

Ruby send using splat not working as expected

I have a services class that helps sanitize data from a JSON payload. attr_reader :data, :method, :args def self.call(*args) new(*args).call end def initialize(data, sanitization_method, *method_args) @data = data @method =…
Jay
  • 493
  • 5
  • 14
0
votes
1 answer

Unrolling list into function args?

Given an argument list and a two-arity function: args = [5, 6] f = ((y,z)->y*z) How do I unroll args into function arguments? - E.g. in Python you can do: f(*args). What I've tried (more JavaScript style I suppose): Function.call.apply(f, args) #…
A T
  • 13,008
  • 21
  • 97
  • 158
0
votes
1 answer

Finding documentation on python "native" types, e.g. set

I'm trying to learn Python, and, while I managed to stumble on the answer to my current problem, I'd like to know how I can better find answers in the future. My goal was to take a list of strings as input, and return a string whose characters were…
JonathanZ
  • 1,046
  • 11
  • 20
0
votes
2 answers

Meaning of * in argument position

Assume arr is an array [[1,3],[2,5],[3,8]]. I was wondering what * means. What does it mean? hash = Hash[*arr.flatten] # => {1=>3, 2=>5, 3=>8} I tried the following arr.flatten # => [1, 3, 2, 5, 3, 8] Hash[arr.flatten] # => {} Hash[*a.flatten] #…
AirWick219
  • 894
  • 8
  • 27
0
votes
1 answer

Get the number of variables in a splat ruby

How can I count the number of values being passed as a splat argument while calling a method? def splat_demo(*var) #code to find out no of values in the splat variable here end
tekina
  • 571
  • 10
  • 21
0
votes
2 answers

Pass a Pre-made Array as an Arguement/Parameter in Ruby?

I want to pass a list of my players in a Go Fish Card Game to a method that deals cards to players. I want to pass in the already-made players array as an argument/parameter to the deal method. I know I'd have to use the splat operator to pass in…
Yallo
  • 51
  • 2
  • 9
0
votes
3 answers

Use current argument of splat operator

I have a list of car makes makes = [acura, honda, ford] and I'm trying to iterate through an array of strings and find out if the individual string contains one of these makes AND if it does, to put that specific makes into an array so I've got…
Kwestion
  • 464
  • 5
  • 19
0
votes
1 answer

Generate Tri-Planar UVs For Arbitrary Mesh?

I have a 3D terrain (a voxel mesh, my "arbitrary mesh"). I know how to "splat" the texture down from above the mesh, but on vertical or steep slopes it smears. I have access to the normals and positions of each vertex. How would I generate UVs…
Clonkex
  • 3,373
  • 7
  • 38
  • 55
-1
votes
1 answer

Dynamically create class members sqlalchemy

I have a tuple of data: ('dave', 23, 'm'), ('alice', 33, 'f'), ('mary', 44, 'f') I have an sqlalchemy base class: class People(Base): __tablename__='people' I want to be able to dynamically create columns/fields for this class using data from…
Stephen K
  • 697
  • 9
  • 26
-1
votes
1 answer

Splat a list in to a function

I have a list fb_param_list = ['Fb01', 'Fb02',..., 'Fb76', 'Fb77'] 62 elements in total I want to use are a parameter into my function def get_data(timeFrame,period,*args): for chute in args: try: ret = get (Period=period, …
pyth0nBen
  • 91
  • 1
  • 10
-1
votes
1 answer

Syntax error: initialization with splat operator and empty hash

I have the following code that as far as I know should work correctly but doesn't: def calculate( *nums, options = {} ) //errors out here on equals sign if options.empty return add(nums) elsif options[:subtract] return substract(nums) …
jStaff
  • 650
  • 1
  • 9
  • 25
-1
votes
3 answers

Unexpected result with splat operator

I have a hash, whose values are an array of size 1: hash = {:start => [1]} I want to unpack the arrays as in: hash.each_pair{ |key, value| hash[key] = value[0] } # => {:start=>1} and I thought the *-operator as in the following would work, but it…
SimonH
  • 1,385
  • 15
  • 35
-1
votes
1 answer

Ruby Method Explanation

so I'm working through some examples of Ruby Methods from the rubymonk website and am having trouble interpreting what is going on in the code below. More specifically, I was hoping that someone might be able to help explain in layman's terms what…
John
  • 443
  • 8
  • 19
-2
votes
1 answer

Splat operator and method overriding

I have a base class with child classes that override a method that takes multiple arguments. class Parent def foo *bar end end class Child < Parent def foo bar, baz end end This works fine. However, suppose there is a method foobar in…
Leo Brito
  • 2,053
  • 13
  • 20
1 2 3
11
12