splat

Best way to document “splatted” parameter with YARD? [closed]

感情迁移 提交于 2019-11-30 18:48:04
I have a method that should take 1+ parameters of any class, similar to Array#push : def my_push(*objects) raise ArgumentError, 'Needs 1+ arguments' if objects.empty? objects.each do |obj| puts "An object was pushed: #{obj.inspect}" @my_array.push obj end end What is the best way to document the method parameters using YARD syntax? Edit: I realize that my original question was a bit too vague and didn't quite specify what I was looking for. A better question would be, what is the best way to specify the arity of a method (1-∞ in this case) in YARD when using a splatted parameter? I know I

What does this mean in Ruby language?

回眸只為那壹抹淺笑 提交于 2019-11-30 17:36:43
问题 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: head, *tail = a means to assign the first element of the array a to head , and assign the rest of the elements to tail . * , sometimes called the "splat operator," does a number of things with arrays. When it's on the left side of an assignment operator ( = ), as in your example, it just means "take everything

Understanding ruby splat in ranges and arrays

十年热恋 提交于 2019-11-30 16:51:27
问题 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] # splat2 = [1, 2, 3, 4, 5, 6, 7, 8, 9] But things get weird when I try to use *(1..9) and [*1..9] directly. *(1..9).map{|a| a.to_s} # syntax error, unexpected '\n', expecting tCOLON2 or '[' or '.' [*1..9].map{|a| a.to_s} # ["1", "2", "3"...] I'm guessing part of the problem is with operator precidence?

Why invoke “apply” instead of calling function directly?

早过忘川 提交于 2019-11-30 08:45:28
When looking at the source code for raphael or g.raphael or other libraries I've noticed the developer does something like this: var val = Math.max.apply(Math, data_array); Why not just invoke the function directly, such as: var val = Math.max(data_array); Thanks. I think the explanation from the Mozilla Docs describes it well: You can assign a different this object when calling an existing function. this refers to the current object, the calling object. With apply, you can write a method once and then inherit it in another object, without having to rewrite the method for the new object. apply

What does the * (asterisk) symbol do near a function argument and how to use that in others scenarios?

妖精的绣舞 提交于 2019-11-30 06:42:48
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 was from the Ruby on Rails 3 framework): def find(*args) return to_a.find { |*block_args| yield(*block_args) } if block_given? options = args.extract_options! if options.present? apply_finder_options(options).find(*args) else case args.first when :first, :last, :all send(args.first) else find_with_ids(*args) end end end This is the splat operator, which comes from ruby (and is thus not rails specific

Why invoke “apply” instead of calling function directly?

流过昼夜 提交于 2019-11-29 12:20:23
问题 When looking at the source code for raphael or g.raphael or other libraries I've noticed the developer does something like this: var val = Math.max.apply(Math, data_array); Why not just invoke the function directly, such as: var val = Math.max(data_array); Thanks. 回答1: I think the explanation from the Mozilla Docs describes it well: You can assign a different this object when calling an existing function. this refers to the current object, the calling object. With apply, you can write a

What is the standalone splat operator (*) used for in Ruby?

牧云@^-^@ 提交于 2019-11-29 08:14:48
I just came across this example where the splat operator is used by itself in a method definition: def print_pair(a,b,*) puts "#{a} and #{b}" end print_pair(1,2,3,:cake,7) #=> 1 and 2 It is clear what and why you would use it in a context like so: def arguments_and_opts(*args, opts) puts "arguments: #{args} options: #{opts}" end arguments_and_opts(1,2,3, a: 5) #=> arguments: [1, 2, 3] options: {:a=>5} But why and how would you use it in the first example? Since it is defined in the Ruby specs there must be a usecase for it? In a parameter list, *args means "gobble up all the remaining

Double-splat operator destructively modifies hash – is this a Ruby bug?

雨燕双飞 提交于 2019-11-27 23:45:35
I noticed what I find to be a very surprising behavior with the ** (double-splat) operator in Ruby 2.1.1. When key-value pairs are used before a **hash , the hash remains unmodified; however, when key-value pairs are only used after the **hash , the hash is permanently modified. h = { b: 2 } { a: 1, **h } # => { a: 1, b: 2 } h # => { b: 2 } { a: 1, **h, c: 3 } # => { a: 1, b: 2, c: 3 } h # => { b: 2 } { **h, c: 3 } # => { b: 2, c: 3 } h # => { b: 2, c: 3 } For comparison, consider the behavior of the single- * operator on arrays: a = [2] [1, *a] # => [1, 2] a # => [2] [1, *a, 3] # => [1, 2, 3]

What does the * (star) mean in Ruby? [duplicate]

核能气质少年 提交于 2019-11-27 20:31:34
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 * means multiple parameters if used in function definition: def hero(name, *super_powers) But what does * do in the code like this: Hash[*[[:first_name, 'Shane'], [:last_name, 'Harvie']].flatten] # => {:first_name=>"Shane", :last_name=>"Harvie"} Variable Length Argument List, Asterisk Operator The last parameter of a method may be preceded by an asterisk(*), which is sometimes called the 'splat' operator. This indicates

Double-splat operator destructively modifies hash – is this a Ruby bug?

青春壹個敷衍的年華 提交于 2019-11-27 04:41:48
问题 I noticed what I find to be a very surprising behavior with the ** (double-splat) operator in Ruby 2.1.1. When key-value pairs are used before a **hash , the hash remains unmodified; however, when key-value pairs are only used after the **hash , the hash is permanently modified. h = { b: 2 } { a: 1, **h } # => { a: 1, b: 2 } h # => { b: 2 } { a: 1, **h, c: 3 } # => { a: 1, b: 2, c: 3 } h # => { b: 2 } { **h, c: 3 } # => { b: 2, c: 3 } h # => { b: 2, c: 3 } For comparison, consider the