python - Why is it "array.include? object" and not "object.in? array"? -
i discovered in python, can this:
array = [1, 2, 3, 4] if 3 in array: print("yep!")
then, thought myself: "mh, why different in ruby? if 3 in array
more readable if array.include? 3
." then, realized, ruby pure oop , approach keyword-based.
but still, wondering. if python approach not oop, why can't there another, shorter way in ruby more readable? when thinking, don't think "does list include element?", "is element in list?".
let's assume, next code possible:
array = [1, 2, 3, 4] if 3.in? array print "yep! end
i see turn-around list.method(element)
element.method(list)
. so, wondering: which ruby principles/rules speak against above-metioned code?
edit: oops, wrote "keyboard-based" meant of course of study "keyword-based". emphasize this: not looking methods behave in? method; looking reasons why not implemented in ruby way.
which ruby principles/rules speak against above-metioned code?
it's not ruby principle, rather general oop principle of encapsulation: fixnum class should not need know arrays. however, because array's primary responsibility contain collections of objects, #in?
or #include?
falls under array's responsibility.
python ruby arrays coding-style
No comments:
Post a Comment