When a hash is the last argument to a method, you can skip its open-
and close-braces.
So this:
IO.new("data.txt", mode: 'w:UTF-16LE', cr_newline: true)
Is the same as this:
IO.new("data.txt", {mode: 'w:UTF-16LE', cr_newline: true})
The first argument is the "data.txt" string, the second argument is
the entire hash. The method then checks the type of second argument,
and in this case - since it's a hash - it considers it to be the opt
variable, with mode taking default value.
I don't know of one, I'd recommend an alternative that when you see syntax
like this, you make a hypothesis about how it works, then go test your
hypothesis. In this case, you could just write a method that prints the
inspection of its arguments, then try calling it both ways to see what
happens. If it contradicts your hypothesis, modify it to accomodate the new
information.
···
On Sun, Feb 12, 2012 at 6:36 AM, maven apache <apachemaven0@gmail.com>wrote:
BTW, the above is just an example,in fact I found so many syntax that I can
not understand.
So I wonder if there is any documents cover all of ruby's *fiexible*
syntax?
The first argument is the "data.txt" string, the second argument is
the entire hash. The method then checks the type of second argument,
and in this case - since it's a hash - it considers it to be the opt
variable, with mode taking default value.
First is, as you are saying, {key => value}, where both key and value
can be anything. {'key' => 'value'} is okay (key and value are
strings), but for example this is also valid: { [1,2] => {3 => 4} }
(here key is an array, value is another hash).
The second way, introduced in Ruby 1.9, is {key: value}. Value is
still an arbitrary Ruby value, but the key is converted to a Symbol -
so this: {key: value} is equivalent to this: {:key => value}. The
second method is less typing, so it's more often used now, when you
don't care about compatibility with Ruby 1.8, and you are fine with
your keys all being symbols.
You can also mix both styles in a single hash definition.
# hmm, that's strange, what if I do this?
meth({:a => 'b'})
# >> {:a=>"b"}
# well that worked, I wonder why (contemplate it for later, or ask if you
can't figure it out)
# now what about those curly braces?
meth(:a => 'b')
# >> {:a=>"b"}
# okay, I guess they are the same.
# now I was thinking that the ':' symbol is same as '=>', lets see
meth(a: 'b')
# >> {:a=>"b"}
# aah, apparently it is... but wait, what if I have a number for a key?
meth(1: 'b')
# ~> -:5: syntax error, unexpected ':', expecting ')'
# ~> meth(1: 'b')
# ~> ^
# ~> -:5: syntax error, unexpected ')', expecting $end
# Hmm, maybe it only works for symbol arguments
# ... etc ...
···
On Sun, Feb 12, 2012 at 7:49 AM, maven apache <apachemaven0@gmail.com>wrote:
> When a hash is the last argument to a method, you can skip its open-
> and close-braces.
>
> So this:
> IO.new("data.txt", mode: 'w:UTF-16LE', cr_newline: true)
> Is the same as this:
> IO.new("data.txt", {mode: 'w:UTF-16LE', cr_newline: true})
>
Now in the IO.new exmaple,it is written as {key:value}. Is the ':' symbol
same as '=>'?? I do not find it is methioned in any document.
--
Saji N Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan
Okay, but ruby-doc's purpose isn't to teach you the language, it's to give
you an API reference.
···
On Mon, Feb 13, 2012 at 5:19 AM, Saji Hameed <saji@u-aizu.ac.jp> wrote:
On Sun, Feb 12, 2012 at 10:49 PM, maven apache <apachemaven0@gmail.com> > wrote:
> But in the ruby document:Programming Ruby: The Pragmatic Programmer's Guide
>
> Definiation of a hash should like this:
>
> {'key'=>'value'.....}
>
> Now in the IO.new exmaple,it is written as {key:value}. Is the ':'
symbol
> same as '=>'?? I do not find it is methioned in any document.
>
Unfortunately, even the latest rubydoc Class: Hash (Ruby 1.9.3) somehow does not mention this
new syntax.
But without knowing the syntax of this language,what is the meaning of the
API reference?
I think there should be some documents about the syntax of ruby including
the hidden feature like the hash exmple in this post.
···
2012/2/13 Josh Cheek <josh.cheek@gmail.com>
On Mon, Feb 13, 2012 at 5:19 AM, Saji Hameed <saji@u-aizu.ac.jp> wrote:
> On Sun, Feb 12, 2012 at 10:49 PM, maven apache <apachemaven0@gmail.com> > > wrote:
>
> > But in the ruby document:Programming Ruby: The Pragmatic Programmer's Guide
> >
> > Definiation of a hash should like this:
> >
> > {'key'=>'value'.....}
> >
> > Now in the IO.new exmaple,it is written as {key:value}. Is the ':'
> symbol
> > same as '=>'?? I do not find it is methioned in any document.
> >
>
> Unfortunately, even the latest rubydoc
> Class: Hash (Ruby 1.9.3) somehow does not mention this
> new syntax.
>
>
Okay, but ruby-doc's purpose isn't to teach you the language, it's to give
you an API reference.
And the std lib (especially documentation of a particular class) is
not the proper place for such a documentation. That's all Josh said.
Kind regards
robert
···
On Mon, Feb 13, 2012 at 1:10 PM, maven apache <apachemaven0@gmail.com> wrote:
2012/2/13 Josh Cheek <josh.cheek@gmail.com>
> On Mon, Feb 13, 2012 at 5:19 AM, Saji Hameed <saji@u-aizu.ac.jp> wrote:
>
> > On Sun, Feb 12, 2012 at 10:49 PM, maven apache <apachemaven0@gmail.com> > > > wrote:
> >
> > > But in the ruby document:Programming Ruby: The Pragmatic Programmer's Guide
> > >
> > > Definiation of a hash should like this:
> > >
> > > {'key'=>'value'.....}
> > >
> > > Now in the IO.new exmaple,it is written as {key:value}. Is the ':'
> > symbol
> > > same as '=>'?? I do not find it is methioned in any document.
> > >
> >
> > Unfortunately, even the latest rubydoc
> > http://ruby-doc.org/core-1.9.3/Hash.html somehow does not mention this
> > new syntax.
> >
> >
> Okay, but ruby-doc's purpose isn't to teach you the language, it's to give
> you an API reference.
But without knowing the syntax of this language,what is the meaning of the
API reference?
I think there should be some documents about the syntax of ruby including
the hidden feature like the hash exmple in this post.
as a new syntax, i would tend to think that rubydoc (which is derived
from the source code documentation) would be more or less the right
place to
mention it. we all know that rubydoc is not perfect... maybe we
should not try to defend it too much
saji
···
And the std lib (especially documentation of a particular class) is
not the proper place for such a documentation. That's all Josh said.
Saji N Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan
In fact, I like ruby . I just want to find some documents about this
language.
Since when I read the ruby(in fact rails) source codes,I often can not
understand some syntaxs even after I read the 'Programming ruby' and 'why's
(poignant) guide to ruby'.
And asking for help is my last choice, I can not expect people answer all
my questions about the syntaxs.
···
2012/2/13 Saji Hameed <saji@u-aizu.ac.jp>
as a new syntax, i would tend to think that rubydoc (which is derived
from the source code documentation) would be more or less the right
place to
mention it. we all know that rubydoc is not perfect... maybe we
should not try to defend it too much
saji
> And the std lib (especially documentation of a particular class) is
> not the proper place for such a documentation. That's all Josh said.
>
> Kind regards
>
> robert
>
>
> --
> remember.guy do |as, often| as.you_can - without end
> http://blog.rubybestpractices.com/
>
--
Saji N Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan
yes, please do keep asking -- it was a perfectly good question and i
am sure lots of people out here would be willing to share their
knowledge of ruby with you...
cheers,
saji
···
On Mon, Feb 13, 2012 at 9:41 PM, maven apache <apachemaven0@gmail.com> wrote:
I never think rubydoc is useless.
In fact, I like ruby . I just want to find some documents about this
language.
Since when I read the ruby(in fact rails) source codes,I often can not
understand some syntaxs even after I read the 'Programming ruby' and 'why's
(poignant) guide to ruby'.
And asking for help is my last choice, I can not expect people answer all
my questions about the syntaxs.
2012/2/13 Saji Hameed <saji@u-aizu.ac.jp>
as a new syntax, i would tend to think that rubydoc (which is derived
from the source code documentation) would be more or less the right
place to
mention it. we all know that rubydoc is not perfect... maybe we
should not try to defend it too much
saji
> And the std lib (especially documentation of a particular class) is
> not the proper place for such a documentation. That's all Josh said.
>
> Kind regards
>
> robert
>
>
> --
> remember.guy do |as, often| as.you_can - without end
> http://blog.rubybestpractices.com/
>
--
Saji N Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan
Saji N Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan
yes, please do keep asking -- it was a perfectly good question and i
am sure lots of people out here would be willing to share their
knowledge of ruby with you...
cheers,
saji
On Mon, Feb 13, 2012 at 9:41 PM, maven apache <apachemaven0@gmail.com> > wrote:
>
>
> I never think rubydoc is useless.
>
> In fact, I like ruby . I just want to find some documents about this
> language.
>
> Since when I read the ruby(in fact rails) source codes,I often can not
> understand some syntaxs even after I read the 'Programming ruby' and
'why's
> (poignant) guide to ruby'.
>
> And asking for help is my last choice, I can not expect people answer all
> my questions about the syntaxs.
>
> 2012/2/13 Saji Hameed <saji@u-aizu.ac.jp>
>
>> as a new syntax, i would tend to think that rubydoc (which is derived
>> from the source code documentation) would be more or less the right
>> place to
>> mention it. we all know that rubydoc is not perfect... maybe we
>> should not try to defend it too much
>>
>> saji
>>
>> > And the std lib (especially documentation of a particular class) is
>> > not the proper place for such a documentation. That's all Josh said.
>> >
>> > Kind regards
>> >
>> > robert
>> >
>> >
>> > --
>> > remember.guy do |as, often| as.you_can - without end
>> > http://blog.rubybestpractices.com/
>> >
>>
>>
>>
>> --
>>
>> Saji N Hameed,
>> ARC-ENV, Center for Advanced Information Science and Technology,
>> University of Aizu, Tsuruga, Ikki-machi,
>> Aizuwakamatsu-shi, Fukushima 965-8580,
>> Japan
>>
>> Tel: +81242 37-2736
>> Fax:+81242 37-2760
>> email: saji@u-aizu.ac.jp
>> url: http://www.u-aizu.ac.jp
>> bib: Web of Science
>>
>>
--
Saji N Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan
I remember saying in one of these threads that documentation should be
pervasive and this is a perfect example of why this should be the
case. The syntax rules of any language, natural or constructed,
belongs in the standard documentation of said language. The only
reason this wasn't really done in the past is that a printed book
couldn't contain this information in a reasonable way. Hypertext
allows us to achieve this.
···
On Mon, Feb 13, 2012 at 9:01 AM, Robert Klemme <shortcutter@googlemail.com> wrote:
Please do not top post.
On Mon, Feb 13, 2012 at 1:41 PM, maven apache <apachemaven0@gmail.com> wrote:
I never think rubydoc is useless.
It's just not the proper medium for documentation of the language.
JavaDoc is also not used to document the language - for that the JLS
is used.
In fact, I like ruby . I just want to find some documents about this
language.
I remember saying in one of these threads that documentation should be
pervasive and this is a perfect example of why this should be the
case. The syntax rules of any language, natural or constructed,
belongs in the standard documentation of said language. The only
reason this wasn't really done in the past is that a printed book
couldn't contain this information in a reasonable way.
What exactly is your definition of "reasonable"? According to my
definition a book is perfectly capable of providing documentation of
syntax in a reasonable way - and actually there are tons of books
around which present syntax of languages in a way useful for the
reader.
Hypertext allows us to achieve this.
Of course you can also document formal language syntax in a hypertext
document, and maybe even better than in a printed book. It is not
necessary though.
Kind regards
robert
···
On Wed, Feb 15, 2012 at 3:08 AM, Kevin <darkintent@gmail.com> wrote:
By reasonable I mean that the volume would not become overly large and
cumbersome to navigate. Containing a proper treatment of the entire
grammar of most languages would be about the same size as a good
pocket or abridged dictionary. Then there is all the flipping around
between pages which doesn't help concentration. In the context of
constructed languages like Ruby one may well be literate but not be
familiar with all of the different parts of the syntax. A language
doesn't exist without the grammar and the API. Technical limitations
have prevented us from representing this fact for most of human
history, instead we've tried to club the entire grammar of our
languages into our heads with some success. That doesn't mean we
should keep doing that when we have the tools to avoid it. Imagine if
we tried to include the entire source for the whole standard library
in the pickaxe the book would be huge and a true pain to navigate.
This annoyance is of course lessened in the electronic versions
because you get links you can click to quickly navigate from the index
at least and you usually have some capacity to do a find for a
specific word etc. Emacs got the right idea, even if the format used
isn't as nice as hypertext. The main things I would add are a
function that could tell me what a given function is made of*, and
integration with the elisp manual. That would complement the fact
that describe-function defaults to selecting the function at the
current depth.**
I've been getting better and better at reading code as I go deeper
into programming, but I still long for better and more easily
navigable documentation.
*Eg. Move into an Emacs function fire off command that shows the
various parts of a function and links to relevant sections of the
manual that explain those things. Though I don't think that is
technically possible.
**If you have code like (let ((f b))) if you put the cursor within the
level of (let) describe-function will have let already selected.
···
On Wed, Feb 15, 2012 at 5:29 AM, Robert Klemme <shortcutter@googlemail.com> wrote:
On Wed, Feb 15, 2012 at 3:08 AM, Kevin <darkintent@gmail.com> wrote:
I remember saying in one of these threads that documentation should be
pervasive and this is a perfect example of why this should be the
case. The syntax rules of any language, natural or constructed,
belongs in the standard documentation of said language. The only
reason this wasn't really done in the past is that a printed book
couldn't contain this information in a reasonable way.
What exactly is your definition of "reasonable"? According to my
definition a book is perfectly capable of providing documentation of
syntax in a reasonable way - and actually there are tons of books
around which present syntax of languages in a way useful for the
reader.
Hypertext allows us to achieve this.
Of course you can also document formal language syntax in a hypertext
document, and maybe even better than in a printed book. It is not
necessary though.