tags:

views:

143

answers:

3

Ramaze.start :port => 80

If my understanding is correct, the line above is a method call in Ruby and you could also write it as:

Ramaze.start(:port => 80)

But in either case, what does it mean when you put the => character between the symbol :port and the number 80?

Is that a way of creating a Hash?

When the Ramaze.start method receives the method parameters, what is the type of the argument?

Is it received as a single argument key-value pair?

Or it received as two arguments: :port and 80?

+8  A: 

It creates a hashmap where the symbol :port is the key and the value is 80.

Daniel
A: 

It's called a lambda. It's a short way of declaring a method. For instance, I can say myBirthday => myBirthday.Where(some value). Notice how you are passing in the object itself into the shortened function. In your case, you're passing in a port, giving it a value of 80, then returning the port object.

Austin
+4  A: 

When you have a method where the last parameter is a hash, the braces are optional.

Example:

def foo options
  # ...
end

Then this is allowed:

foo :bar => :baz

But if you have:

def foo options, say_hi
  # ...
  if say_hi
    puts 'Hi!'
  end
end

Then you cannot do:

foo :bar => :baz, true

But you'd have to call it like:

foo({:bar => :baz}, true)

I can't figure out why the parentheses are required in that instance, though, but they seem to be.

devbanana