solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 228 forks source link

problem with named arguments in ruby 2.0 (unknown keyword exception) #238

Closed assembler closed 10 years ago

assembler commented 10 years ago

I'm having strange issue with virtus. If i want to pass virtus object to method which has mixture of required and optional named parameters, it throws an error. Here is the reproduction of the bug:

require 'rubygems'
require 'virtus'

class StructPerson < Struct.new(:name)
end

class VirtusPerson
  include Virtus::Model
  attribute :name
end

def greet_person(person, scope: 'World')
  puts "Hi #{person.name}. You are the best in #{scope}!"
end

struct_person = StructPerson.new('John')
virtus_person = VirtusPerson.new(name: 'John')

greet_person struct_person
greet_person virtus_person

This will output:

Hi John. You are the best in World!
virtus-error.rb:20:in `<main>': unknown keyword: name (ArgumentError)

Somehow greet_person virtus_person translates into greet_person name: 'John'.. The way to get around this error is to call method like this: greet_person virtus_person, {}

elskwid commented 10 years ago

Hi @assembler,

Can you tell me what version of Ruby you're using? I just tried this on both ruby-2.0.0-p247 and ruby-2.1.0 with no errors.

Thanks!

solnic commented 10 years ago

It's probably unrelated but please use include Virtus.model as Virtus::Model is private API.

assembler commented 10 years ago

I was using ruby 2.0.0p195. Yep it was due to ruby version! Thanks for help!

solnic commented 10 years ago

@assembler ok cool :) happy virtus'ing ;)

elskwid commented 10 years ago

Good to know! (I wonder if it was erroneously judging the Virtus object to be a hash...)