From: "Booker C.Bense" Date: 2006-10-13T07:30:14+09:00 Subject: Re: Ruby expressions vs. values (like Perl?) -----BEGIN PGP SIGNED MESSAGE----- In article <65e0bb520610121433u151b89d9y1d757621bc4d41d3@mail.gmail.com>, John Gabriele wrote: >Does Ruby use the same terminology as Perl with regard to expressions >versus values? > >I read a great MJD comment on PerlMonks a while back ( >http://www.perlmonks.org/?node_id=72263 ). That comment notes how, in >Perl, one needs to be well aware of the difference between expressions >(the things you type into your code), and values (the things the >interpreter evaluates expressions to). > >It seems that, the big reason for watching the difference between >expressions and values in Perl is because expressions turn into >different values at runtime depending upon context. This is what many people dislike about perl. In Ruby, the distinction mostly doesn't exist. It's an Object... > >So, back to Ruby, I believe that the rhs of the following statement is >an "Array expression": > >irb(main):001:0> foo = ['a','bee','sea'] >=> ["a", "bee", "sea"] > >but, what kind of expression is the rhs of the following: > >irb(main):002:0> bar = 'dee', 'ee', 'eff' >=> ["dee", "ee", "eff"] > >? One of the nice things about Ruby is that you can always ask it what it thinks. irb(main):090: bar.class => Array irb(main):010:0> bar.methods => ["respond_to?", "index", "select", "<<", "to_a", "delete_if", "slice", "&", "type", "each_index", "length", "protected_methods", "partition", "sort!", "assoc", "to_ary", "eql?", "*", "grep", "instance_variable_set", "+", "is_a?", "hash", "push", "send", "to_s", "-", "rindex", "reject", "size", "pack", "join", "class", "reverse_each", "tainted?", "collect!", "private_methods", "rassoc", "at", "member?", "__send__", "compact!", "untaint", "|", "find", "each_with_index", "reject!", "flatten", "id", "pop", "slice!", "replace", "collect", "inspect", "transpose", "instance_eval", "reverse", "all?", "clone", "entries", "indexes", "public_methods", "map!", "uniq", "fetch", "extend", "freeze", "detect", "values_at", "display", "zip", "__id__", "shift", "<=>", "methods", "method", "map", "==", "clear", "empty?", "===", "any?", "indices", "sort", "nil?", "dup", "instance_variables", "include?", "min", "instance_of?", "flatten!", "first", "find_all", "delete_at", "nitems", "each", "object_id", "insert", "reverse!", "unshift", "=~", "inject", "singleton_methods", "fill", "delete", "concat", "uniq!", "equal?", "taint", "sort_by", "instance_variable_get", "max", "[]", "frozen?", "compact", "kind_of?", "last", "[]="] > >Incidentally, I also note that this one -- although performing >parallel assignment -- also happens to be generating an Array value: > >irb(main):003:0> zz = ( a, b, c = 'gee', 'aitch', 'aye' ) >=> ["gee", "aitch", "aye"] > >irb(main):004:0> zz >=> ["gee", "aitch", "aye"] > >According to irb, the Array gets generated even if you leave off the >"zz =" and the parentheses. Seems like extra work for the interpreter >to go through (making that Array), just to do a parallel assignment, >no? It's not making an Array, it's making a reference to an existing object. Every ruby statement generally returns a reference to the result, this can be an incredibly powerful tool. _ Booker C. Bense -----BEGIN PGP SIGNATURE----- Version: 2.6.2 iQCVAwUBRS7BCGTWTAjn5N/lAQGrXgP+NWJ+xN04v1o67cQ9PSQeHCtakRJQAz3r Rx/ivNqUXXMeR9uL3G2Ffui8BTE1KGpnsci2rzBcrxn2w1yjqAOV/IkmKFrmk72g 27FlxtqVCT3OonCx1KtsHF/8akrqlojFPwdw5MVvDXAxFQoYYOY2DE17WVmfXmY9 T+wBUTromgw= =Ilxn -----END PGP SIGNATURE-----