3) array (whether you use [] or not, it is still an array literal)
s(:masgn,
which look like Lisp to me).
Ultimately, then, the website’s first lines show me that splat on
arrays is an Identity prefix operator, without mentioning that
explicitly. That’s not very helpful, IMHO. Don’t you agree?
Best wishes,
Richard
A little more info … this time, incorrect IMHO:
Splat array of arrays:
a = [[:planes, 21], [:cars, 36]]
h = Hash[*a] # => {[:planes, 21]=>[:cars, 36]}
Allegedly, h = Hash[*a] # => { :planes=>21, :cars=>36}
Do you concur in my finding that the site’s in error here?
So, I hunt for a more informative (and more accurate) presentation of
the virtues of splat.
It looks to me the you gave me Lisp expressions one could use, in
part, to translate the three right-hand-sides I provided. I knew that
Ruby treats “x,y = 1,2” as equivalent to “x=1; y=2”,
but I didn’t know that Ruby first view them as “LHS-array assigned
values from a RHS-array” (which is how I interpret your expressions,
which look like Lisp to me).
Ultimately, then, the website’s first lines show me that splat on
arrays is an Identity prefix operator, without mentioning that
explicitly. That’s not very helpful, IMHO. Don’t you agree?
It looks to me the you gave me Lisp expressions one could use, in
part, to translate the three right-hand-sides I provided.
Ultimately, then, the website’s first lines show me that splat on
arrays is an Identity prefix operator, without mentioning that
explicitly. That’s not very helpful, IMHO. Don’t you agree?
I have no idea what you’re saying here nor why you’re capitalizing
identity.
This implies to me that I should be able to define to_ary on an object,
and
it will be able to be RHS of a mass assign.
a = Object.new
def a.to_ary
[1, 2]
end
b , c = a
b # => 1
c # => 2
Yes, it works. But I am confused where to_ary came from. I have never
used
it myself, and wouldn’t have even thought to use it except I saw it in
the
sexp. Googling turns up Splat, #to_ary and #to_a - Ruby - Ruby-Forum, which
isn’t
helpful, and Pragmatic Bookshelf: By Developers, For Developers which left
me
rather unconvinced, and only addresses the difference without really
addressing the reasoning.
Same for the Pickaxe which says on 366
to_ary: “This is used when interpreter needs a parameter to a method to
be
an array, and when expanding parameters and assignments containing the
*xyz
syntax”
to_a: “This is used when the interpreter needs to convert an object into
an
array for parameter passing or multiple assignment.”
So one’s a po-tay-to, the other’s a po-tah-to?
In The Ruby P.ming Language, on page 80, it says they are defined
for
the purposes of implicit conversions. I verified with this code
class Fixnum
alias to_str to_s
end
‘1’ + 1 # => “11”
But I don’t understand why the implicit version should be different from
the
explicit version, or why “#{obj}” doesn’t invoke the implicit version.
Is
there some example which shows the necessity of a separate, but but
similar
method for to_a/to_ary, to_i/to_int, to_s/to_str, etc?
Splat array of arrays:
a = [[:planes, 21], [:cars, 36]]
h = Hash[*a] # => {[:planes, 21]=>[:cars, 36]}
Allegedly, h = Hash[*a] # => { :planes=>21, :cars=>36}
Do you concur in my finding that the site’s in error here?
So, I hunt for a more informative (and more accurate) presentation of
the virtues of splat.
Yes, it works. But I am confused where to_ary came from. I have never used
it myself, and wouldn’t have even thought to use it except I saw it in the
sexp.
to_ary and to_str are implicit cast methods. to_a (deprecated for
Array()) and to_s are (usually) explicit cast methods. to_ary/str are
used internally when something is used in an array or string context and
you’re saying that the object is array-like or string-like. Most things
aren’t array-like tho, so you don’t see the method much.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.