How would you allow variable from specific list of Fixnum?

I have:
def initialize(type= nil,location=nil,mode=17)

and the mode can be one of these: 1,4,17,20
but… I want that If anyone tries to use other number then these he will
get exception error.

how would you implement it?
using raise?
I was thinking about:

modes = [1, 4, 17, 20]
raise SyntaxError.new(“Modes Allowed: #{modes}”) if !modes.index(mode)

Thanks,
Eliezer

class ModeError < Exception
end

class Dog
@modes = [1, 4, 17, 20]

def self.modes
@modes
end

def initialize(type=nil, location=nil, mode=17)
raise ModeError if not self.class.modes.include?(mode)
end
end

Dog.new(‘x’, ‘y’, 21)

–output:–
1.rb:13:in initialize': ModeError (ModeError) from 1.rb:17:innew’
from 1.rb:17:in `

I’d use ArgumentError instead of SyntaxError.

2012/9/21, 7stud – [email protected]:

from 1.rb:17:in `


Posted via http://www.ruby-forum.com/.


Wysłane z mojego urządzenia przenośnego

– Matma R.

On 9/21/2012 6:18 PM, 7stud – wrote:

Because that syntax is dumb?

If there is not difference in the operation such as in:
newregex = /[\d]+/
newregex = Regexp.new("[\d]+")

so there is not really a good reason to use one or the other since the
result is the same.

Thanks,
Eliezer

Hi,

Why is everybody using this Java-like exception syntax nowadays? Is
there some book teaching this?

Use

raise SyntaxError, “Modes Allowed: #{modes}”

not

raise SyntaxError.new(“Modes Allowed: #{modes}”)

On 9/21/2012 10:05 PM, Brandon W. wrote:

It’s a matter of efficiency and clarity. Besides, regex is actually:

Test =~/t/

Class: Regexp (Ruby 1.9.3)

Lo and behold, the docs have imparted divine wisdom on such subjects of
implementation.

Brandon W.
Thanks.

I ment something else.

Yes they do.

Thanks again,
Eliezer

On Sep 21, 2012, at 1:48 PM, Eliezer C. [email protected]
wrote:

Eliezer


Eliezer C.
https://www1.ngtech.co.il
IT consulting for Nonprofit organizations
eliezer ngtech.co.il

It’s a matter of efficiency and clarity. Besides, regex is actually:

Test =~ /t/

Lo and behold, the docs have imparted divine wisdom on such subjects of
implementation.

Brandon W.

On 22 September 2012 05:05, Brandon W. [email protected]
wrote:

It’s a matter of efficiency and clarity. Besides, regex is actually:

Test =~ /t/

Class: Regexp (Ruby 1.9.3)

Lo and behold, the docs have imparted divine wisdom on such subjects of
implementation.

Brandon W.

#=~ is the test-using-this-regex operator, #= is the assignment
operator. Eliezer was, I think, completely right in saying that
newregex = /[\d]+/ and newregex = Regexp.new("[\\d]+") are
equivalent, but one the is “better” (cheaper, faster, cleaner,
whatever) than the other. This is analogous to the choice between
using raise SyntaxError, "foo bar" and raise SyntaxError.new "foo bar".


Matthew K., B.Sc (CompSci) (Hons)
http://matthew.kerwin.net.au/
ABN: 59-013-727-651

“You’ll never find a programming language that frees
you from the burden of clarifying your ideas.” - xkcd

Eliezer C. wrote in post #1077009:

If there is not difference in the operation such as in:
newregex = /[\d]+/
newregex = Regexp.new("[\d]+")

so there is not really a good reason to use one or the other since the
result is the same.

In the first, you are using a Regexp literal. This is immutable, and you
will get the same object_id each time round a loop.

In the second, inside Regexp.new you are using a string literal - which
is mutable and gets a different object_id each time round a loop. And
therefore a new Regexp has to be built each time, because Ruby doesn’t
know that the String actually has the same contents.

3.times { puts /\d+/.object_id; puts Regexp.new("/\d+/").object_id }
2165193160
2165191640
2165193160
2165191560
2165193160
2165191480

On Fri, Sep 21, 2012 at 5:50 AM, Eliezer C. [email protected]
wrote:

modes = [1, 4, 17, 20]
raise SyntaxError.new(“Modes Allowed: #{modes}”) if !modes.index(mode)

I’d use one of

class Foo
VALID_MODES = [1, 4, 17, 20].freeze
VALID_MODES = [1, 4, 17, 20].to_set.freeze

def initialize(type = nil, location = nil, mode = 17)
raise ArgumentError, “Invalid mode: #{mode.inspect}” unless
VALID_MODES.include? mode
end

def initialize(type = nil, location = nil, mode = 17)
raise ArgumentError, “Invalid mode: %p” % mode unless
VALID_MODES.include? mode
end

def initialize(type = nil, location = nil, mode = 17)
raise ArgumentError, “Invalid mode: %p” % mode unless
VALID_MODES.include? mode
end
end

The first one is a tad more robust since in case of Array arguments
the second one (which I find nevertheless the most elegant one) will
only report the first element. The third one is a kind of compromise.

But in any case I’d use a constant for the array or set of valid
modes. As a variant you could use an integer as bitset:

class Foo
VALID_MODES = [1, 4, 17, 20].inject(0) {|set, n| set | 1 << n}

def initialize(type = nil, location = nil, mode = 17)
raise ArgumentError, “Invalid mode: %p” % [mode] unless
VALID_MODES[mode] == 1
end
end

Kind regards

robert