havenwood changed the topic of #ruby to: Rules: https://ruby-community.com | Ruby 3.2.2, 3.1.4, 3.0.6, 3.3.0-preview1: https://www.ruby-lang.org | Paste 4+ lines to: https://gist.github.com | Books: https://t.ly/9ua4 | Logs: https://libera.irclog.whitequark.org/ruby/
roadie has joined #ruby
crespire has quit [Killed (NickServ (GHOST command used by crespire1!crespire@user/crespire))]
crespire1 has joined #ruby
sickdyd has joined #ruby
donofrio has joined #ruby
donofrio has quit [Ping timeout: 250 seconds]
roadie has quit [Ping timeout: 248 seconds]
jhass has quit [Ping timeout: 256 seconds]
TomyLobo has quit [Ping timeout: 240 seconds]
shokohsc9 has joined #ruby
shokohsc has quit [Ping timeout: 250 seconds]
shokohsc9 is now known as shokohsc
jhass has joined #ruby
crespire has joined #ruby
crespire1 has quit [Ping timeout: 250 seconds]
ap4y has quit [Remote host closed the connection]
roadie has joined #ruby
roadie has quit [Ping timeout: 248 seconds]
rvalue has quit [Ping timeout: 240 seconds]
rvalue has joined #ruby
donofrio has joined #ruby
sam113102 has joined #ruby
sam113101 has quit [Ping timeout: 240 seconds]
sam113102 is now known as sam113101
Sankalp has quit [Ping timeout: 256 seconds]
donofrio has quit [Ping timeout: 250 seconds]
Sankalp has joined #ruby
_ht has joined #ruby
jhass has quit [Ping timeout: 250 seconds]
jhass has joined #ruby
friendlypunk has joined #ruby
friendlypunk has quit [Remote host closed the connection]
friendlypunk has joined #ruby
otisolsen70 has joined #ruby
roadie has joined #ruby
Sankalp has quit [Ping timeout: 240 seconds]
roadie has quit [Ping timeout: 248 seconds]
roadie has joined #ruby
ollysmith has quit [Ping timeout: 240 seconds]
Sankalp has joined #ruby
ollysmith has joined #ruby
_ht has quit [Remote host closed the connection]
johnjaye has quit [Ping timeout: 265 seconds]
johnjaye has joined #ruby
tangib52 has joined #ruby
mexen has joined #ruby
grenierm has joined #ruby
aighearach has joined #ruby
infinityfye has joined #ruby
tangib52 has quit [Read error: Connection reset by peer]
llua has quit [Ping timeout: 265 seconds]
crespire1 has joined #ruby
crespire has quit [Killed (NickServ (GHOST command used by crespire1))]
Rounin has quit [Ping timeout: 268 seconds]
friendlypunk has quit [Quit: Leaving.]
Rounin has joined #ruby
Rounin has joined #ruby
Rounin has quit [Changing host]
BazzaBazaa has joined #ruby
jvalleroy has quit [Quit: https://quassel-irc.org - Chat comfortably. Anywhere.]
jvalleroy has joined #ruby
Linux_Kerio has joined #ruby
BazzaBazaa has quit [Ping timeout: 245 seconds]
BazzaBazaa has joined #ruby
BazzaBazaa has quit [Remote host closed the connection]
BazzaBazaa has joined #ruby
brw has quit [Quit: Ping timeout (120 seconds)]
brw has joined #ruby
yosafbridge has quit [Quit: Leaving]
yosafbridge has joined #ruby
Linux_Kerio has quit [Read error: Connection reset by peer]
Linux_Kerio has joined #ruby
BazzaBazaa has quit [Remote host closed the connection]
BazzaBazaa has joined #ruby
Linux_Kerio has quit [Read error: Connection reset by peer]
Linux_Kerio has joined #ruby
BazzaBazaa has quit [Remote host closed the connection]
BazzaBazaa has joined #ruby
desnudopenguino1 has joined #ruby
desnudopenguino has quit [Ping timeout: 240 seconds]
desnudopenguino1 is now known as desnudopenguino
otisolsen70 has quit [Ping timeout: 265 seconds]
passbe has joined #ruby
grenierm has quit [Quit: Client closed]
TomyLobo has joined #ruby
gr33n7007h has quit [Quit: WeeChat 3.8]
gr33n7007h has joined #ruby
otisolsen70 has joined #ruby
gr33n7007h has quit [Ping timeout: 240 seconds]
gr33n7007h has joined #ruby
desnudopenguino1 has joined #ruby
otisolsen70 has quit [Ping timeout: 240 seconds]
desnudopenguino has quit [Ping timeout: 248 seconds]
desnudopenguino1 is now known as desnudopenguino
sickdyd has quit [Ping timeout: 250 seconds]
mexen has quit [Quit: Connection closed for inactivity]
sickdyd has joined #ruby
dwindl has joined #ruby
sickdyd has quit [Ping timeout: 268 seconds]
llua has joined #ruby
lena64t2 has quit [Remote host closed the connection]
shokohsc has quit [Read error: Connection reset by peer]
shokohsc has joined #ruby
<adam12> isene: cheers
sickdyd has joined #ruby
<adam12> mark22k: A barrier is for only one task running at a time. Just glancing at your code, you're basically using Async but running it Sync.
dwindl has quit [Quit: Best CPUs can count to infinity twice without being bugged out by zero division errors or whatnot.]
BazzaBazaa has quit [Ping timeout: 245 seconds]
johnjaye has quit [Ping timeout: 240 seconds]
johnjaye has joined #ruby
<aesthetikx> adam12 do you have a github
BazzaBazaa has joined #ruby
lena64t2 has joined #ruby
BazzaBazaa has quit [Ping timeout: 245 seconds]
BazzaBazaa has joined #ruby
Sankalp has quit [Ping timeout: 265 seconds]
Linux_Kerio has quit [Read error: Connection reset by peer]
<mooff> how would i go about making https://github.com/awfulcooking/iirc async?
BazzaBazaa has quit [Ping timeout: 245 seconds]
<mooff> it only offers synchronous methods atm, though you can run many bots using threads
<adam12> aesthetikx: github.com/adam12
<adam12> mooff: IMHO, I might just look at adding Polyphony. It monkeypatches some of the core libs, so you might not have to modify much existing code.
<mooff> adam12: that sounds like a big thing to add as a dependency, i'd worry
<mooff> patching core libs etc, but i sort of see iirc as trying to be based on pure Ruby as much possible
<mooff> if that makes sense.. though i'll def check it out & sounds like a cool project
BazzaBazaa has joined #ruby
BazzaBazaa has quit [Ping timeout: 245 seconds]
Thanzex3268 has quit [Read error: Connection reset by peer]
<adam12> mooff: I can understand that. There is no built in scheduler in Ruby for Async. So you'd need async, polyphony, or fiber_scheduler.
<adam12> mooff: I just presumed that Polyphony could be the easiest lift to experiment.
Thanzex3268 has joined #ruby
<adam12> fiber_scheduler could be a safe default. https://brunosutic.com/blog/ruby-fiber-scheduler
<mooff> cool.. so, it's sort of like, the async practitioner uses whatever they're happy with already?
<adam12> I believe so? I've only used async library so far. But fiber_scheduler can co-operate with the other schedulers.
<mooff> i've been timid to try them because they're third party, i guess
<mooff> waiting to see which approach wins?
<mooff> i probably just want Ractors :)
roadie has quit [Quit: ERC 5.5.0.29.1 (IRC client for GNU Emacs 29.0.91)]
BazzaBazaa has joined #ruby
<mark22k> adam12 What can I use instead of Barrier? I want to wait for these two tasks before continuing the code.
Sankalp has joined #ruby
<adam12> mark22k: I think barrier.new takes an argument?
<adam12> so you could tell it 2? I think the default is 1.
BazzaBazaa has quit [Ping timeout: 245 seconds]
BazzaBazaa has joined #ruby
szkl has joined #ruby
<adam12> mark22k: Let me look up some of my code once I am off a call.
<adam12> mark22k: Sorry. Maybe I was misthinking semaphore.
sickdyd has quit [Ping timeout: 240 seconds]
admin2 has quit [Changing host]
admin2 has joined #ruby
<adam12> mark22k: What happens when you change barrier.async to task.async
admin2 is now known as roger_rabbit
sickdyd has joined #ruby
shokohsc9 has joined #ruby
shokohsc has quit [Ping timeout: 248 seconds]
shokohsc9 is now known as shokohsc
<mark22k> same behavior
sickdyd has quit [Ping timeout: 240 seconds]
BazzaBazaa has quit [Ping timeout: 245 seconds]
tomtmym has joined #ruby
tomtmym has joined #ruby
tomtmym has quit [Changing host]
BazzaBazaa has joined #ruby
BazzaBazaa has quit [Remote host closed the connection]
BazzaBazaa has joined #ruby
<adam12> mark22k: Removing the .wait from the second task?
<adam12> ^ mark22k
<mark22k> I had already removed that on a test basis. But that didn't bring anything either.
FetidToot4 has joined #ruby
Guest26nakilon has joined #ruby
FetidToot has quit [Ping timeout: 240 seconds]
FetidToot4 is now known as FetidToot
<Guest26nakilon> does anyone have an idea why async-websocket tutorial example does not start? https://github.com/socketry/async-websocket/issues/13#issuecomment-1568556808 -- I tried both using shebang as given and via $ falcon ... ./temp.rb
nmollerup has quit [Quit: Leaving]
FetidToot3 has joined #ruby
<adam12> Guest26nakilon: are you using a Gemfile in that project?
<adam12> Maybe you need a `require "bundle/setup"` at the top of the file before all the rest of the requires.
FetidToot has quit [Read error: Connection reset by peer]
FetidToot3 is now known as FetidToot
<Guest26nakilon> adam12 hmmm after mv Gemfile Gemfile the error changed to "NoMethodError: undefined method `run' for main:Object"
<Guest26nakilon> there was in fact a Gemfile there, it was just irrelevant
llua has quit [Ping timeout: 265 seconds]
<adam12> Sounds like it doesn't know the file is a rackup file and running in Rack::Builder.
<adam12> You're doing falcon -c temp.rb ?
<Guest26nakilon> both "falcon serve --bind http://localhost:7070 --count 1 -c ./temp.rb " and ./temp.rb
<adam12> What if you rename temp.rb to config.ru
<adam12> or temp.ru
<adam12> (I think file extension matters)
dstein64- has joined #ruby
dstein64 has quit [Ping timeout: 246 seconds]
dstein64- is now known as dstein64
<Guest26nakilon> the error is gone, lol
<Guest26nakilon> too much undocumented magic
<adam12> The .ru one for sure.
<Guest26nakilon> I wonder why the documentation example isn't like this one https://github.com/socketry/falcon/blob/358697db9b3486f2fce782c43472c05d2c8b1787/examples/server/standalone.rb
<Guest26nakilon> maybe I should try that
<adam12> Yeah, maybe. Might be worth a PR too....
<leah2> i regret doing .ru ;)
<adam12> I do find the Async/Falcon stuff to not be documented _great_.
<adam12> Ohh yeah. The Rack author is in here. All blame them!
<adam12> :p
<leah2> ;)
<adam12> Bacon too, right?
<leah2> yes
<leah2> anyone still use that?
<adam12> What a time to be in Ruby.
<adam12> I used it ages ago and enjoyed it.
<adam12> I've just normalized on Minitest, tho minitest isn't necessarily my _favourite_.
<leah2> yeah
<adam12> I wish Ruby would have adopted a minitest-like framework into core.
<adam12> With a _good_ test runner.
<adam12> It's very muddy lately. test-unit still lives and is used, then there is minitest which is external and good, but at the whim of Ryan who has a preferred style.
<adam12> We should ship a basic one like the nodejs people are.
<Guest26nakilon> when I first say config.ru like 15 years ago I wondered what does Russia have to do with it
<Guest26nakilon> *saw
<Guest26nakilon> in some sinatra tutorial
<adam12> LOL
<adam12> It does make sense on how it's set up. Back then it was all DSL/minimalsym.
<adam12> Maybe today it would be `Rack do run ... end`
<adam12> Then keep it .rb
<Guest26nakilon> I don't mind Ryan's style
<adam12> Might even be worth a suggestion to rack upstream.
<leah2> not me! :p
<Guest26nakilon> what do you guys want from test runner? I heard the gem "m" and probably used it once
<Guest26nakilon> to specify a line number I guess
<adam12> Guest26nakilon: Line number. Copy/paste test failures. Easier way to include/exclude. etc.
<adam12> m is good. I was using minitest-sprint for a while (which is from Ryan)
<Guest26nakilon> you know what I want?
sickdyd has joined #ruby
<adam12> leah2: I'm happy for Rack because I remember the time before Rack :(
<Guest26nakilon> the rspec html formatter to be a standalone converter of junit to html
<Guest26nakilon> because it's blended in
<adam12> I wonder why TAP didn't really take off.
<Guest26nakilon> and the rspec report is the only one existing that does not make eyes bleed
<Guest26nakilon> and that prevents me to introduce the minitest in production
<adam12> I want minitest-like with power-assert (without edgecases) and an rspec runner.
<leah2> adam12: i use it for all my stuff :D
<adam12> leah2: TAP? iirc bacon supported it.
<leah2> yes
<leah2> mostly from bash and C nowadays tho
<adam12> leah2: Ah interesting. I figured it was dead, so that's good to know.
<leah2> still common in perl land
sickdyd has quit [Ping timeout: 250 seconds]
<adam12> Guest26nakilon: I dont' mind Ryans style, but his use of preforce and hoe makes working with his repo's unique. Can't just add the git path to bundler, have to unpack a gemspec if I want it locally, and commits go into preforce manually then back out to git somehow.
<Guest26nakilon> so I launch https://editor.p5js.org/abachman-mica/sketches/nV1BOJomZ but the connection is Pending; if I change wss: to http: it's error
<Guest26nakilon> oh wait, no error I guess, just nothing happens
<Guest26nakilon> adam12 oh I remember how to run tests of minitest I needed to forge a gemspec or something
<adam12> leah2: Huh interesting. Was just trolling your profile and noticed Knock, a simplification of TAP. Do you use that at all?
<adam12> Hard to believe it was 15 years ago. Where does the time go.
<Guest26nakilon> (the above sketch link is from https://github.com/abachman/p5.websocket)
<adam12> I think it's working.
<adam12> Just maybe there's something wrong with the JS.
<leah2> adam12: no
<Guest26nakilon> oh, changed wss: to ws: and got connection, now to figure out why it's still pending
<mooff> *who* is using Perforce?! 🤓
<adam12> Guest26nakilon: Oh, I see.
<adam12> Guest26nakilon: I can get your demo to work by adding fill(myColor) to the bottom of messageReceived()
<adam12> Guest26nakilon: but you're saying maybe teh websocket handshake hasn't finished.
<adam12> mooff: zenspider
<Guest26nakilon> oh the circle is changing the color! I guess it's just chrome's feature that it's 0 bytes and pending, but it in fact goes on
<leah2> i should use bitkeeper ^.^
<adam12> Guest26nakilon: Oh nevermind. It works without fill(myColor), just super delayed.
<adam12> leah2: lulz.
<Guest26nakilon> adam12 not sure why delayed, the draw() is supposed to work in loop
<adam12> I can understand not wanting to change source systems all the time. I went CVS/SVN/Git.
<adam12> Guest26nakilon: Maybe latency?
<adam12> Guest26nakilon: It does work, I was just not patient enough.
<leah2> adam12: i used almost everything and then git
<Guest26nakilon> ..D
<adam12> I think git has enough staying power right now, but someone might figure out how to make the git ui a bit less rough.
<Guest26nakilon> speaking about the async documentation, I added "p 1" within $connections.each but it does not go to my stdout
<adam12> Is stdout buffered
<Guest26nakilon> I need to debug but I foresee the struggling
<adam12> Try setting it to sync if not already. $stdout.sync = true
<adam12> `1` might not be enough to flush the buffer.
<Guest26nakilon> sec
<adam12> bbiaf lunch :)
<Guest26nakilon> oh
llua has joined #ruby
<Guest26nakilon> it's because I had temp.rb in sublime opened
<Guest26nakilon> _--
<adam12> LOL
<Guest26nakilon> bon appeti
<Guest26nakilon> I want to try use p5js instead of taking curses from the shelve
<Guest26nakilon> thanks for hints, I now can proceed
<Guest26nakilon> I imagine responding with graphics primitives with ruby lambda __id__s attached
<Guest26nakilon> not sure about memory though; need to keep the lambdas attached to some objects; or using classes and tuples of obj.__id__ and method name
szkl has quit [Quit: Connection closed for inactivity]
<Guest26nakilon> "to make the git ui a bit less rough." -- I see no need
<Guest26nakilon> those who don't want or can't deal with it can gtfo
<Guest26nakilon> it takes just few days of patient working in a team that knows better, and you'll be ok with it
BazzaBazaa has quit [Ping timeout: 245 seconds]
_ht has joined #ruby
BazzaBazaa has joined #ruby
crespire has joined #ruby
sickdyd has joined #ruby
crespire1 has quit [Ping timeout: 250 seconds]
crespire1 has joined #ruby
crespire has quit [Killed (NickServ (GHOST command used by crespire1))]
BazzaBazaa has quit [Remote host closed the connection]
sickdyd has quit [Ping timeout: 240 seconds]
donofrio has joined #ruby
donofrio has quit [Ping timeout: 240 seconds]
desnudopenguino has quit [Ping timeout: 248 seconds]
donofrio has joined #ruby
llua has quit [Quit: <Rudolph> shell code is what greycat reads to kids when he tucks them in]
sickdyd has joined #ruby
llua has joined #ruby
Prodigy has joined #ruby
donofrio has quit [Ping timeout: 256 seconds]
sickdyd has quit [Ping timeout: 250 seconds]
blades7 has joined #ruby
blades7 has quit [Read error: Connection reset by peer]
donofrio has joined #ruby
Guest26nakilon has quit [Quit: Client closed]
roadie has joined #ruby
johnjaye has quit [Read error: Connection reset by peer]
johnjaye has joined #ruby
Prodigy has quit [Quit: WeeChat 3.8]
henk has joined #ruby
<henk> Hi, I’m trying to run the example on https://ruby-doc.org/stdlib-2.2.0/libdoc/resolv/rdoc/Resolv.html in irb: http://ix.io/4x6M I don’t understand the error. why does it expect 'end' there? I’m thinking it has something to do with the ; or the {} but I don’t understand why.
<aesthetikx> hi henk;
<aesthetikx> you either need something do |block| ; end
<aesthetikx> or,
<aesthetikx> something { |block| }
<aesthetikx> but not moth
<aesthetikx> both*
<aesthetikx> in your case, you probably want "Resolv::DNS.open { |dns| ..... }
<johnjaye> what is the |block| thing called
<aesthetikx> a block argument
<johnjaye> i see
<aesthetikx> 3.times { |i| puts i }
<aesthetikx> will print i three times
<aesthetikx> methods can 'yield' items to a block an arbitrary number of times, and the block will run each time with the argument in between the |pipes|
<aesthetikx> typically rubyists will use the braces form for one liners, but for longer stuff:
<aesthetikx> 3.times do |i|
<johnjaye> not sure what you mean by arbitrary. usually it's just to go over a collection right
<aesthetikx> puts i
<aesthetikx> end
<aesthetikx> yes, but you can have methods that only yield once, for example a callback
<aesthetikx> or File.open { |f| # the file is open here but this is only called once, the file is automatically closed after the block }
<johnjaye> isn't that the opposite of conventional notation
<johnjaye> normally the whole point of braces is a multiline block
<aesthetikx> the opposite I believe
<johnjaye> so ruby is cool and doesn't play by the rules. i dig it
<aesthetikx> lol you could say that , yes
<johnjaye> i thought it was some weird usage of set builder notation
<johnjaye> like A = { x | x is blue }
<aesthetikx> a lot of people get confused because they think they are looking at | | as an absolute value or something, but its just how the block args are written
<aesthetikx> yeah
<aesthetikx> nope
<johnjaye> it does look very odd yeah.
<aesthetikx> would almost be clearer if it was parenthesis, but that is what 'smalltalk' did, and thats where its from
<johnjaye> why does it have to be enclosed though
<johnjaye> why not ,arg or ;arg
<aesthetikx> if you use a stabby lambda you get my_fn = ->(one, two, three) { # do something }
<johnjaye> the -> is code for lambda?
<aesthetikx> shorthand yeah
<aesthetikx> there can also be multiple args, e.g. some_collection.each.with_index { |the_thing, the_index| # ... }
<johnjaye> lambda is probably an unfortunate choice of letters anyway. but meh
<johnjaye> oooh really
<henk> aesthetikx: oooh, I see, thanks
<aesthetikx> yup lmk if you get stuck again
friendlypunk has joined #ruby
donofrio has quit [Ping timeout: 268 seconds]
donofrio has joined #ruby
infinityfye has quit [Quit: Leaving]
<johnjaye> aesthetikx: hope you don't mind if i ping you
<johnjaye> but i forgot to ask something
<aesthetikx> all good
<aesthetikx> whats up
<johnjaye> in ruby is there no way to access a class or object variable directly?
<johnjaye> the tutorial i read implied that but it's a bit strange to me
<aesthetikx> hmm elaborate?
<johnjaye> in java you can make an object field public or private
<johnjaye> if public you can set it directly
<aesthetikx> oh I see
<johnjaye> as in a.x = 2, a.y = 3
<johnjaye> or just use it. i = a.x + 7
<aesthetikx> if you want that behavior, you can use 'attr_accessor :the_thing'
<aesthetikx> e.g. class Pet; attr_accessor :name; end
<johnjaye> oh attr_accessor is literal?
<aesthetikx> dog = Pet.new; dog.name # => nil ; dog.name = "Jim"; dog.name #=> Jim
<aesthetikx> yeah its actually a method, that makes two more methods, let me explain
<aesthetikx> so, instances of a class have instance variables, which you may have seen, they look like @name (they have a '@' prefixed)
<aesthetikx> these are private by default
<aesthetikx> so, by convention you would write class Pet; def name; @name; end; end (sorry for the single line, but you get the idea)
<johnjaye> yeah. in java you declare those directly. like class Pet; string name; end
<johnjaye> but in ruby examples it's only in the constructor
<aesthetikx> and then you would also write class Pet; def name=(new_name); @name = new_name; end; end; for hte 'setter'
<johnjaye> right.
<aesthetikx> attr_accessor just defines both of those methods for you
<johnjaye> i saw that in the tutorial
<johnjaye> oh ok
<aesthetikx> there is also "attr_reader" which just gives you the public method to get, and "attr_writer" which just gives you the setter
<johnjaye> ah ok. yeah when i put () at the end it still runs
hrberg has quit [Quit: https://quassel-irc.org - Chat comfortably. Anywhere.]
<johnjaye> the ruby way of omitting the () for method calls is very odd
<aesthetikx> its fun :)
hrberg has joined #ruby
<aesthetikx> but yeah if you aren't familiar it can be confusing, becuase its hard to tell what is a method
<aesthetikx> but pretty much everything is a method, so yeah
<mooff> *weekly thought wishing classes could be imported as modules*
<johnjaye> what about for a class variable
<johnjaye> like the @@i=1 type thing
<aesthetikx> typically not used as much, but those are "@@thing", yes
<johnjaye> can i not define them outside of the constructor?
<johnjaye> or an object methodi mean
<aesthetikx> so, for methods, you use "def self.the_class_method" , e.g. prefix the word self
<johnjaye> java concept maybe coming in here. in java you can have a static variable i think it's called? where it's basically just callable from any object at all
sickdyd has joined #ruby
donofrio has quit [Ping timeout: 250 seconds]
<aesthetikx> you can do "def self.whatever" for a class method;
<johnjaye> oh. then it's callable even if there areno objects?
<aesthetikx> for class variables you can just say "@@static_variable = '4'" right in the class body
<aesthetikx> yes
<johnjaye> like Pet.print("something")
<aesthetikx> yup call it right on the class, Pet
<johnjaye> i tried @@i=2 in the class body but it didn't work
<johnjaye> or at least i couldn't print it out
<johnjaye> do i need an accessor too?
<aesthetikx> for class variables I think no, they are treated as sort of public
<mooff> @@ variables are specific
<johnjaye> how do i get at it then
<johnjaye> i get undefined method i
<aesthetikx> I think you should be able to do class Pet; @@i = 1; end; puts Pet.i # > 2
<johnjaye> if i do x.i
<aesthetikx> # => 1 *
<mooff> they are stored on the module you're currently working in, lexically
<aesthetikx> oh, if you need to get at it from an instance instead of the class, youw ould have to write like def i; self.class.i; end
<johnjaye> hrm
<johnjaye> i was trying something like class Pet; @@i=2; end; puts Pet.i and it gave error
<johnjaye> this is in a file
<aesthetikx> I think that sould work, i just tried it
<aesthetikx> huh
<mooff> they're scoped to your current nested module, as in the module for your nearest class / module keyword
sickdyd has quit [Ping timeout: 250 seconds]
<johnjaye> i get NoMethodError
<aesthetikx> oh I'm dumb yeah ignore me
<johnjaye> mooff: why did it work just no but not for me? i'm confused
<aesthetikx> tbh class variables are so rarely used that I honestly don't know much about how they work, despite using ruby for like 15 years hahah
<johnjaye> yeah. i would think a class method would be more common. and for that i need self
<johnjaye> i think?
<johnjaye> so like File.read
<johnjaye> how do i define that again. sorry if i misunderstood
<aesthetikx> yeah class File; def self.read #... ;end
<mooff> the main thing you're thinking about is @foo aye
<mooff> that's the one you want
<mooff> it works exactly the same way whether self is a class object or any other object
<johnjaye> o i see that works.
<mooff> and totally separate to @@ vars
<johnjaye> but on the @@ vars thing
<mooff> the @@ vars are lexical module vars
<mooff> that's the clearest way I can think to put it
<mooff> they are found and set based on the nearest class or module you've opened
<mooff> and the scope for that can't be set by anything other than "class" and "module" keywords
<mooff> they must have been set to at least once before they're read, or Ruby raises a NoNameError
<mooff> I use @@foo ||= sometimes which gets around it
sickdyd has joined #ruby
<mooff> they're nice but specific in that way
<mooff> and with the specific scope
johnjaye has quit [Ping timeout: 250 seconds]
panella32 has joined #ruby
panella32 has quit [Client Quit]
<aesthetikx> johnjaye, there is one secret; you can do class Pet; class << self; attr_accessor :something; end; end if you want Pet.something = 1
<aesthetikx> although that syntax is something that you will need more experience with ruby to actually understand meaningfully
<aesthetikx> otherwise in general you can only access @@something within other class or instance methods of that class
panella32 has joined #ruby
johnjaye has joined #ruby
panella32 has joined #ruby
<panella32> Hi currently I'm having a :movies table and a :people table which have many-to-many relationship. I am going to add a new column "director_id" to :movies table. Using a migration containing the following line I achieved the goal and now it is working as expected:
<panella32> >> add_reference :movies, :director, foreign_key: {to_table: :people}
<panella32> but the problem is I don't see an association like "Movie.first.director", any idea how can I add that?
<aesthetikx> sure
<aesthetikx> You will need to, in your class Movie
<aesthetikx> has_one :director, class_name: 'Person'
<aesthetikx> something like that, there are more options, but that should get you there
<panella32> aesthetikx Thank you from that quick response! Let me try that! A sec please..
<aesthetikx> Rails will assume that :director is a Director, but since you have specified otherwise you will need to provide class_name.
<aesthetikx> sure
<aesthetikx> I believe based on 'has_one :director', Rails will automatically determine that the correct foreign key is 'director_id'.
<aesthetikx> actually sorry I was backwards
<aesthetikx> based on your schema, you want belongs_to :director, class_name: 'Person'
<aesthetikx> also, to get the other side, you will need in your Person class, something along the lines of
<aesthetikx> has_many :movies, inverse_of: :director
<aesthetikx> that way you will get some_movie.director.movies # => [some_movie, ...]
sickdyd has quit [Ping timeout: 250 seconds]
<panella32> sorry for the too long delay, need to restart the whole thing
<panella32> now i get RecordInvalid: Validation failed: Director must exist
<aesthetikx> If you don't need a director, I think you can say ", optional: true"
<aesthetikx> in newer versions of rails, belongs_to associations are required by default I believe
<aesthetikx> so "belongs_to :director, class_name: 'Person', optional: true"
<panella32> Maybe it's looking for a class like "Director" and can't find it?
<aesthetikx> I believe that validation is happening for new records being created, but I think "optional: true" will allow you to have a nil director_id
<johnjaye> aesthetikx: i was disconnected. did you reply to me
<aesthetikx> i did
<aesthetikx> sorry
<johnjaye> i didn't see it i mean
<aesthetikx> Add "optional: true"
<aesthetikx> to your "belongs_to"
teclator has joined #ruby
<aesthetikx> in newer versions of rails, belongs_to is required by default for new records; optional: true will allow you to have a nil director_id
<aesthetikx> oh
<aesthetikx> not you johnjaye haha someone else
<aesthetikx> did you have a question
<panella32> aesthetikx Thank you so much! Now it works like a charm! https://postimg.cc/4YPRz1Cw
<johnjaye> yes i didn't understand your pet example
<johnjaye> class Pet @@i=2 end puts Pet.i
<johnjaye> why does this fail
_ht has quit [Quit: _ht]
<aesthetikx> panella32 glad to hear it!
<aesthetikx> johnjaye I think class variables are only accessible to that classes' class methods or instance methods;
<johnjaye> meaning the only way to access it would be with a method?
<aesthetikx> if you want that behavior you can say class Pet; class << self; attr_accessor :i; end; end which we could go over more later, that goes into some advanced ruby usge
<johnjaye> ok. i tried adding def i / i / end to the class but that doesn't work either
<aesthetikx> class Pet; def self.i; @@i; end; def self.i=value; @@i = value; end; end; will also work
<johnjaye> oh i see. Pet.i is not allowed. I can only create a specific pet object and call it through that
<aesthetikx> right, with a regular 'ol "def i" yeah
<johnjaye> i learned oop in java so that's why i'm asking things like this.
<aesthetikx> they are valid questions
<aesthetikx> ruby is a mix of OOP, functional, perlisms, smalltalk, so there is usually a 'ruby way' of doing things that isnt 1:1 with other langs
<johnjaye> well for example say you had class Shape and needed to put pi=3.14 as a static class member, that would be an example coming to mind.
<johnjaye> because you want it available to every object of the class
<johnjaye> does it have good concurrency or is that more of an erlang thing
<aesthetikx> in that case, you would probably want an actual constant;
<aesthetikx> class Shape; PI = 3.14; end
<aesthetikx> Shape::PI # => 3.14
<aesthetikx> variables written in all caps like that are constants
donofrio has joined #ruby
<johnjaye> i guess you sort of have that already though
<johnjaye> so maybe in super elite oo theory you don't need class variables
<aesthetikx> concurrency is a mixed bag depending on who you ask,
<johnjaye> a constant has different visibility than a member?
<aesthetikx> there are some new things added in ruby 3.x that are pretty nice for both concurrency and parallelism
<aesthetikx> constants are public but need to be accessed with :: (namespace resolution operator) outside the class, or just 'PI' if it is in scope already
johnjaye has quit [Ping timeout: 265 seconds]
<aesthetikx> panella32 I still haven't seen the others, although it is on my list, is it good?
johnjaye has joined #ruby
<johnjaye> hmm my connection seems unstable. guess i will bow out for now
<johnjaye> thanks for answering my questions though
<panella32> aesthetikx I was a kid when I watched it back in the day and idk why but I loved the kind of atmosphere and stories. Still it's a nostalgia kind of thing for me.
<panella32> btw the other side of the association seems to need a bit of tweak: https://postimg.cc/PCTzgWCg
graywolf has joined #ruby
<aesthetikx> I see
<aesthetikx> yeah so panella32, since you have two kinds of ways of associating with movies, you would need two associatiosn (if you need them)
<aesthetikx> i.e. you cant specify the same relation name twice,
<aesthetikx> in this case I would do something like
<aesthetikx> has_many :directed_movies, class_name: 'Movie', inverse_of: :director
<aesthetikx> that way you could do some_person.movies # => all the movies they workded on through movie people,
<aesthetikx> or some_person.directed_movies # => the movies they directed
<aesthetikx> in addition to that, you might want to add "dependent: :nullify",
<aesthetikx> so that if a Person is destroyed who was a director, the director_id on those Movies would be nil (instead of I think you would end up with a foreign key violation or something)
<aesthetikx> ofc, if you don't need to access movies / directors like that in your app, you don't actually need the associatoins. In this case it would probably be good to have it either way though, to have rails clean up the foreign keys if a person is destroyed regardless
Pixi` has joined #ruby
nektro_ has joined #ruby
<panella32> I see, great point thanks! I'm not sure why but I'm still getting : unknown column 'movies.person_id'
<aesthetikx> sure,
FetidToot4 has joined #ruby
<aesthetikx> I think it may want you to specify "foreign_key: :director_id", on that has_many
<aesthetikx> normally that wouldn't be needed, but in this case because it is the Person class it is by default assuming that the foreign_key will be called person_id, and you ahve to tell the association that you have a column called director_id
<aesthetikx> I can never remember if that is needed or not, I always think that "inverse_of: :director" should automatically change the foreign key, but it doesn't
kenichi_ has joined #ruby
shokohsc4 has joined #ruby
Shell- has joined #ruby
Fridtjof_ has joined #ruby
catphish_ has joined #ruby
dstein64- has joined #ruby
leftylin1 has joined #ruby
Shell- is now known as Shell
Shell has quit [Killed (copper.libera.chat (Nickname regained by services))]
johnjaye has quit [Ping timeout: 250 seconds]
asio_ has joined #ruby
genpaku_ has joined #ruby
tomtmym has quit [Quit: Gone.]
zak1 has joined #ruby
peder_ has joined #ruby
finstern1s has joined #ruby
<panella32> Tried the "foreign_key: :director_id"" option and now it's working great! Thanks you must be a ActiveRecord specialist aesthetikx! I wish I could had a teacher like you to learn ror from!
kenichi_ has quit [Changing host]
kenichi_ has joined #ruby
<aesthetikx> awesome, I think you have actually covered most of the various options for 90% of relations
<aesthetikx> I'm here most of the time if you need
<aesthetikx> This guide is also pretty good although I'm sure you have seen it https://guides.rubyonrails.org/association_basics.html
<aesthetikx> this may or may not be needed for your app, but another approach to modelling your data could be to have a kind of 'role' on movies_people,
<aesthetikx> e.g. Movie has_one :director, through: :movie_people, -> { merge(MoviePeople.director) }
<panella32> It is a blessing to have you here, I am really happy about it!
<aesthetikx> or something like that, I cant' do it off the top of my head, but anyway its an option to dig into if you are super bored.
<aesthetikx> seems liek what you have is good for now though
<panella32> Not a fan of docs but I'll check it out lol
<panella32> Actually I had that in mind too!
<aesthetikx> yeah I get that but the docs aren't actually that bad
<aesthetikx> in this case anyway
donofrio has quit [Ping timeout: 240 seconds]
<aesthetikx> yeah if you have a role (probably an Enum, look that up if you haven't seen that before, those are great in rails),
<aesthetikx> then you could make a bunch of associations without changing your schema, for other types of things e.g. screenwriter or something
dstein64 has quit [*.net *.split]
FetidToot has quit [*.net *.split]
shokohsc has quit [*.net *.split]
Fridtjof has quit [*.net *.split]
nektro has quit [*.net *.split]
ox1eef_ has quit [*.net *.split]
asio has quit [*.net *.split]
Pixi has quit [*.net *.split]
leftylink has quit [*.net *.split]
catphish has quit [*.net *.split]
peder has quit [*.net *.split]
mooff has quit [*.net *.split]
finsternis has quit [*.net *.split]
kenichi has quit [*.net *.split]
z4kz has quit [*.net *.split]
genpaku has quit [*.net *.split]
joast has quit [*.net *.split]
peder_ is now known as peder
FetidToot4 is now known as FetidToot
dstein64- is now known as dstein64
shokohsc4 is now known as shokohsc
finstern1s is now known as finsternis
<panella32> yeah I was actually going to add such column but skipped it for later but it's a good idea..
<aesthetikx> yeah thats a later idea for a later time
John_Ivan has joined #ruby
mooff has joined #ruby
<aesthetikx> it is often the case in 'enterprise' or productioney systems where things like 'A movie has a director' fails as soon as there is some movie out there with two director credits or something
<aesthetikx> always super fun to go from N=1 to N=M
<aesthetikx> modeling human relationships especially can be hard
teclator has quit [Ping timeout: 250 seconds]
roadie has quit [Ping timeout: 248 seconds]
<panella32> I agree. My project actually it's a simplified version of TMDB and I just ignored that case because as you said it'd bring too much unnecessary complication to my basic project as I am just gonna learn though developing the app..
<panella32> not directly related but is there any specific course or code learning website you generallyrecommend for lower intermediate ror devs?
<aesthetikx> Back in the day I really liked "destroy all software" and "railscasts" but both of those are somewhat outdated now
<aesthetikx> let me see
<aesthetikx> there is someone on yt that is pretty good
<aesthetikx> check this guy out maybe https://ruby.social/@kobaltz@rails.social and 'drifting ruby'
<aesthetikx> but other people will probably have better input on that one than me, i learned a long long time ago
<aesthetikx> you could make an account or follow some people on ruby.social (im there) or rails.social etc.
joast has joined #ruby
<aesthetikx> ruby.social I mean, not sure what rails.social is, seems private although that kobaltz guy has an account there somehow, maybe its his instance
<aesthetikx> im off for a bit ttyl
<panella32> Sure I do check it out. Let me thank you again!
<panella32> Have a good time!
catphish_ is now known as catphish
<panella32> I mean I have to go also :)
<panella32> Bye for now
panella32 has quit [Quit: Textual IRC Client: www.textualapp.com]
reset has quit [Quit: reset]
johnjaye has joined #ruby
friendlypunk1 has joined #ruby
friendlypunk has quit [Read error: Connection reset by peer]
roadie has joined #ruby
ruby[bot] has quit [Remote host closed the connection]
ruby[bot] has joined #ruby
sickdyd has joined #ruby
sickdyd has quit [Ping timeout: 248 seconds]
gr33n7007h has quit [Ping timeout: 246 seconds]
gr33n7007h has joined #ruby
graywolf has quit [Quit: WeeChat 3.8]
sickdyd has joined #ruby
sickdyd has quit [Read error: Connection reset by peer]
sickdyd has joined #ruby
sickdyd has quit [Ping timeout: 265 seconds]
sickdyd has joined #ruby
sts has joined #ruby
sts has quit []
sts has joined #ruby
CalculusCats has quit [Quit: Meow Meow Meow Meow Meow Meow Meow Meow]
CalculusCats has joined #ruby
sickdyd has quit [Ping timeout: 265 seconds]
ox1eef_ has joined #ruby
smp_ has joined #ruby
smp has quit [Ping timeout: 256 seconds]
smp_ is now known as smp
kenichi_ is now known as kenichi
lena64t2 has quit [Remote host closed the connection]