Last active
April 4, 2016 19:21
-
-
Save piotrdz/f63aa302cb0bfbf7e54b5c4414d3f6a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# An example to illustrate a problem with splat operator when using different Ruby versions | |
# When run under Ruby 2.1.5 the output is: | |
# | |
# Passing in arguments: | |
# access_control: 123 | |
# (splat array): [:type, #<ToHashableClass:0x007fbf1c85d4b8 @hash={1=>2, 3=>4}>] | |
# | |
# ToHashableClass#to_hash method called | |
# | |
# Received arguments: | |
# access_control: 123 | |
# type: :type | |
# resource: #<ToHashableClass:0x007fbf1c85d4b8 @hash={1=>2, 3=>4}> | |
# status: 200 | |
# meta_data: {} | |
# When running under Ruby 2.3.0 the output is: | |
# | |
# Passing in arguments: | |
# access_control: 123 | |
# (splat array): [:type, #<ToHashableClass:0x007fa7bb0cc898 @hash={1=>2, 3=>4}>] | |
# | |
# ToHashableClass#to_hash method called | |
# | |
# Received arguments: | |
# access_control: 123 | |
# type: :type | |
# resource: {1=>2, 3=>4} | |
# status: 200 | |
# meta_data: {} | |
class SomeClass | |
def initialize(access_control, type, resource = [], status: 200, **meta_data) | |
puts "Received arguments:" | |
printf " access_control: "; p access_control | |
printf " type: "; p type | |
printf " resource: "; p resource | |
printf " status: "; p status | |
printf " meta_data: "; p meta_data | |
end | |
end | |
class ToHashableClass | |
def initialize | |
@hash = { 1 => 2, 3 => 4} | |
end | |
def to_hash | |
puts "ToHashableClass#to_hash method called" | |
puts "" | |
@hash | |
end | |
end | |
def create_with_splat(*args) | |
access_control = 123 | |
puts "Passing in arguments:" | |
printf " access_control: "; p access_control | |
printf " (splat array): "; p args | |
puts "" | |
SomeClass.new(access_control, *args) | |
end | |
type = :type | |
resource = ToHashableClass.new | |
create_with_splat(type, resource) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment