We often fear what we do not know. For many IT admins, this is Ruby code. JNUC session presenter, Chris Lasell, was on hand today to help provide clarity and ultimately alleviate users’ apprehension of coding in Ruby.
He started with some familiar names: Python, Perl, Advanced Bash, etc., and clarified that attendees already understood data types, conditionals and loops. Then Lasell introduced gem – the command for installing and working with Ruby packages.
Next he discussed irb, or interactive Ruby, a shell where users type Ruby code. “It’s useful for testing your code as you write, or for performing one-off tasks,” he explained. “’Require’ tells Ruby to read and execute some pre-written code.”
Take a look:
> require ‘intro-ruby’
To keep it simple, think of everything in Ruby as an object. “Objects equal nouns,” Lasell explained. He added that there are many kinds of objects, like Strings, Integers, each with different attributes and abilities. Think of: Kind = Class.
And every action is a method. So, methods equal verbs, or functions. They make objects do things, like retrieving attributes and performing actions. Methods are called by sending the name to an object via a dot at the end. And different classes have different methods.
=> NoMethodError: undefined method ‘capitalize’ for 15:Fixnum
Things to note about methods:
- Some work as-is
- Some require parameters
- Parens are (usually) optional
- Each one’s documentation will tell you how to use it
Lasell added, “Because methods return values, method calls can be chained. And some methods work with no target, seemingly.”
The session dove into detail about variables and constants, quoting strings, and string interpolation before looking at symbols, arrays and hashes. “Ruby comes with many classes,” Lasell recapped. “Some are immediately available in the core, and some need to be loaded in by requiring a library.” Or, he excitedly announced, you can write your own.
Jam packed with information, the session continued a deep dive into the depths of Ruby. From modules to iterators, and everything in between, Lasell shared examples of how everything works, sharing code along the way.
He also explained how Ruby works with the JSS REST API, giving the advice, “Be sure the user has enough permissions!”
The discussion included key bits of information like:
- To delete, call the .delete method, and it happens immediately
- Extension Attributes can return current values
- To create an object not yet in the JSS, use “id: :new” and provide at least a name
Lassel ended with a treat – going beyond the code. He briefly explained Core Library, Standard Library, Gems and Resources.
While attendees may not yet be proficient with Ruby, they got a glimpse into the not-so-scary code and saw how it could be used to simplify API access. Next steps – go forth and conquer.