Variables: Declared? Defined? Both??
What is or isn't “Perl-ish” I suppose is subjective but I think these two have a syntax I can find some familiarity with. I still haven't felt empowered enough yet to really want to use either of these for actual
$workStuff yet. Oddly enough I've reached for another Perl-ish language for this that I initially only cared about for its killer app.
If there's one thing that kinda struck me as odd about Ruby it's how variables are “defined.” Honestly I have a tendancy to spend hours of trial an error when learning new things to spare me the ten minutes it takes to just read the documentation but I got to thinking about it and realized I had a knowledge gap in what makes a variable “defined”? If I declare a variable like so:
$ re.pl $ my $string = 'ayy... lmao';
I've declared and defined the variable. But there's this other thing I've been doing without ever giving it much thought:
$ my ($string, %hash, @list);
So I just defined 3 different variables at once right? Turns out that is not the case as I learned in, of all things, a JS tutorial. I've declared these three variables but they are infact, undefined:
$ node Welcome to Node.js v14.17.0. Type ".help" for more information. > let string, hash, list; undefined > string undefined > undeclaredVar Uncaught ReferenceError: undeclaredVar is not defined
I recognized this as
undef in Perl:
$ re.pl $ my ($string, %hash, @list); $ use Data::Dumper; $ Dumper($string); $VAR1 = undef;
Cool. So there's declared variables and then there's defined variables. But the thing about Ruby is there's no
my keywords. The way you define a variable in Ruby reminds me more of
$ irb irb(main):001:0> string = 'ayy... lmao' => "ayy... lmao"
But what if I just want to “declare” a variable but not “define” it? The best solution I've found so far goes like this:
irb(main):001:0> string = nil => nil
This is actually different than:
irb(main):001:0> string = '' => ""
Coincidentally (or perhaps not, I'm using a filler word) only
nil will evaluate as false in Ruby:
irb(main):001:0> string = '' => "" irb(main):002:0> puts "ayy... lmao" if string ayy... lmao => nil
That means this madness is real:
irb(main):003:0> puts "ayy... lmao" if 0 ayy... lmao => nil
If only Ruby had
my so I could declare but not define variables... Wait. What if I did something like... ?:
irb(main):001:0> string, hash, list = nil => nil irb(main):002:0> puts "ayy... lmao" if list => nil irb(main):003:0> puts "ayy... lmao" if undeclaredVar (irb):3:in `<main>': undefined local variable or method `undeclaredVar' for main:Object (NameError) from /usr/share/gems/gems/irb-1.3.5/exe/irb:11:in `<top (required)>' from /usr/bin/irb:23:in `load' from /usr/bin/irb:23:in `<main>'
Ultimately it seems Ruby gives me all the roap I need to hang myself. It just doesn't look right to me because I've grown fond of
my. One could write their entire web backend and frontend both in JS but I'd rather use Ruby on Rails in my backend. Or in theory at least. I learned Ruby to learn Rails but I can't yet write anything useful with Rails. But with plain Ruby I can do some neat system-side stuff as I would with Perl. My homepage uses Perl on the backend because I already knew how to do it with Perl. And my frontend is more JS than CSS because I actually like JS the more I learn about it.
But I really need to stop dicking around and learn Rails already. I wrote a blog post for the first time in months to put off reading another chapter of my Rails book. On the brightside I won't have to learn SQL if I can master ActiveRecord. How nice would it be to just do everything in one language? SQL (pronounced like sequel) must be a conspiracy to sell more CS degrees. Or Oracle licenses.
I should probably just learn SQL so I can finally learn Rails.