Question about Hash and Array

hi,

I have a confusion in creating array and hash, Please have look at the
below code.

a=[]

Code mentioned afore create empty array, Now my question is, whether
“[]” is a function call which returns the empty array or “[]” is already
created empty Array Object which we are assigning to Array?

puts [].class #which prints “Array” but I don’t know whether [] is a
function or Object.

puts {}.class #which is not printing anything like Array as I mention
afore.

could anyone please clarify for me?

a = []

should be the same as

a = Array.new

[] is called array object literal, it’s just a different way of creating
a new array object.

[].object_id == [].object_id # => false

Vlad M_ wrote in post #1147245:

[] is called array object literal, it’s just a different way of creating
a new array object.

What do you mean by array object literal? where can I refer this?

a = []

After assignment being done, “a” is receiving the empty object, what
does it mean?, right side value providing the empty object, so it may
provide the empty object by calling a function name “[]” which returns
empty object or predefined empty object might be named as “[]”,isn’t it?
So My question was whether it’s a function call or predefined empty
object?

puts {}.class

is parsed as

(puts() {}).class

where {} is used as block
that prints nothing and return nil, and then NilClass

you need

puts({}.class)

See this:
http://www.ruby-doc.org/core-2.1.1/Array.html#class-Array-label-Creating+Arrays

[] and {} are not functions; they are special syntax that
is being recognized by the ruby interpreter as a particular type/object.

hi Vlad M_

Thank you.

hi Hans M.

That’s an excellent explanation,thank you.


Raja gopalan wrote in post #1147248:

a = []

After assignment being done, “a” is receiving the empty object, what
does it mean?, right side value providing the empty object, so it may
provide the empty object by calling a function name “[]” which returns
empty object or predefined empty object might be named as “[]”,isn’t it?
So My question was whether it’s a function call or predefined empty
object?

It is an expression which you can very easily test yourself:

irb(main):003:0> 4.times { p([].object_id) }
12886935000
12886934740
12886934700
12886934460
=> 4

As you can see: all different objects.

Robert K. wrote in post #1147445:

Raja gopalan wrote in post #1147248:

a = []

After assignment being done, “a” is receiving the empty object, what
does it mean?, right side value providing the empty object, so it may
provide the empty object by calling a function name “[]” which returns
empty object or predefined empty object might be named as “[]”,isn’t it?
So My question was whether it’s a function call or predefined empty
object?

It is an expression which you can very easily test yourself:

irb(main):003:0> 4.times { p([].object_id) }
12886935000
12886934740
12886934700
12886934460
=> 4

As you can see: all different objects.

Expression evaluates and returns something,isn’t it?

For an example if statement is an expression in Ruby


a=3
b=if a>2
‘greater 3’
end
puts b


If so, Since you told me “[]” is an expression, it returns new array
object every time, but what it evaluates? Something it needs to
evaluates and the result of that it needs to return Array object,isn’t
it?

hi,

Thanks for your nice explanation, But I have been confused a bit so to
speak. Can you please define the following question for me please

What’s expression? If expression is something which returns the values
each time, Can I consider a function is a expression?

you said :[] receives a list of values - and that list may be empty:

If so, “[]” is a sort of function to receive and return values? If not
how does it differ from function?

Raja gopalan wrote in post #1147539:

Thanks for your nice explanation, But I have been confused a bit so to
speak. Can you please define the following question for me please

What’s expression?

In Ruby essentially anything is an expression because anything produces
a value. There is no such thing as a statement like in other
programming languages.

If expression is something which returns the values
each time, Can I consider a function is a expression?

I’d reserve the term “function” for the intangible thing that is created
as a side effect of executing a function definition (introduced by “def
…”) which is an expression (line 1 below). You can use a function in
an expression, i.e. by invoking it (line 2 below).

Lastly you can even obtain a handle on the function (line 3 below). And
you can use that to invoke the function via method #call on that handle
(line 4 below).

irb(main):001:0> def f(a,b) a + b end
=> nil
irb(main):002:0> 4 + f(1, 3)
=> 8
irb(main):003:0> m = method(:f)
=> #<Method: Object#f>
irb(main):004:0> m.call(1, 2)
=> 3

you said :[] receives a list of values - and that list may be empty:

If so, “[]” is a sort of function to receive and return values? If not
how does it differ from function?

It has a different syntax for invocation than other functions.

Note: I used the term “function” here to not change terminology in the
middle of the discussion although the term “method” would be more
precise as there are only methods (self is always defined inside a
method).

Raja gopalan wrote in post #1147448:

If so, Since you told me “[]” is an expression, it returns new array
object every time, but what it evaluates? Something it needs to
evaluates and the result of that it needs to return Array object,isn’t
it?

No. An expression does not need any inputs to produce output. [] is a
perfect example for that. You can define a function that receives no
input and still produces different output every time when called (i.e.
evaluated):

irb(main):001:0> def a;Array.new end
=> nil
irb(main):002:0> 4.times { x=a; printf “%10d %p\n”, x.object_id, x }
18357280 []
18357140 []
18357020 []
18356920 []
=> 4

You can look at it differently and say [] receives a list of values -
and that list may be empty:

irb(main):001:0> []
=> []
irb(main):002:0> [1, “foo”]
=> [1, “foo”]
irb(main):003:0> [:bar, 2+5]
=> [:bar, 7]

But generally there is no separate input needed for an expression to
produce output. A String literal also produces a new object but does
not receive any input:

irb(main):004:0> 4.times { puts(‘foo’.object_id) }
19555440
19555400
19555360
19555300
=> 4

You could argue that the characters between the quotes are input. But
this input is read at parse time. And it is not fed into the expression
but it is processed by the parser. It is not evaluated at runtime as
input of some expression evaluation because it is fixed for the whole
life time of the program.

Raja gopalan wrote in post #1147571:

Everything is clear to me,

Reading your question at the end I doubt it.

thank you very much and at last line you said:

“It has a different syntax for invocation than other functions.”

Can you give me the example of what this different syntax is? How this
different syntax look like?

Are you serious? See your first post in this thread.

Hi,

Everything is clear to me, thank you very much and at last line you said
:

“It has a different syntax for invocation than other functions.”

Can you give me the example of what this different syntax is? How this
different syntax look like?

hi,

I asked whether [] is a function call, but you said that’s an expression
which returns the value and then I asked what’s the difference between
function and expression and you said function would be created in the
side effect of ‘def’ and expression is anything it returns the value. I
understood correctly ,right? Or Am I missing something? Or Are you meant
to say the different kind of syntax to create expression here is this
a=[]?

“It has a different syntax for invocation than other functions.”

Is this a=[]?

hi,

No, it’s “[]” or “[9,2,4,7]” whereas for any function it’s “fun”,
“fun()”, “fun 9,2,4,7” or “fun(9,2,4,7)”. “a={any expression}” is an
assignment.

This is what I had to confirm from you, Thank you. I am clear now.

Raja gopalan wrote in post #1147633:

I asked whether [] is a function call, but you said that’s an expression
which returns the value and then I asked what’s the difference between
function and expression and you said function would be created in the
side effect of ‘def’ and expression is anything it returns the value.

I said in Ruby everything is an expression because there are no
statements, i.e. things which do not have a resulting value.

I
understood correctly ,right? Or Am I missing something? Or Are you meant
to say the different kind of syntax to create expression here is this
a=[]?

Honestly, I lost track of the discussion. Your initial question was,
whether [] produces new objects or returns a predefined object. It was
demonstrated that it is the former.

Then you thought that an expression needs inputs to produce outputs
which I denied. Then you asked for a general definition of expression.
Here’s the Wikipedia version:

http://en.wikipedia.org/wiki/Statement_(programming)#Expressions

Then you asked whether function and expression are the same which I
denied and tried to explain in what ways both are related. I also
mentioned that everything you see written in Ruby code constitutes an
expression because everything yields a value. Maybe that was the
confusing bit.

Then you asked whether [] was a kind of function and where the
difference to a regular function invocation was and I said it’s just the
different syntax. And it’s the syntax you have been asking about right
in your first posting.

“It has a different syntax for invocation than other functions.”

Is this a=[]?

No, it’s “[]” or “[9,2,4,7]” whereas for any function it’s “fun”,
“fun()”, “fun 9,2,4,7” or “fun(9,2,4,7)”. “a={any expression}” is an
assignment.

With the current state of the discussion I suggest you get yourself a
textbook or other media introducing programming. That should help get
the basic concepts right.

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