0

Below is my method level code.

def create_todo_lists(options={})
    options[:title] ||= "My todo List"
end

Here is how i am passing.

create_todo_lists title: ""

I am not able to understand how my title arg is used in the method. Can anyone explain me on how the method accepts the title argument Can anyone write an equivalent JS code for the same, i understand JS better.

theJava
  • 14,620
  • 45
  • 131
  • 172
  • 1
    There is not such thing as `title` in your code. There is `:title`, and that is not an argument. It is a key of a hash with its braces omitted, which is the argument to `create_todo_lists`. – sawa Mar 19 '14 at 20:05
  • @theJava please explain what exactly you don't understand about how `title` is being used in our method. Be specific. –  Mar 19 '14 at 20:43
  • @Cupcake: how does the title argument passed turns into options[:title] – theJava Mar 19 '14 at 20:46

3 Answers3

1

Your method is currently taking in a hash

In your example, you are currently sending in the hash

{ title: '' }

the options[:title] ||= "My todo List" is checking for the existence of the key :title, if it is there... do nothing. if it isn't, set it to 'My todo List'

You are passing in an empty string as :title, which is not equal to nil.

So your example will do nothing.

You could do:

create_todo_lists title: nil

or

create_todo_lists {}

or

create_todo_lists

All of those will result in:

{ title: 'My todo List' }
1

It's just a optional parameter that defaults to an empty hash.

It's a little hack that's used in the Ruby world to provide a bunch of optional parameters at once. Very useful when using templating DSLs, this way you can add attributes to html tags and the like.

As for a JavaScript example, I would probably refer to this answer: Multiple arguments vs. options object. Except the options parameter is optional.

Community
  • 1
  • 1
Azolo
  • 4,353
  • 1
  • 23
  • 31
1

If we try to analyze your question and in particular your case there are few important concepts.

  • default value

def create_todo_lists(options={})

options={} here signifies that if at all when calling this function no argument is passed, it will get a parameter named options which will be an emty Hash.

  • optional parenthesis

create_todo_lists title: ""

in this line method create_todo_lists is called without using parenthesis around, since parenthesis are optional in a Ruby method call, it's a valid syntax and is similar to

create_todo_lists(title: "")
  • hash with single key value pair In ruby a Hash with single key-value pair also has parenthesis optional therefore in your case

title: "" and {title: ""}

are similar.

  • two different syntax for Hash Ruby has two different syntax when using symbol as a Hash key therefore in your case

title: "" and :title => ""

are very much similar.

one more interesting concept in your scenario is

  • ||= operator

which is also pretty unique in Ruby. This operator assigns right side value to left side variable only if value of left side variable is false or nil.

Therefore in your scenario create_todo_lists method is getting a Hash as argument but covering few very important concepts of Ruby.

Hope this helps. :)

Ashish Chopra
  • 1,413
  • 9
  • 23