What is attr_accessor in Ruby?

Let’s say you have a class Person. Obviously we never defined method name. Let’s do that. Aha, we can read the name, but that doesn’t mean we can assign the name. Those are two different methods. The former is called reader and latter is called writer. We didn’t create the writer yet so let’s do that. Awesome. Now we can … Read more

ruby operator “=~” [duplicate]

The =~ operator matches the regular expression against a string, and it returns either the offset of the match from the string if it is found, otherwise nil. You can place the string/regex on either side of the operator as you can see above.

Parsing XML with Ruby

As @pguardiario mentioned, Nokogiri is the de facto XML and HTML parsing library. If you wanted to print out the Id and Name values in your example, here is how you would do it: A few notes: at_xpath is for matching one thing. If you know you have multiple items, you want to use xpath instead. Depending on your document, namespaces can be problematic, … Read more

How to use “gets” and “gets.chomp” in Ruby

gets lets the user input a line and returns it as a value to your program. This value includes the trailing line break. If you then call chomp on that value, this line break is cut off. So no, what you have there is incorrect, it should rather be: gets gets a line of text, including a line break at the end. … Read more

What does %w(array) mean?

%w(foo bar) is a shortcut for [“foo”, “bar”]. Meaning it’s a notation to write an array of strings separated by spaces instead of commas and without quotes around them. You can find a list of ways of writing literals in zenspider’s quickref.

How to update Ruby Version 2.0.0 to the latest version in Mac OSX Yosemite?

Open your terminal and run When this is complete, you need to restart your terminal for the rvm command to work. Now, run rvm list known This shows the list of versions of the ruby. Now, run rvm install ruby@latest to get the latest ruby version. If you type ruby -v in the terminal, you should see ruby X.X.X. If it still shows you ruby 2.0., … Read more

what does ? ? mean in ruby

Functions that end with ? in Ruby are functions that only return a boolean, that is, true, or false. When you write a function that can only return true or false, you should end the function name with a question mark. The example you gave shows a ternary statement, which is a one-line if-statement. .nil? is a boolean … Read more