tags:

views:

84

answers:

3
class ArrayMine < Array

  def join( sep = $,, format = "%s" )

    collect do |item|
      sprintf( format, item )
    end.join( sep )
  end

end

=> rooms = ArrayMine[3, 4, 6]    #i couldn't understand how this line works

print "We have " + rooms.join( ", ", "%d bed" ) + " rooms available."

i have tried the same with String, but it comes up with an error.

    thank you...
+1  A: 

For String, change %d bed to %s bed

irb(main):053:0> rooms = ArrayMine["a","b","c"]
=> ["a", "b", "c"]

irb(main):055:0> print "We have " + rooms.join( ", ", "%s bed" ) + " rooms available."
We have a bed, b bed, c bed rooms available.=> nil
S.Mark
+3  A: 

ArrayMine inherits from Array and you can initialize Ruby arrays like that.

>> rooms = Array[3,4,6]
=> [3, 4, 6]

is the same as

>> rooms = [3,4,6]
=> [3, 4, 6]

Also the quite strange looking def join( sep = $,, format = "%s" ) is using the pre-defined variable $, that is the output field separator for the print and Array#join.

It could also have been done like this

rooms=["b",52,"s"]
print "We have " + rooms.map{|s| s.to_s+" bed"}.join(", ") + " rooms available."

The reason you can't do what you are trying to do with String is because assignment is not a class method but [] on Array is. Just new it instead.

>> s = Substring.new("abcd")
=> "abcd"
>> s.checking_next
=> "abce"

You can't override assignment in Ruby, it's only so that setter methods looks like assignment but they are actually method calls and as such they can be overridden. If you are feeling like being tricky and still want a similar behaviour as a=SubArray[1,2,3] you could create a << class method something like this:

class Substring < String
  def next_next()
    self.next().next() 
  end
  def self.<<(val)
    self.new(val)
  end
end 

>> sub = Substring<<"abcb"
=> "abcb"
>> sub.next_next
=> "abcd"
>> sub<<" the good old <<"
=> "abcb the good old <<"
>> sub.class<<"this is a new string"
=> "this is a new string"
Jonas Elfström
yep thanks.... but why the same is not working with string...i created sub class of string and tried to initialize,it doesn't work....... thanks man (string = Subclass_of_String"hello") it works fine with String class..
Please show the string code that is not working.
Jonas Elfström
i took this from why's poignant guide to ruby.... its really awesomeu can find lots of strange examples like this...
class Substring < String; def checking_next() self.next() end;end; # i have just passed the arguments same as that of array; obj = Substring"a"; p obj.checking_next;
You have no method call between `Substring` and `"a"` and there is no method call to override for assignments in Ruby. Setter methods looks a whole lot like an assignment (using = and all) but they are still just method calls and as such they can be overridden.
Jonas Elfström
+1  A: 

You're just invoking a [] class method:

class Spam
  def self.[]( *args )
    p args
  end
end
>> Spam[3,4,5]
[3, 4, 5]
jleedev
yeah...thanks man
@gsmuralee If the question has been answered, could you click the check on the best answer?
Tim Snowhite