1

Lately I was developing some code using Backbone.js and Coffeescript, and got pretty much used to Coffeescripts built in methods to access stuff passed as objects:

{ firstname, lastname, @email } = options

Which is equivalent of:

firstname = options.firstname
lastname = options.lastname
@email = options.email

Question

Is there any built in Ruby syntax for achieving same behaviour on Ruby's hashes?

What I've managed to achieve so far is this:

firstname, lastname, @email = params.values_at(:firstname, :lastname, :email)

But it isn't a 1:1 solution of problem.

Edit #1

JS generated by Coffeescript:

var firstname, lastname;

firstname = options.firstname, lastname = options.lastname, this.email = options.email;
5
  • What is the equivalent Javascript produced by the Coffeescript compiler? Commented Nov 24, 2014 at 9:49
  • @DouglasFShearer added as edit in question Commented Nov 24, 2014 at 9:52
  • I hope you do know the difference between Ruby and CoffeeScript, because both are completely different than each other. Both languages have different semantic and syntax structures. I don't see any point of looking for 1:1, Ruby code you have shown in question looks just fine to me. Commented Nov 24, 2014 at 10:32
  • @Surya As Ruby strongly influenced Coffeescript I was hoping that there is a 1:1 equivalent. But yes I'm aware of differences, I'm just looking for solution if it exists. Commented Nov 24, 2014 at 10:42
  • @jadrol : As far as I know, there isn't any way in Ruby like you have in coffescript. Commented Nov 24, 2014 at 10:46

1 Answer 1

2

Ruby doesn't have any built-in solution; your solution is the right way to do it.

firstname, lastname, @email = params.values_at(:firstname, :lastname, :email)

If you happen to know the hash is an ordered hash, and contains just the values you want, and in the order you want, then you can get all the values like this:

# Suppose params = {
#    firstname: …, 
#    lastname: …,
#    email: ….
# }
firstname, lastname, @email = params.values

As a general hint, if you happen to know the hash is an ordered hash, and contains more entries than the values you want, yet the values are in the order you want, then you can get all the values and skip the ones you don't care about. Use the "useless" underscore variable as a placeholder as many times as you want, to skip an omittable value:

# Suppose params = {
#    firstname: …, 
#    middlename: …,
#    lastname: …,
#    bithdate: …,
#    email: …, 
#    whatever: …
# }
firstname, _, lastname, _, @email, _  = params.values
Sign up to request clarification or add additional context in comments.

3 Comments

Consider you have: params = { firstname: 'A', lastname: 'Z', email: '[email protected]', country: 'USA' }, if you do: firstname, lastname, _, _, _, @email = params.values you will have @email set to nil. which won't be a desired output. So, please mention that you must have :firstname, :lastname, :address, :state, :country, :email in an order inside params hash for your example to work.
I think that @joelparkerhenderson made a good point and you didn't understand him. He only proposed "useless" underscores to skip omittable values. It was general hint, currently not present in my examples.
@Surya Yes, I will mention that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.