Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistency in behaviour for Rantly(number) { ... } #77

Open
linhpham opened this issue Apr 18, 2021 · 1 comment
Open

Inconsistency in behaviour for Rantly(number) { ... } #77

linhpham opened this issue Apr 18, 2021 · 1 comment

Comments

@linhpham
Copy link

Hi Rantly team,

I found an inconsistency in the behaviour or Rantly(number) { ... }.

The documentation said:

Rantly(5) { integer } # same as Rantly.map(5) { integer }
=> [-1843396915550491870, -1683855015308353854, -2291347782549033959, -951461511269053584, 483265231542292652]

So I expect Rantly(number) { ... } to always return an array, however Rantly(0) { ... } and Rantly(1) { ... } will only return the element and not wrapped in an array.

Please refer to this console output:

irb(main):001:0> require 'rantly'
=> true
irb(main):002:0> Rantly(0) { string }
=> "e7j1\"6"
irb(main):003:0> Rantly(1) { string }
=> "6%H)H."
irb(main):004:0> Rantly(2) { string }
=> [".3*m|m", "\\*'#w+"] 
irb(main):005:0> Rantly.map(0) { string }
=> []
irb(main):006:0> Rantly.map(1) { string }
=> ["jmdPnP"]
irb(main):007:0> Rantly.map(2) { string }
=> ["dO?Ndy", "w !\\7j"]
irb(main):008:0>

I found that the Rantly.map(number) { ... } is more consistent.

Thanks.

@abargnesi
Copy link
Member

Hi @linhpham. That is a good observation.

The behavior of Rantly() depends on the number value as you have demonstrated. Here is the method:

rantly/lib/rantly.rb

Lines 9 to 15 in 4b6ce32

def Rantly(n = 1, &block)
if n > 1
Rantly.map(n, &block)
else
Rantly.value(&block)
end
end

Rantly.map() would be a best practice if you always expect Array return values. The Rantly.value() method can be used to return the single value when that's what you expect.

Note: Rantly {} can be useful when it's clear what you're returning and you prefer the terseness:

Rantly {
  [
    dict {
      [string, integer]
    },
    dict {
      [string, integer]
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants