The ruby syntax

Hi:

I like ruby the first time I read its document.

However then I found that the syntax is too fliexible to learn it.

For example:

The IO.open method,this is the api page:
http://ruby-doc.org/core-1.9.3/IO.html#method-c-new

This is the argument of this method:

new(fd [, mode] [, opt])

I am confused by the third argument-‘opt’.

Is its type is ‘hash’??

If so I think this is the way to call it:

IO>new(“data.txt”, “w”,{:encoding=>‘xxx’,:autoclose=>true…})

But I found so many people call it like this:

IO.new(“data.txt”, mode: ‘w:UTF-16LE’, cr_newline: true)

What does this mean?

‘data.txt’ is the first argument. How about the others? which is the
second? and which is the third?

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.

– Matma R.

On Sun, Feb 12, 2012 at 6:36 AM, maven apache
[email protected]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?

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.

2012/2/12 Bartosz Dziewoński [email protected]

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})

But in the ruby document:http://www.ruby-doc.org/docs/ProgrammingRuby/

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.

2012/2/12 maven apache [email protected]:

But in the ruby document:http://www.ruby-doc.org/docs/ProgrammingRuby/

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.

There are two ways to define a hash.

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.

– Matma R.

On Sun, Feb 12, 2012 at 7:49 AM, maven apache
[email protected]wrote:

But in the ruby document:http://www.ruby-doc.org/docs/ProgrammingRuby/

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.

So, again, you could figure out the answer to this by writing a method
that
simply prints the inspection of its args. Try this:

def meth(arg)
p arg
end

meth {:a => ‘b’}

~> -:5: syntax error, unexpected tASSOC, expecting ‘}’

~> …9178_68393_18610 = (meth {:a => ‘b’});$stderr.puts("!XMP1329…

~> … ^

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 10:49 PM, maven apache [email protected]
wrote:

But in the ruby document:http://www.ruby-doc.org/docs/ProgrammingRuby/

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.

saji

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: [email protected]
url: http://www.u-aizu.ac.jp
bib: http://www.researcherid.com/rid/B-9188-2009

On Feb 12, 2012, at 05:49 , maven apache wrote:

But in the ruby document:http://www.ruby-doc.org/docs/ProgrammingRuby/

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.

Extracted from the book “Programming Ruby - The Pragmatic Programmer’s
Guide”
Copyright 2001 by Addison Wesley Longman, Inc. This material may be
distributed only subject to the terms and conditions set forth in the
Open Publication License, v1.0 or later (the latest version is presently
available at http://www.opencontent.org/openpub/)).

2012/2/13 Josh C. [email protected]

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.

On Mon, Feb 13, 2012 at 5:19 AM, Saji H. [email protected] wrote:

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.

On Mon, Feb 13, 2012 at 1:10 PM, maven apache [email protected]
wrote:

Definiation of a hash should like 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.

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

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 :wink:

saji

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: [email protected]
url: http://www.u-aizu.ac.jp
bib: http://www.researcherid.com/rid/B-9188-2009

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 [email protected]
wrote:


University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan

Tel: +81242 37-2736
Fax:+81242 37-2760
email: [email protected]
url: http://www.u-aizu.ac.jp
bib: http://www.researcherid.com/rid/B-9188-2009

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: [email protected]
url: http://www.u-aizu.ac.jp
bib: http://www.researcherid.com/rid/B-9188-2009

:slight_smile:

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. :slight_smile:

2012/2/13 Saji H. [email protected]

Thanks for the kind of all you guys.

I will keep asking if need.

Thanks. :slight_smile:

2012/2/13 Saji H. [email protected]

Please do not top post.

On Mon, Feb 13, 2012 at 1:41 PM, maven apache [email protected]
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 recommend
http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/
http://www.amazon.com/Programming-Ruby-1-9-Pragmatic-Programmers/dp/1934356085/
http://www.amazon.com/Well-Grounded-Rubyist-David-Black/dp/1933988657/

Kind regards

robert

On Mon, Feb 13, 2012 at 9:01 AM, Robert K.
[email protected] wrote:

language.

remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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 Wed, Feb 15, 2012 at 3:08 AM, Kevin [email protected] 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.

Kind regards

robert

On Wed, Feb 15, 2012 at 5:29 AM, Robert K.
[email protected] wrote:

definition a book is perfectly capable of providing documentation of
Kind regards

robert


remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs