Chapter 1 - More With Less

Exercise 2: IRB Session

Here’s the output you’ll see when you type those expressions into irb. We’ve also added some notes about what the results mean.

$ irb
2.2.2 :001 > my_string = "ruby is so cool"
 => "ruby is so cool"

Here we assign a string to a variable. The result of an assignment expression is the value assigned, which is why irb prints => "ruby is so cool" after you press Enter. The string is now stored in the variable, and ready for use in subsequent expressions.

2.2.2 :002 > my_string.methods
 => [:<=>, :==, :===, :eql?, :hash, :casecmp, :+, :*, :%, :[], :[]=, :insert, :length, :size, :bytesize, :empty?, :=~, :match, :succ, :succ!, :next, :next!, :upto, :index, :rindex, :replace, :clear, :chr, :getbyte, :setbyte, :byteslice, :scrub, :scrub!, :freeze, :to_i, :to_f, :to_s, :to_str, :inspect, :dump, :upcase, :downcase, :capitalize, :swapcase, :upcase!, :downcase!, :capitalize!, :swapcase!, :hex, :oct, :split, :lines, :bytes, :chars, :codepoints, :reverse, :reverse!, :concat, :<<, :prepend, :crypt, :intern, :to_sym, :ord, :include?, :start_with?, :end_with?, :scan, :ljust, :rjust, :center, :sub, :gsub, :chop, :chomp, :strip, :lstrip, :rstrip, :sub!, :gsub!, :chop!, :chomp!, :strip!, :lstrip!, :rstrip!, :tr, :tr_s, :delete, :squeeze, :count, :tr!, :tr_s!, :delete!, :squeeze!, :each_line, :each_byte, :each_char, :each_codepoint, :sum, :slice, :slice!, :partition, :rpartition, :encoding, :force_encoding, :b, :valid_encoding?, :ascii_only?, :unpack, :encode, :encode!, :to_r, :to_c, :unicode_normalize, :unicode_normalize!, :unicode_normalized?, :>, :>=, :<, :<=, :between?, :interesting_methods, :nil?, :!~, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]

Don’t be overwhelmed! This is a list of all methods available on the String object referenced by my_string. (We’ll explain what the [] brackets and : characters mean in later chapters; they’re not important for now.) Some of these will be useful to a Ruby beginner, some of them won’t. But you can’t hurt anything by playing around with them, so let’s try a few…

2.2.2 :003 > my_string.next
 => "ruby is so coom"

The next method produces whatever string would be “next” in a sequence, so "a".next would return "b". It increments the last letter in the string, and “m” follows “l”, which is why we get "ruby is so coom".

Note that next (and most other methods) return a new string; they don’t modify the original. So the string referenced by my_string is untouched for our next operation…

2.2.2 :004 > my_string.reverse.upcase
 => "LOOC OS SI YBUR"

These are methods we’ve used before, but here we chain them together using dot operators. my_string.reverse returns "looc os si ybur", and then upcase captializes the result.

Note that some developers frown on chaining methods together like this (in part because it can cause an error if a method doesn’t return the value you expect). But you’ll still see it in the wild, so it’s good to know that it’s possible.

2.2.2 :005 > my_string.insert(11, "very, very ")
 => "ruby is so very, very cool"

The insert method inserts one string into another, at the character position you specify. It’s a rare exception among String methods; it does modify the original string.

2.2.2 :006 > "Rolling a die... we got #{ rand(1..6) }"
 => "Rolling a die... we got 6"

You can use any Ruby code when interpolating values into a string with #{}. Here, we call rand to generate a random number (so the string you get back may be different). The 1..6 is a range, indicating the set of all numbers between 1 and 6, so this will get us a random number between 1 and 6.

After you press Enter to submit this expression, try using the up-arrow on your keyboard to bring the previous command back and run it again to see the various results!

2.2.2 :007 > "There are #{ 60 * 60 } seconds in an hour."
 => "There are 3600 seconds in an hour."

Here, we use #{} interpolation to multiply two numbers and embed the result in our string.

2.2.2 :008 > 42.to_s
 => "42"

The to_s method converts an object to a string. In the case of a number, we get a string representing that number.

2.2.2 :009 > "42".to_i
 => 42

The to_i method converts an object to an integer. Since this is a string containing nothing but a number, that’s the number we get back.

2.2.2 :010 > exit
$ 

Typing exit by itself exits irb, and returns us to the system prompt.