last day (22 days later) » 

9:25 AM
Hi
I have something to discuss with you regarding Ruby...
 
 
3 hours later…
12:31 PM
Discuss away.
@ArupRakshit I guess I should highlight you.
 
@sepp2k Thanks..
I will
@sepp2k I posted the question here - ruby-forum.com/topic/4486908#new .. Didn't get any reply.. in SO, I like your Ruby answers always.. :-)
Hope you can illustrate it ...
 
12:48 PM
Is it possible that the gateway is broken? Because I don't see your question in the mailing list.
@ArupRakshit ^
 
@sepp2k may be.. there is a discussion topic - ruby-forum.com/topic/4486783#new I have followed some days back
I used the Ruby forum UI to ask questions there..
 
Yeah, seems like forum posts are no longer posted to the ML (though it still seems to work the other way around).
Anyway, the answer to your question is that the method names are the same as the class names whose instances they construct.
 
What is that pattern called.. ? I found some gem also used the same conventions
 
@ArupRakshit So having a method name start with capital letters is acceptable style if and only if that name is also a class name and the method's purpose is to construct instances of that class (and the initialize method already constructs instances in a different way).
 
ok.. Nice one... hold on
Let me try some example in my editor
I had a hope you know the answer..
class Foo
  def self.Foo
    #
  end
  def meth
    p 12
  end
end

Foo().meth

#'<main>': undefined method 'Foo' for main:Object (NoMethodError)
Nopes.. I am a dumb person.. What wrong I did...
?
May be I am close :
module Kernel
  def Foo
    #
  end
end

class Foo
  def meth
    p 12
  end
end

Foo().meth
# undefined method 'meth' for nil:NilClass (NoMethodError)
@sepp2k Why Foo() not giving the instance of Foo ?
 
1:05 PM
@ArupRakshit Because the method is empty.
 
should I need to put the #initialize method there ?
 
Giving the method a name with a capital letter is just a naming convention. It does not automatically make the method do something. You still need to give it a body.
@ArupRakshit Generally you'll have an initialize method that does one thing and a Foo() method that does another (by calling Foo.new(args) after constructing the arguments in an appropriate way).
 
Yes,,, I know.. That's how we used to construct objects of regular classes
But Kernel#Hash() made me a bit crazy, while I was thinking, why I need this, when I have {} or Hash::new etc.. ?
@sepp2k This is my actual hunt why so design ? Can I use it in any of my design or so ? If I can, how and what benefits it can give me.... etc :-)
 
Because Hash.new already does something else.
 
that's an example
 
1:12 PM
But that's the answer to your question.
 
You can take String#new and Kernel#String too
as an example
 
Defining Foo() to construct Foo objects makes sense iff Foo.new already does soemthing else.
 
Good point.. Great knock to my brain...
 
@ArupRakshit And I'll give you the same answer for String: String.new does one thing and String() does another.
 
If you have time, can you show an example, where Foo() is creating a object of class Foo. I just need to see, how would you define that method.. actually ?
 
1:18 PM
class MyArray
test
Ehrm... how do you write multi-line messages in Chat?
 
@sepp2k When you paste the code, you can see, at the extreme right, one button will come as Fixed Font, you just need to click on it.
 
I wasn't trying to paste, I was trying to write directly in the chat box.
Anyway, here's a pastie: codepad.org/nKa2NDpr
 
ok..
for future reference, here I am also keeping your super duper example :-
class MyArray
  def initialize(arr)
    @arr = arr
  end

  # ...
end

def MyArray(*arr)
  MyArray.new(arr)
end

arr1 = MyArray.new([1,2,3]) # @arr will be [1,2,3]
arr2 = MyArray([1,2,3]) # @arr will be [[1,2,3]]
Here, MyArray.new is calling the #initialize method...
right ?
as usually we used to see...
 
Right.
 
You have cleared my pain.... :)
@sepp2k any answer here - stackoverflow.com/questions/22832023/… from you ?
 
1:27 PM
My answer to the title would be: "never". That's probably not worth an actual answer though.
 
Why then Kernel module polluted with that method ? :-)
 
To imitate Perl.
 
@sepp2k How many languages you do know BTW ?
:-)
 
Depends on how you define "know". 5ish to write code in and about 20ish in the sense that I'd probably recognize the language if I saw code in that language.
 
Ooops!! I am kid to you.. but you helped me earlier also in lots of my confusions I can recall that.. Very helpful @sepp2k you are.. If I have doubt, I will come to you again..
 
 
7 hours later…
8:19 PM
@sepp2k Hi...
Array#sample doc last style is not clear to me..
Why the second argument, we need sample(n, random: rng) method... ?
 
8:40 PM
@ArupRakshit In case you want to use a different RNG than the default one.
 
wait.. I tried some arbitrary code.. in my vim..
none of those make sense...
let me show you those
@sepp2k Here are those samples -
a = (1..12).to_a
a.sample(2, rng: (1..3)) # => [10, 7]
a.sample(2, rng: (1..3)) # => [2, 4]
a.sample(2, rng: (1..3)) # => [7, 9]
first argument makes sense... not second argument... :(
 
Not sure why that doesn't cause an exception. The second argument is supposed to be an RNG (Random Number Generator), not a range.
 
here are some more :
a = (1..12).to_a
a.sample(2, rng: [1,2,4]) # => [5, 8]
I am really confused...
 
As I said: it's supposed to be an RNG. Not a range, not an array.
 
Ok.. Could you give me one example, where I can see, the second argument makes sense in the output, which I am not able to reproduce.
 
8:52 PM
@ArupRakshit r = Random.new; r2 = r.dup; a1 = a.sample(rng: r); a2 = a.sample(rng: r2); a1==a2
 
Let me try these..
when you want to wrtie multiline and use Alt+Enter.. when done only Enter.. :)
 
Ah, thank you. I tried ctrl+enter and shift+enter earlier, but not alt+enter.
 
I got your pain.. when I see your workaround.. I mean you used ;.. :)
I don't get the example -
a = (1..10).to_a
r = Random.new
r2 = r.dup
a1 = a.sample(rng: r)
a2 = a.sample(rng: r2)
a1 == a2 # => false
 
Wait, what? That's supposed to be true.
Let me check.
 
I mean sample would always give any random number for the array.. It may be true or false.. we can't predict..
But how does rng affect the result ? That I didn't get
 
8:59 PM
Ah, I see. It's random, not rng.
It just ignores keyword arguments it doesn't know.
If you use random: r and random: r2, it works.
Or not...
 
Interesting... Let me try again
You are awesome -
a = (1..10).to_a
r = Random.new
r2 = r.dup
a1 = a.sample(random: r)
a2 = a.sample(random: r2)
a1 == a2 # => true
How are you inventing, which didn't mention in documentation ?
:
:)
 
Hm? It did mention in the docs that it's called random.
 
not in a correct way.. see the bold line sample(n, random: rng) → new_ary
 
Yes, so the key's name is "random".
 
I thought random is a key, rng is a value of it..
 
9:04 PM
Yep, and that's true.
So if we want to pass r as the value we write random: r.
 
humm... Got it from your words,, but it could be mentioned as only val or something like that.. any way
point is sample, takes its second argument as keyword argument... If we use something like foo: 12 or rng: (1..2), it will ignore these.. Am I right ?
 
Right.
 
Do you have energy to hear my second doubt ? Or should I ask tomorrow ?
 
Go ahead.
 
give me a moment, let me then first write a code... to illustrate my confusions..
# working as expected
class Foo
  @@x = 12
  def self.bar
    p @@x
  end
end

class Bar < Foo
end

Bar.bar # => 12
I am having some hard time to understand why the below code is not
working, while the above.
module Foo
  @@x = 12
  def self.bar
    p @@x
  end
end

class Bar
  include Foo
end

Bar.bar # undefined method bar' for Bar:Class (NoMethodError)
 
9:14 PM
Yes, class methods aren't inherited from modules. Dunno why exactly - that's just the way it is.
What you can do is define bar as an instance method and then extend Foo rather than include Foo.
 
some time internal Ruby implementations are not symmetric ... :(
I asked ruby-forum.com/topic/4483424#new, no one told me the philosophy.. :(
This has been hard coded, for any hidden reasons..
 
9:29 PM
@sepp2k You can see in Ruby 2.1.1 we have Enumerable#to_h method exist
But... at the same time when I try below :
(arup~>~)$ irb --simple-prompt
>> (1..2).to_h
TypeError: wrong element type Fixnum (expected array)
	from (irb):1:in `each'
	from (irb):1:in `to_h'
	from (irb):1
	from /home/kirti/.rvm/rubies/ruby-2.1.0/bin/irb:11:in `<main>'
>>
Range is enumerable, then why error with #to_h ?
 
It's supposed to be an enumerable of key-value pairs (i.e. 2-element arrays), not of integers.
 
That I know like...
>> [[1,2]].to_h
=> {1=>2}
>>
It didn't mention... in doc.. Range being an enumerable, wouldn't work... even if Array#to_h does exist, thus [[1,2]].to_h worked I am sure
@sepp2k Any answer do you have for this ?
 
It has nothing to do with whether Array#to_h exists. Array#to_h does not need to exist because it can just use the Enumerable#to_h implementation.
The problem is merely that your range was of integers (and you can't have an enumerable range of arrays).
If you use some other enumerable of 2-element arrays, it will work just as well as with an array of arrays.
Likewise if you do [1,2].to_h it will fail just like it failed with the range.
 
wait,.....
see the error...
>> class Array
>> undef to_a
>> end
=> nil
>> [[1,2]].to_h
/home/kirti/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/irb/ruby-lex.rb:192:in `buf_input': undefined method `to_a' for #<Array:0x982d620> (NoMethodError)
	from /home/kirti/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/irb/ruby-lex.rb:105:in `getc'
	from /home/kirti/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/irb/slex.rb:206:in `match_io'
	from /home/kirti/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/irb/slex.rb:76:in `match'
	from /home/kirti/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/irb/ruby-lex.rb:290:in `token'
It proves that, [[1,2]] not using Enumerable#to_h... rather its own... Am I right ?
 
9:44 PM
No, it just proves that Enumerable.to_h calls to_a on the elements.
 
ohh... sorry :p
I did it wrongly..
 
Though, actually, it should have been the same error message with the range then.
 
let me refresh my code... again
 
Anyway I'm not saying that Array doesn't define it's own to_h (it might for performance reasons), just that it doesn't have to.
And that if it does, it's functionally equivalent to Enumerable#to_h
 
No... see now
(arup~>~)$ pry --simple-prompt
>> class Array
 |   undef to_h
 | end
=> nil
>> [[1,2]].to_h
NoMethodError: undefined method `to_h' for [[1, 2]]:Array
from (pry):4:in `__pry__'
>>
@sepp2k While the method is for specific class, what is the point to include it into enumerable module... not getting..
 
9:55 PM
@ArupRakshit But that's my point: it's not for a specific class. It works for any enumerable that contains 2-element arrays.
 
enumerable that contains 2-element array means ?
 
It means that the elements of the enumerable are 2-element arrays.
 
I understand.... but it also tells me receiver should be a array of array, where inner array must be having 2 elements...
 
No, not array of array. Enumerable of array.
If you have a set of arrays or an enumerator of array that works just as fine as an array of arrays.
 
Can you give an example of what you are saying... I am nervous :-)
 
10:00 PM
require 'set'
Set[[1,2],[3,4]].to_h
Alt+enter does not seem to work by the way.
 
sorry it is shift+ Enter
 
test
Yes, that works. Thanks.
 
But not the below :-
>> ([1,2]..[4,5]).to_h
TypeError: can't iterate from Array
from (pry):5:in `each'
>> ([1,2]..[4,5])
=> [1, 2]..[4, 5]
 
Ranges of arrays aren't enumerable. If you call .each on it, you get the same error.
 
Yes.. I know...
 
10:05 PM
Ranges are only enumerable if the elements support .succ
So it has nothing to do with to_h is my point.
 
Okay..
but not clear why did you force on Enumerable of array, but not on No, not array of array... I have to think,,, where these 2 things differ...
@sepp2k Last question for today ----
to_a(*args) → array - telling this method can take arguments... Is it really so ?
IRB objecting me..
 
It can take arguments if and only if each takes arguments.
The arguments would then just be forwarded to each.
 
(arup~>~)$ pry --simple-prompt
>> (1..3).to_a(2)
ArgumentError: wrong number of arguments (1 for 0)
from (pry):1:in `each'
>>
 
Exactly. As you see the arguments are forwarded to each.
 
Array#each never takes argument, but why then such API is given to us.. Why not only Array#to_a ?
 
10:13 PM
I don't understand the second part of the question.
Yes, Array#each does not take arguments, but other Enumerables' each methods can and do.
 
what are those others as an example ?
 
Specifically enumerators' each methods take arguments if the enumerator has been created from a method that takes arguments.
Example: (1..10).enum_for(:each_slice).to_a(3)
 
Wooww.. killed me... :)
hope same explanation holds on to_h(*args) → hash..
 
It does.
And incidentally (1..10).enum_for(:each_slice).to_h(2) is another example of where you can call to_h on something other than an array.
 
Thanks... and excellent
>> (1..10).enum_for(:each_slice).to_h(2)
=> {1=>2, 3=>4, 5=>6, 7=>8, 9=>10}
>>
what a one liner... :-)
>> (1..10).enum_for(:each_slice).to_a(3)
=> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
>>
I wouldn't disturb you anymore today... again tomorrow... I have long waited pains, which I will clear with the help of you..
:)
@sepp2k Bye,,,
 

  last day (22 days later) »