From: Brent Clark on 29 Jul 2010 07:59 Hiya Im going through the Ruby Cookbook. On page 51, there is the following example letter = { ?v => 'aeiou', ?c => 'xyz' } My question is, what does the key ?v do or mean. Kind Regards Brent Clark
From: Jesús Gabriel y Galán on 29 Jul 2010 08:34 On Thu, Jul 29, 2010 at 1:59 PM, Brent Clark <brentgclarklist(a)gmail.com> wrote: > Hiya > > Im going through the Ruby Cookbook. > > On page 51, there is the following example > > letter = { ?v => 'aeiou', ?c => 'xyz' } > > My question is, what does the key ?v do or mean. irb(main):008:0> ?v.class => Fixnum irb(main):009:0> ?v => 118 irb(main):010:0> "v"[0] => 118 It's a literal that contains the numeric value of the character v Jesus.
From: Josh Cheek on 29 Jul 2010 08:50 [Note: parts of this message were removed to make it a legal post.] On Thu, Jul 29, 2010 at 6:59 AM, Brent Clark <brentgclarklist(a)gmail.com>wrote: > Hiya > > Im going through the Ruby Cookbook. > > On page 51, there is the following example > > letter = { ?v => 'aeiou', ?c => 'xyz' } > > My question is, what does the key ?v do or mean. > > Kind Regards > Brent Clark > > In earlier versions of Ruby, prefixing a character with a question mark was sort of a literal for a given character. I just checked irb for 1.8.6 and 1.8.7, ?v would return 118, which is the ascii value of the character v, and in 1.9.1 it returned "v" the one character String. Another place you can see this shift take place is in String#[] where you might say "vat"[0] in 1.8 you would get 118, and in 1.9 you would get "v" A more idiomatic alternative would probably be to use symbols as hash keys. letter = { :vowels => 'aeiou' , :consonants => 'xyz' } (I don't have the book, I'm guessing as to what ?v and ?c are supposed to mean)
From: Eugen Ciur on 29 Jul 2010 08:50 It returns ASCII code following for character question mark: For example: irb(main):002:0> ?a => 97 On 07/29/2010 02:59 PM, Brent Clark wrote: > Hiya > > Im going through the Ruby Cookbook. > > On page 51, there is the following example > > letter = { ?v => 'aeiou', ?c => 'xyz' } > > My question is, what does the key ?v do or mean. > > Kind Regards > Brent Clark > >
|
Pages: 1 Prev: my script just read one line? Next: ruport, reading a csv whose column name have white spaces |