cmoneylulz has quit [Remote host closed the connection]
<ruby-lang616>
hey Ruby community, I have a question about hashes vs. arrays
<ruby-lang616>
Why use a hash vs. an array?
<ruby-lang616>
is it b/c they allow you to associate objects with other objects rather than being limited to just an integer with an object (array)
<ruby-lang616>
?
<red_menace>
depends on what you are doing, I suppose
weems|mac has joined #ruby-lang
<ruby-lang616>
but, I guess as a general topic in explaining what a hash is and then why to use one vs. an array?
<apeiros>
hashes allow you to use almost any object as a key, also they're "sparse". only keys you actually set are in it
deg- has quit [Ping timeout: 264 seconds]
<red_menace>
an array is an ordered collection of items that are accessed by their position (index) in the list
<apeiros>
arrays are positional. you access items by position.
<red_menace>
a hash is an unordered collection of items that are accessed by a key value
<apeiros>
an array is not sparse. if you do `x = []; x[1000000] = 1`, your array will contain 1000001 items.
<apeiros>
red_menace: small correction: ruby hashes are ordered
<apeiros>
they're, however, not meant for positional access. order is achieved through linking.
<ericwood>
yes
<ericwood>
iteration will preserve order
deg- has joined #ruby-lang
<red_menace>
by unordered I was referring to the key/value pairs not being in any specific order
<apeiros>
red_menace: I understand. but they are.
<apeiros>
they retain insertion order. which is a specific order.
<ericwood>
it's best not to assume order when working with ruby hashes, even though they are ordered
<ruby-lang616>
gotcha
<ericwood>
although there are certain circumstances where that's acceptable
<ruby-lang616>
so, I suppose the use of a hash vs. an array really depends on the data you want to store? If I want to store a list of students I could use an array, but if I want to store names of students associated with their grades, it seems to me like I would use a hash
<ruby-lang616>
so I could have a name (string) associated with a grade (string) and order doesn't really matter to me
<apeiros>
ruby-lang616: no. it depends on how you want to access the data you store.
<apeiros>
you can store any kind of objects in both.
<ericwood>
apeiros: you can use hashes as keys I found out recently
<ericwood>
ain't that messed up?
<ruby-lang616>
haha
<apeiros>
ericwood: as said, you can use almost every object
theharshest has quit [Quit: This computer has gone to sleep]
<apeiros>
the only requirement is #hash and #eql? being implemented properly
<ruby-lang616>
so when do you guys use hashes instead of arrays?
<ericwood>
when I'm storing data that's associated to some other value
<ericwood>
think of it like a phone book
<apeiros>
ruby-lang616: when I need to access my data by key, or when it's sparse
<ericwood>
I have a bunch of people with phone numbers? perfect use for a hash, assuming I'm looking up phone numbers by name
<ruby-lang616>
ah, I see
<ruby-lang616>
and then what would a prototypical use for an array be?
<ericwood>
a collection of potentially ordered data
jxpx777 has joined #ruby-lang
<apeiros>
a list of all users in an irc channel
<apeiros>
a list of all channels on an irc server
<apeiros>
a list of all messages sent in a channel
<ruby-lang616>
gotcha, there is a prime example to my right lol
<red_menace>
while a hash could be a collection of those
<ruby-lang616>
okay, I think it is becoming clear...thank you!
<apeiros>
an array of arrays of pixels of your picture
<ruby-lang616>
when you say a hash could be a collection of those...what would the key/value pair be in that case?
<apeiros>
nickname => user
<apeiros>
channelname => channel
<ruby-lang616>
gotcha
theharshest has joined #ruby-lang
<ruby-lang616>
thank you for your help with this
katlogic_ has joined #ruby-lang
matp has joined #ruby-lang
breezy_ has joined #ruby-lang
karamazov has quit [Remote host closed the connection]
theharshest has quit [Quit: This computer has gone to sleep]
iamcalledrob has joined #ruby-lang
banister has joined #ruby-lang
twright has quit [Ping timeout: 244 seconds]
mikecmpbll has quit [Quit: i've nodded off.]
tectonic has joined #ruby-lang
yubrew has quit [Ping timeout: 264 seconds]
Miphix has quit [Ping timeout: 248 seconds]
<arup_r1>
Why we need to defined `#respond_to?` while defining `#method_missing` in any of our custom class ?
jperry has quit [Read error: Connection reset by peer]
jperry has joined #ruby-lang
tectonic has quit []
postmodern has quit [Quit: Leaving]
jhass|off is now known as jhass
<brahmana>
arup_r1: I don't think it is stricktly required, but it makes sense to do so. With method_missing you are trying to handle to provide functionality which is not there in any of the existing defined methods.
<brahmana>
If a consumer of your class is very careful and calls methods only after responds_to returns true then your method_missing will never be called.
<arup_r1>
If you don't mind would you consider to give an example for the same ?
Lumio has joined #ruby-lang
banister has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
<Mon_Ouie>
arup_r1: You shouldn't redefine respond_to?, you should redefine respond_to_missing?
_ht has joined #ruby-lang
<Mon_Ouie>
Because that's what gets checked when you call Object#method
<arup_r1>
brahmana: makes sense.. create gist, so that I can bookmark it.. Now how would you implement #respons_to? so that it says `true` when the methods will be defined dynamically or will be handled by #method_missing ?
<arup_r1>
Mon_Ouie: Aww.. I was not aware of... People always says me to define #respond_to? not #respond_to_missing? .. I am bit confused now..
<brahmana>
arup_r1: Please read what Mon_Ouie said. He definitely knows more Ruby than me.
<Mon_Ouie>
def respond_to_missing?(symbol, include_all); symbol =~ regexp || super; end
<Mon_Ouie>
Where 'symbol =~ regexp' would be whatever condition you use in method_missing
<arup_r1>
Mon_Ouie: Thanks for your response... Could you create a gist, to explain this all.. so that I can bookmark and play with it... This concept sucks for me :(
<brahmana>
arup_r1: You can create a gist of anything you want yourself, isn't it?
<arup_r1>
Humm.. But still not clear to me .. how this 2 works together in one place.. so I am thinking if I see a code till the point, it
rippa has joined #ruby-lang
aero224 has quit [Remote host closed the connection]
<arup_r1>
is paining.. I can take it forward from there on..
mistym has quit [Remote host closed the connection]
<arup_r1>
Mon_Ouie: Got it.. Thanks
schaerli has joined #ruby-lang
<Mon_Ouie>
apeiros: Plus including private methods if include_all is true :)
iamcalledrob has joined #ruby-lang
bin7me has joined #ruby-lang
iamcalledrob has quit [Ping timeout: 252 seconds]
yubrew has joined #ruby-lang
Missphoenix has joined #ruby-lang
<arup_r1>
Mon_Ouie: In summary. what I understand is - if `respond_to?` is returns `true`, then `#respond_to_missing?` will not be called. But if `respond_to?` returns false, then `#respond_to_missing?` will be called, and if it returns true, then `#respond_to?` also will return true. Is my understanding correct ?
<jhass>
yes
<Mon_Ouie>
It's not if respond_to? returns true/false. It's if the first check inside respond_to? is true/false.
Miphix has quit [Ping timeout: 255 seconds]
zUriel_ has joined #ruby-lang
yubrew has quit [Ping timeout: 255 seconds]
<Mon_Ouie>
(You said 'if respond_to? returns false and respond_to_missing? returns true then respond_to? returns true', which is contradictory)
<arup_r1>
Ohh... God give me some gray matter ........... :-(
marr has joined #ruby-lang
* Mon_Ouie
gives arup_r1 a rock
<arup_r1>
Ok. first check is respond_to? , if it returns false, then respond_to_missing? will be called ... Is it correct understanding ?
<Mon_Ouie>
Yes. That first check being, 'has the method been defined through normal means?'
<arup_r1>
ok....
<arup_r1>
Now hold on
<arup_r1>
Now first check is #respond_to?... it returns false. check goes to #respond_to_missing? ........... if now this method returns true, then what will be the output of `#respond_to?` ?
<Mon_Ouie>
true
<arup_r1>
Ok.. let me lock it first inside my brain.. otherwise I will become again dumb.. :-)
drbrain has quit [Ping timeout: 248 seconds]
stardiviner has joined #ruby-lang
drbrain has joined #ruby-lang
twright has joined #ruby-lang
Lewix has joined #ruby-lang
thomasxie has quit [Read error: Connection reset by peer]
<arup_r>
Yes.. shared example is cleared... but *shared_context* not much...
<jhass>
as I understood it, while technically similar, they're basically opposing features. shared_context provides a common context for different examples, while shared_examples provides common examples for different contexts
ruby-lang648 has joined #ruby-lang
<arup_r>
ahh.. I will think about your this sentence,,,
Guest27760 has quit [Quit: My MacBook Pro has gone to sleep. ZZZzzz…]
<arup_r>
It has answer I think..
ruby-lang648 has quit [Client Quit]
thomasxie has quit [Ping timeout: 240 seconds]
rubyaw has joined #ruby-lang
bin7me has quit [Remote host closed the connection]
dangerou_ has joined #ruby-lang
sonander has joined #ruby-lang
dangerousdave has quit [Ping timeout: 248 seconds]
dangerou_ has quit [Read error: Connection reset by peer]
dangerousdave has joined #ruby-lang
theharshest has quit [Quit: This computer has gone to sleep]
oste has joined #ruby-lang
theharshest has joined #ruby-lang
theharshest has quit [Client Quit]
iamcalledrob has quit [Quit: Computer has gone to sleep.]
Phoenixmiss has quit [Quit: Leaving]
havenwood has joined #ruby-lang
iamcalledrob has joined #ruby-lang
theharshest has joined #ruby-lang
iamcalledrob has quit [Ping timeout: 272 seconds]
AKASkip has joined #ruby-lang
nathanstitt has joined #ruby-lang
x0f has quit [Ping timeout: 260 seconds]
x0f has joined #ruby-lang
theharshest has quit [Quit: This computer has gone to sleep]
snoopybbt has quit [Remote host closed the connection]
snoopybbt has joined #ruby-lang
Xzyx987X has joined #ruby-lang
relix has quit [Quit: My MacBook Pro has gone to sleep. ZZZzzz…]
dm78 has quit [Remote host closed the connection]
Xzyx987X_ has quit [Ping timeout: 272 seconds]
relix has joined #ruby-lang
snoopybbt has quit [Remote host closed the connection]
iamcalledrob has joined #ruby-lang
Lumio has joined #ruby-lang
mehlah has joined #ruby-lang
mehlah has quit [Client Quit]
dangerousdave has quit [Read error: Connection reset by peer]
dangerousdave has joined #ruby-lang
relix has quit [Ping timeout: 252 seconds]
iamcalledrob has quit [Ping timeout: 264 seconds]
Lewix has quit [Remote host closed the connection]
weems|mac has joined #ruby-lang
rahul_j has quit [Quit: rahul_j]
rahul_j has joined #ruby-lang
havenwood has quit [Remote host closed the connection]
rahul_j has quit [Client Quit]
schaerli has joined #ruby-lang
relix has joined #ruby-lang
havenwood has joined #ruby-lang
sarkyniin has quit [Ping timeout: 272 seconds]
relix has quit [Ping timeout: 264 seconds]
sarkyniin has joined #ruby-lang
dangerou_ has joined #ruby-lang
dangerousdave has quit [Read error: Connection reset by peer]
touzin has joined #ruby-lang
postmodern has joined #ruby-lang
mistym has joined #ruby-lang
<arup_r>
I never read Fiber http://ruby-doc.org/core-2.1.2/Fiber.html . I will start it straight from the doco.. Before starting it if I can get a backgorund of the area of application of this concept, it will be helpful for me to read keeping those facts inside mine, so that I can relate ...... Can anyone give a brief intro on it ?
MindfulMo is now known as MindfulMonk
<jhass>
I yet have to see fibers used in production code
mconnolly has joined #ruby-lang
mconnolly has quit [Client Quit]
nathanstitt has quit [Quit: I growing sleepy]
yubrew has joined #ruby-lang
Lewix has joined #ruby-lang
<arup_r>
jhass: I never have seen any code on this.. But I am interested to know this topic too...
<arup_r>
havenwood: I gave it a look, went above my head ........... :(
<havenwood>
arup_r: My fault, bad example.
iamcalledrob has joined #ruby-lang
simoz1111114 has joined #ruby-lang
simoz1111115 has joined #ruby-lang
dangerou_ has quit [Read error: Connection reset by peer]
dangerousdave has joined #ruby-lang
iamcalledrob has quit [Ping timeout: 240 seconds]
<havenwood>
arup_r: Fibers spawn faster with less memory but they come with their own drawbacks.
simoz1111114 has quit [Ping timeout: 248 seconds]
<havenwood>
arup_r: Might be worth looking at a little bit higher layer abstractions for concurrency in Ruby. The `concurrent-ruby` gem has some great examples of stuff like agents, actors, futures, etc: https://github.com/ruby-concurrency/concurrent-ruby#readme
theharshest has quit [Quit: This computer has gone to sleep]
theharshest has joined #ruby-lang
<havenwood>
arup_r: Or practical uses of Threads/Fibers with Celluloid, Celluloid::IO and friends.
theharshest has quit [Client Quit]
apeiros has joined #ruby-lang
<arup_r>
havenwood: thanks,, I will try to understand those..
<havenwood>
arup_r: ^ 100 Fibers in an Array. This is a terrible example. >.>
<havenwood>
Since they only yield once.
<havenwood>
arup_r: Anyways, suffice it to say you schedule them, they can resume from one another back and forth. Multiple yields. Just play with them and you'll get a feel for it.
brianpWins has quit [Quit: brianpWins]
|jemc| has joined #ruby-lang
relix has joined #ruby-lang
<arup_r>
havenwood: I bookmarked it... Please don't delete the gist.... :-) I will go from there... If any issue I will let you know..''
<arup_r>
If any more example comes into your mind.. just throw them to me.. Let me get some pain.. :-)
thmzlt has quit []
yubrew has joined #ruby-lang
sharpmachine has joined #ruby-lang
<apeiros>
arup_r: just fork the gist then
yubrew has quit [Ping timeout: 260 seconds]
nathanstitt has quit [Quit: I growing sleepy]
<arup_r>
apeiros: What is the help of it ?
twright has quit [Ping timeout: 240 seconds]
<apeiros>
arup_r: if you fork it, the fork is yours. nobody but you can delete it.
<arup_r>
Ohhh :-p good idea.
nathanstitt has joined #ruby-lang
ledestin has quit [Ping timeout: 248 seconds]
scmx has joined #ruby-lang
ledestin has joined #ruby-lang
<arup_r>
apeiros: Done.. I was not aware of it... Thanks
<havenwood>
arup_r: updated gist with a fibonacci sequence fiber
<havenwood>
hem, what are other trivial things to implement with Fibers that don't make sense? :P
<arup_r>
havenwood: I learned some times back a new thing *fork* . Let me do it again..
hackeron_ is now known as hackeron
hackeron has quit [Changing host]
hackeron has joined #ruby-lang
theharshest has joined #ruby-lang
<arup_r>
If any more example comes to your mind, just add it.. these are really good exercise for the newbies like me.. :-)
<arup_r>
Guys... If you have any idea, a short code which can be implemented using Fiber.. just give me that to help me on this topic..
theharshest has quit [Client Quit]
<havenwood>
arup_r: Just heading out the door but I'll try to add a more realistic example later.
<arup_r>
havenwood: How would you notify me ?
theharshest has joined #ruby-lang
<havenwood>
hem, i dunno if you can watch a gist? follow me on githubs? gotta run! later
<arup_r>
I am here for next 30 mins... Its too late in India
<arup_r>
Ok
havenwood has quit []
<arup_r>
Yes.. I forked your gist, so I have the link of the original gits.. I can see it later too.. no issue
snoopybbt has joined #ruby-lang
<arup_r>
Do anyone have any more idea as an example using Fiber ?
iamcalledrob has joined #ruby-lang
nofxx has joined #ruby-lang
iamcalledrob has quit [Ping timeout: 248 seconds]
theharshest has quit [Quit: This computer has gone to sleep]
breezy_ has quit [Remote host closed the connection]
aero224 has joined #ruby-lang
breezy_ has joined #ruby-lang
sonander has quit [Ping timeout: 240 seconds]
scmx has quit [Ping timeout: 272 seconds]
schaerli has quit [Remote host closed the connection]
schaerli has joined #ruby-lang
rippa has quit [Quit: {#`%${%&`+'${`%&NO CARRIER]
yubrew has joined #ruby-lang
<arup_r>
In Rspec what is the difference between allow_any_instance_of and expect_any_instance_of ?
<jhass>
expect raises if the call doesn't happen
nathanstitt has quit [Quit: I growing sleepy]
vintik has joined #ruby-lang
rau has quit [K-Lined]
nathanstitt has joined #ruby-lang
yubrew has quit [Ping timeout: 264 seconds]
lolmaus has joined #ruby-lang
karamazov has joined #ruby-lang
amerine has joined #ruby-lang
symm- has joined #ruby-lang
dangerousdave has quit [Read error: Connection reset by peer]
<arup_r>
jhass: allow ?
<jhass>
doesn't
sdouglas has joined #ruby-lang
dangerousdave has joined #ruby-lang
snoopybbt has quit [Quit: leaving]
snoopybbt has joined #ruby-lang
<arup_r>
jhass: Cool ... exactly like that.. Just tested
<arup_r>
Any more difference ?
sonander has joined #ruby-lang
sdouglas has quit [Remote host closed the connection]
sdouglas has joined #ruby-lang
cored has quit [Ping timeout: 264 seconds]
<arup_r>
jhass: would you tell me. when people think of to use allow_* and when the expect_* ?
<jhass>
allow is for when you pass doubles into the code you test and expect is for when you expect your code to make a specific call on that double
<jhass>
same holds true for a external dependency you stub out