<adam12>
mooff: Sometimes I like the module builder pattern. class Foobar < Module.
<adam12>
mooff: And sometimes I like to mod = Module.new; const_set(:some_const, mod); mod.define_method.
<mooff>
ooh, module builder sounds neat
<adam12>
I think there was a Rubyconf or Railsconf talk on it recently.
<mooff>
:: class Foobar < Module; def cool() end end; f = Foobar.new
<ruby-eval>
=> #<Foobar:0x04426a90>
<mooff>
:: f.respond_to? :cool
<ruby-eval>
=> true
<mooff>
:: Object.new.extend f
<ruby-eval>
=> #<Object:0x04435d10>
<mooff>
:: _.respond_to? :cool
<ruby-eval>
=> false
<mooff>
that's funky. you'd need some plumbing to define instance methods for the module, wouldn't you?
<adam12>
mooff: Yeah. Usually define_method inside initialize();
<adam12>
class IsAwesome < Module; def initialize(awesome = true); define_method(:awesome?) { awesome }; end; end; class Widget; include IsAwesome.new(true); end; Widget.new.awesome?
<mooff>
include AwesomeStruct.new(:ooh, :cool)
<mooff>
:: (0..Float::INFINITY).find { |i| rand(100).zero? }.then { |n| "Took #{n} times to roll a zero" }
<ruby-eval>
=> "Took 124 times to roll a zero"
<mooff>
that one's useful at times. especially if you .lazy.map { turn it into something you want }.find { }
<mooff>
now there's (0..) instead of (0..Float::INFINITY) .. ahh
Cena has joined #ruby
Cena has quit [Client Quit]
<ox1eef>
i like Module.new {} - great for creating modules on the fly, using const_set on the module is something i don't think i've done before, but interesting as well. 'class Foo < Module' is what crosses the line, too weird.
<ox1eef>
it is certainly clever code. in the past i've used it to group, or include - other modules which were named, then returning the anonymous module from a method. it has a nice style or way to it that would be great to see encouraged in ruby.
bluedust_ has joined #ruby
bluedust has quit [Ping timeout: 256 seconds]
gr33n7007h has quit [Ping timeout: 252 seconds]
oxfuxxx has joined #ruby
teclator has joined #ruby
oxfuxxx has quit [Ping timeout: 256 seconds]
<nakilon>
IIRC I used Module.new to split logically some sets of methods I put together into a project autotests library defined in spec_helper.rb
<nakilon>
there was no point in declaring a constant that won't even be used
<nakilon>
except of the immediate local inclusion
_ht has joined #ruby
Cena has joined #ruby
Cena has quit [Ping timeout: 256 seconds]
Rounin has joined #ruby
<ox1eef>
it can make for appealing syntax, eg: include Foo.bar
weaksauc_ has quit [Ping timeout: 272 seconds]
Guest3952 has joined #ruby
weaksauce has joined #ruby
bluedust has joined #ruby
bluedust_ has quit [Ping timeout: 240 seconds]
bluedust has quit [Remote host closed the connection]
TCZ has joined #ruby
taupiqueur has joined #ruby
gr33n7007h has joined #ruby
fercell has joined #ruby
ferr_ has joined #ruby
fercell has quit [Ping timeout: 256 seconds]
bluedust has joined #ruby
roadie has left #ruby [ERC (IRC client for Emacs 25.3.50.1)]
donofrio has joined #ruby
Guest2 has joined #ruby
Guest2 has quit [Client Quit]
Aylat has joined #ruby
Aylat has joined #ruby
Aylat has quit [Changing host]
Aylat_ has joined #ruby
Aylat_ has quit [Changing host]
Aylat_ has joined #ruby
Aylat has quit [Ping timeout: 272 seconds]
ikowasz has joined #ruby
Aylat_ has quit [Read error: Connection reset by peer]
ua_ has quit [Ping timeout: 256 seconds]
bluedust has quit [Remote host closed the connection]
<mooff>
so i have a method which writes to a socket, and takes a block to call when a reply is received later.. asynchronously
<mooff>
how might i turn that into a 'synchronous' enumerator?
<mooff>
e.g. take an array, and map each value to a reply, sequentially
<mooff>
sorry if i'm not describing it well. i feel like it should be possible with some technique, involving Enumerator and/or Fibers
<mooff>
but i can't figure out quite what
wand has joined #ruby
<nakilon>
what do you mean "a block to call asynchronously"? isn't socket read blocking in a loop?
<nakilon>
or do you mean you read sync but then call the block async? that's probably where you should wait for the block to finish before continuing the read
<weaksauc_>
mooff post some code of what you are trying?
<nakilon>
in my irc bot tests I don't care about syncing -- I just read the number of replies the test assumes to get and if all the tests pass then it's by 99.999% chance ok, if not then not
<mooff>
the ugly but working thing i got yesterday was:
<nakilon>
if there was not enough or excessive replies the tests would fail because of their assertions anyway
<nakilon>
if there are 100 tests the only way to miss the error is if there is an extra reply in the last test that is 1% chance
perrierjouet has quit [Quit: WeeChat 3.4]
<nakilon>
(assuming you reuse the connection)
<mooff>
to concretize.. i have a couple dozen bots running on different threads. they have a request method which takes an IRC command, gives it a label(*), and stores a block to be called when the server acknowledges that command
<mooff>
i wanted to take a folder of ASCII art... and print each piece of art using a random bot, waiting for one bot's art piece to complete before starting the next
<mooff>
i figured there's gotta be a way to use Fibers to do it without resorting to "sleep 0.01 until local_var_has_been_updated"
robotmay has quit [Quit: No Ping reply in 180 seconds.]
robotmay has joined #ruby
<ox1eef>
another thing you might be able to do is suspend thread(s) with Thread#{resume,stop}.
hack3r_v_ has joined #ruby
<ox1eef>
i seem to remember the code youve shared already using shared, globally accessible state - that mixed with threads doesn't sound too good.
<mooff>
for what it's worth, that isn't at play :-)
<mooff>
the hack i was exploring then doesn't seem practical, so only the bot running in the main thread sets / uses globals
<ox1eef>
well, any reason to try fibers is reason enough. i wish i had a use case for them.
TCZ has quit [Quit: Leaving]
<mooff>
i struggled to see one before, to be honest
<mooff>
"that's just blocks with extra steps"
<sam113101>
what's a good book on ruby for those who already know the language but are pretty rusty… I think I started on ruby 1.7 and haven't really kept up because I don't program much