tundal45@wordpress:~$

>puts "Hello, World!"

Ruby Quirks: String#[]

with 4 comments

In the series of posts that have Ruby Quirks in the title, my goal is to keep track of unexpected behaviors I encounter with Ruby as I continue learning the language. I will divide the quirks into separate sections (most likely respective classes) as I encounter them. Since it is the same post, that will be constantly updated, I will put the date I encountered a particular quirk next to it’s subheading. I am going to take Pete’s advice and write separate posts for each quirk I encounter.

Quirk Information

Class: String

Operator: []

String class has [] operator that allows us to get a substring for a given string. For example:

name = "Ashish"
str = name[2,3] # str contains "his"

However, if you pass a single index to the [] operator, the response is not something you would expect. For example:

char = name[1] # char contains 115, the ASCII representation of the character s

While we are in the topic of characters & their ASCII representation, Ruby provides a ? operator to evaluate the ASCII representation of a string. For example:

?a # returns 97
?A # returns 65
?s # returns 115
?AA # results in SyntaxError

This quirk can be seen in Ruby 1.8.7 & earlier versions of Ruby. However, from 1.9.1, the behavior is as expected. For example, in 1.9.1 and beyond:

?a # returns a
"Ashish"[1] # returns s

Written by tundal45

July 10, 2010 at 2:12 am

Posted in ruby

Tagged with , , ,

Ruby: Shovel vs. Plus Equals

with 12 comments

This is going to be first of what I hope to be many blog posts documenting my foray into Ruby programming. Foray might be a bad choice of word given that this is not my first attempt at learning the language. However, the approach that I am taking this time around is more active than my previous attempts so I am hopeful. Like any other endeavor, this too has a goal & a specific one at that – being able to build a foundation in Ruby in time for Lone Star Ruby Conference at the end of August.

The approach this time is to follow Mike Clark‘s advice and build tests as I delve into different features of Ruby while reading The Well Grounded Rubyist by David A. Black.

The reason I am optimistic about this approach is because I am currently working through the wonderful Ruby Koans created by EdgeCase. Ruby Koans was what introduced me to Mike Clark’s approach on learning different facets of Ruby. This is a great starting point for anyone who is new to Ruby as it introduces both the core concepts or Ruby while introducing the idea of testing. Since we are not writing code, it is not test-first-development but more of an inquiry into the ruby interpreter through test cases. The best part about Ruby Koans so far is the question that are posed that forces you to move away from the hows that the tests focus on to more of the whys. I am going to focus on one such whys on this post.

The question posed in one of the Koans was in regards to String modification. The tests were for the shovel (<<) & plus equals (+=) operators. The question was:

“Ruby programmers tend to favor the shovel operator (<<) over the plus equals operator (+=) when building up strings. Why?”

At first, I did not pay attention to this & was about to move to the next failing test. However, since I wanted to do proper justice to my learning experience, I decided to think a little bit about the the two operators in relation to the question being posed. My gut feeling told me that it probably had to do with the fact that the shovel (<<) operator modifies the actual object while the plus equals (+=) operator created a new object altogether.

Of course, I was afraid of being wrong and wanted to verify this. I was about to fire up IRB to test it out when I remembered Mike Clark’s approach to learning and thought I should write a test to see if what I was thinking was the answer was true. So, I decided to write a quick test:
require ‘test/unit’

class StringTest < Test::Unit::TestCase

  def test_plus_equals_creates_new_object
    original_string = "Hello, "
    hi = original_string
    assert_equal original_string.object_id, hi.object_id
    there = "World"
    hi += there
    assert_not_equal original_string.object_id, hi.object_id
  end

  def test_shovel_does_not_create_new_object
    original_string = "Hello, "
    hi = original_string
    assert_equal original_string.object_id, hi.object_id
    there = "World"
    hi << there
    assert_equal original_string.object_id, hi.object_id
  end
end

The test proved my initial hunch. I was going to leave it at that but then I remembered that Array also use both operators. So I wrote another test to verify that the behavior is the same for Arrays:

require 'test/unit'

class ArrayTest < Test::Unit::TestCase

  def test_plus_equals_creates_new_object
    original_string = ["Hello"]
    hi = original_string
    assert_equal original_string.object_id, hi.object_id
    there = ["World"]
    hi += there
    assert_not_equal original_string.object_id, hi.object_id
  end

  def test_shovel_does_not_create_new_object
    original_string = ["Hello"]
    hi = original_string
    assert_equal original_string.object_id, hi.object_id
    there = ["World"]
    hi << there
    assert_equal original_string.object_id, hi.object_id
  end

end

So long story short, the reason why Ruby programmers prefer the shovel operator (<<) over the plus equals (+=) operator when building strings is to avoid creating multiple objects during the process. If there are subtleties that I am missing or if I am completely wrong, I would love to hear from people and improve upon what I have learned so far.

UPDATE: Thanks to @i82much for telling me how to embed code in wordpress.

UPDATE 2: Thanks again to @i82much, you can find out how to have source code displayed for different languages here

Written by tundal45

July 10, 2010 at 12:42 am

Posted in ruby

Tagged with , , ,

On Liberal Arts

with 10 comments

If it seems to you that I keep talking about my experience with the US Higher Education system, you will not be wrong. Past few posts have mostly been a retrospective look into my collegiate years in the United States in an effort to abstract out lessons that I have learned that could be valuable to international students who are interested in coming to the United States. As I have mentioned before, liberal arts education was a completely foreign concept to me when I was first applying to colleges in the United States. In this post, I will attempt to shed some more light on what it means to get a liberal arts education based on my own meandering experience.

A quick search for the term liberal arts education and you come across many links that explain the approach in great detail highlighting not only the characteristics of the approach but also how they can be beneficial to a student. Reading through these articles, you will quickly realize the power of this approach. The motivation behind developing a liberal arts approach to education stems from the fact that our surroundings are in a constant state of change. As human beings, we need to consistently adapt to new scenarios to ensure our survival and further our development as a generation of well rounded professionals. Therefore, the idea that someone should focus on a particular area of study and not even seek to an introduction to other disciplines seems short sighted. Liberal arts education centers around the idea of creating an intellectual and educational environment that fosters curiosity and exploration across different sects. It does not mean that as a student attending a liberal arts institution, you will not be able to concentrate on a particular field of study. I went to a liberal arts institution and spent most of my time studying Mathematics & Computer Science. To be honest, students who are passionate about a field and want to focus a good portion of their energy in delving deeper into the concepts are encouraged to do so through a plethora of independent studies & summer research opportunities.

I knew I wanted to study computer science and mathematics has always captured my interest so my major was pretty much decided. As I mentioned in my earlier post, I was initially looking into engineering programs. However, spending a semester in a liberal arts institution helped me understand the benefits of exploring areas such as philosophy, political science & economics. As the articles above illustrate, a liberal arts curriculum is designed to help you develop a holistic understanding of the world around you. As an example, taking classes across disciplines enables you to develop a great product with algorithms based on sound fundamentals of mathematics and implement it using the new computer language that you learnt in class. Introduction to economics you took last semester gives you the ability to come up with a good pricing strategy & the principles that you will learn in psych & the communication classes next semester will serve as a foundation for a rudimentary marketing strategy to increase your user base giving your product greater visibility in the market. This example might be oversimplified but it illustrates how you can connect the information you acquire across several disciplines in unique and meaningful ways.

Liberal arts education is a perfect vehicle to instill in us the idea of attacking a problem from different angles. An example that illustrates this core principle is an essay that I had to write for my application to Bennington College. It has been seven years so the details are a bit fuzzy but the exercise was to pick an object in the house and describe it from the vantage point of a physicist, a carpenter & I forget the third. At that point, I did not understand the purpose and inherent value of the question because I was still unclear on what liberal arts education is. However, looking back, I realize that exercise was a perfect way to introduce to the prospective students exactly how liberal arts institutions such as Bennington encourage you to analyze the same problem by looking at it from different perspectives. Considering the fact that the success of iPod & Nintendo Wii can be attributed more to a superb user experience brought about re-thinking how users use their product, you can see how a liberal arts background breeds the fundamental skills required for success.

The intention is not to teach the student how to do one particular thing well. Instead, the motivation is to help students develop the ability to become an independent learner. This approach is useful because it prepares us with the knowledge necessary to adapt to the changes that are constantly taking place around us. It also puts the responsibility on the student and makes for a much more independent and engaging educational experience. This is not just limited to picking your classes and majors but includes exploring specialized areas within the major  or combining the fundamentals of different fields in developing a research project or an independent study. The fact that liberal arts school allow you to carve out a personalized interdisciplinary college experience is truly awesome.

Liberal arts education is becoming increasingly important as the world becomes more and more interconnected. With the advancement in telecommunications and other technologies that bring the world to our fingrtips, a solid understanding of different cultures and the awareness of the differences in perspectives is integral to maintain good relationships with our fellow global citizens. Powered by the breadth of knowledge and an uncanny ability to research and learn new things, a professional with a liberal arts background is equipped with all the tools necessary to excel in such an environment. The smaller classes with a focus on discussions over monotonous lectures along with numerous extra-curricular activities provide students with a number of avenues to hone their intellectual and interpersonal skills. The community driven activities are also a great way to help the students understand how a little effort on their part can help the communities that they are part of in a meaningful way.

I hope that this post has been helpful in explaining what liberal arts education is. If you do not fully grasp the concept still, that’s okay. It is one of those things that is better understood when experienced first hand. I may have missed a few details in the post so if there are anything you feel I should include, please let me know through the comments and I will make necessary updates.

UPDATE: I have to thank the author of a wonderful blog on intercultural household for encouraging me to write on this topic.

Written by tundal45

November 18, 2009 at 1:16 pm

Posted in 2cents

Tagged with , ,

Life of Pi

with 6 comments

I just completed reading Life of Pi by Yann Martel. I had been meaning to read this book because I had heard so much about it. I purchased it last winter with other books in this list thinking that there is no better time to get some reading done than cold winter days. However, I quickly got distracted with responsibilities and never got around to it. Recently, I decided that I would much rather read a book than flip though channels aimlessly. I am not saying I am abandoning TV. I am just trying to make better choices in terms of what I watch and not spend the entire weekend watching whatever is on for the sake of watching. Clearly, I digress.

So where were we? Oh yes, Life of Pi. To be honest, I do not know what to think of the book. Initially, I was angry at the main character, Pi Patel, because of his claim that zoo animals are just as happy, if not happier, within the confines of the zoo. I understand that there are ways where zoo animals can be given an environment that closely resembles their natural habitat. However, to claim that this simulated, confined environment is better did not sit right with me. It seemed that Pi, or the author, needed to read Ishmael before making that argument. I realized that the mild hangover I had might have something to do with my grumpiness and overall negative attitude towards the book. So I went to get coffee before reading further. The coffee seemed to do the trick because once I got back, the book seemed a lot less annoying. That’s a positive, right?

This is one of the few books that I have read in just a couple of days. I am not a reader by any stretch of the imagination. I never really read that many fiction or non-fiction growing up outside of what I needed to read for my classes. It was not until high school that I actually started reading few books here and there that I realized the impact a good book has on one’s perception of everything around them. And I started blabbering again.

Going back to the topic at hand, one thing that I find interesting in this book is the religious nature of the main character. What was interesting to me is that for someone who was a devout Hindu, Muslim & Christian at the same time, he never seemed to thank God for being alive with a tiger in the boat throughout his ordeal at sea. He always provided proper reasoning based on what he had learned as a zoo keeper’s son to explain why the tiger let him live. It was only when he saw nature in action, whether it be in the form of lightning storm or the marine life in the sea that he thought of God. To me it felt like he saw God as someone who is responsible for running all the different cycles and not the usual all loving God who shall come to save him like the mainstream religious crowd (not to mention he did not blame God for his plight either).

However, I still cannot decide how I feel about the book. Even though I read the book in just a couple of days, I think it was more because it was just a good story rather than the fact that I felt like I was growing as a person because of the abundance of wisdom throughout the book. Maybe it is just supposed to be a good story and that’s fine  but I was expecting the book that has won numerous accolades to have a little more impact on me than it actually did. It could be that I did not pay proper attention or somehow unconsciously tuned out all the lessons for whatever reasons but I do not feel like I have grown as a person after reading this book. As someone who does not read often, that is something I look for in a book. I want the books I read to make me see things in a different light or make me aware of something  that I did not know. I want reading to be a learning experience rather than just passive experience and unfortunately, Life of Pi did not rate too high for me in that scale.

Written by tundal45

November 17, 2009 at 1:23 pm

Posted in 2cents, book

Tagged with , ,

America, F*** YEAH!

with 2 comments

I am sure that those who have watched the awesomeness that is Team America – World Police will have the song playing in their heads. Before you jump to the conclusion that I have turned into a patriotic American, I want to tell you that this post is about a different aspect of the United States that I have come to love: the higher education system.

Prior to this, I posted about my life as an international student in the United States where one of my friend brought up a really interesting point in the comments section. She pointed out that unlike my problem of having no options but to fight it out, her friends were overwhelmed with options. She was obviously talking about the plethora of options in terms of classes to choose & activities to participate in. In my excitement to shed a light on my struggles as an international student, I completely lost sight of highlighting the positives that are related to academics.You might recall I mentioned how I got to love liberal arts and how I was unaware of what it meant before coming to the United States. However, my friend’s comment made me think about why I wanted to come to the United States in the first place.

I was in 8th grade when my cousin started applying to schools in the United States. Up until that point, the idea of going to a completely different country was not something I entertained. It was not until my cousin started looking into schools in the United States that I even realized that as an option. I remember reading through the brochures that he used to get and being surprised at how much flexibility a student has to carve out the academic experience that they want. “Personalized education, what a novel idea!”, I thought. However, that was all it was at that point, a cool & progressive approach to education.

It was not until 11th grade that I started considering coming to the United States for my undergraduate. In Nepal, after 10th grade, you go to “college” for your 11th & 12th grade. However, there is a significant gap in the level of difficulty between 10th & 11th grade. Furthermore, the class size is generally bigger and the curriculum is very outdated and in dire need of a reform. Since I went to one of the better institutions, I was surrounded by really smart individuals, most of them proficient in the art of grasping abstract concepts as well as memorizing them. I was not so special in that I thrived on learning through class discussions and relied heavily on the teacher. Rote learning was not my cup of tea and it was tougher for me to grasp a concept unless I had someone explain it to me. To make matters worse, there were very few teachers whose approach matched with my style of learning. Soon it became clear that I will not be able to develop in such a environment.

Another major factor for me to want to come to the United States was the opportunity to be independent. With my parents there to take care of me, I was starting to take things for granted. This was another reason why my academics suffered in 11th & 12th grade. I just stopped caring and not appreciating the opportunity that I had. The only way I saw how to fix that was by coming to the United States where I had to figure out things on my own. I knew it was going to be a challenge but I also knew that I will come out of the experience as a better individual. However, little did I know how much more I was going to learn from the experience.

It was not until I got to the United States did I realize the true power of personalizing my educational experience. With majors & electives of my choosing and projects that allowed me to delve in the areas that I was interested in, the opportunities were truly limitless. Another important part of the experience was the value of being an active member of the campus community. It is pretty common, especially among international students, to be content with getting good grades and focus only on activities that somehow relate to them landing a dream job upon graduation. I say this because I used to think along the same lines. It was during my junior year when I helped organize a jazz concert with a professor to raise money for Tsunami victims did I realize that a tiny effort on my part could serve a better purpose than just being  a resume builder. As an international house coordinator my senior year, I got a chance to help incoming international students find their place in the community as well as help the American students develop better understanding of all the cultures represented through cultural events. Being involved on campus was a great way to develop skills I would have not been able to acquire from the classroom setting.

Looking back, I realize that I was lucky to have been able to get everything and more out of my collegiate experience in the United States. It was definitely tough and there were times I wish I had it a little easier but in the end, it was worth all the blood, sweat & tears. More importantly, I realize that since I came for the right reasons, they always helped me make rational decision and remain focused on learning, whether it be in the classroom or outside.

Written by tundal45

November 13, 2009 at 3:24 am

Posted in 2cents

Tagged with , , , ,

Life as an International Student

with 8 comments

I was just reading a blog post my friend wrote about education abroad and I realized how I have always wanted to write about what I have learned as an International Student in the United States. This is that post.

I came to the United States Fall of 2002 as an International Student at Hanover College in Hanover, IN. At that time, I had no understanding what liberal arts education meant even though I had applied to several liberal arts schools. In fact, I was never really forced to think about what liberal arts means & why I wanted to attend a liberal arts institution until I applied to St. Lawrence University as a transfer student. Like most students from a developing nation, my intention was to transfer to an engineering school so I can become an engineer. All that changed when I took my first few requirement classes. I started to realize that while I loved solving math problems & writing programs, I also loved to talk about our purpose in this world, the recent political developments in South Asia, how capitalism & globalization affect the world that we live in and bunch of other things that I had not realized I was interested in until I was exposed to them. It was then that I understood what it took to be a well rounded individual and how a liberal arts education helps one to achieve that goal.

Having been around my parents who took care of me, this was the first time I was outside of my comfort zone. I was fairly certain that I was going to cry myself to sleep every night and be homesick all the time. You see, while I had prepared myself to face the world independently, I had very little idea of how to tackle different things life throws at you until I got here. I always felt that the application process kind of served as an introduction to life in the United States because I had to go out & seek a lot of information & figure out the whole process on my own. That experience made it somewhat easy to transition to American Student life & helped me seek out people & information relevant to multiple issues an international student faces in the first week ranging from registering for classes to finding a job on campus so you can have spending money (or in my case money to pay for tuition).

I realized early on that most people around you are just as scared to talk to you as you are to talk to them. However, if you take that first step to go out and start a conversation, it becomes that much easier to connect with other students. It is especially tough for International Students because its very easy to just hang out with other international kids. Taking the initiative and breaking that initial barrier did  a world of good to me not just in terms of making friends but also in terms of being immersed into the American culture. After all, I was studying abroad and the cultural exchange, as ignored as it was in the original decision making process of coming to the United States, has been an important (and pleasant) side effect.

A quick way to figure things out, as I learned first hand, was not having any options. For example, I did not know how to go about finding a job on campus my first week. However, I no longer had the option of going back to my parents or anyone else for that matter. There was no option but to find a job. The thing is that when you are forced to find an answer, you find a way to get that answer. I used to panic pretty easily when put in pressure situations like that (and I still do to a certain degree) but between finding a job to pay for school, figuring out a way to transfer to a better school with a stronger financial aid package & navigating the tough job market as an unemployed recent graduate, I have become confident that no matter how much of a curve ball life throws at me, I will find a way to get a hit and get on base. I don’t think I would have been able to develop that level of confidence had I not faced so many curve balls. The only thing that’s different from baseball is that in real life, there are no batting practices.

With that said, I want to emphasize that it is not easy being an international student in the United States, especially when you are trying to pay for your school. Financial challenges will be a norm and strict budgeting discipline is a must. Things are not going to fall in place and sometimes it feels like no matter what you do, you are doomed. Its those moments that you need to search for perseverance within yourself & fight the urge to give up. Not everyone is prepared to take on this challenge and, as a result, not everyone survives the challenges an American collegiate life throws at an International Student. However, if you are successful, you are guaranteed to come out of the experience a richer, more confident individual who is that much more prepared to take on life by the horns.

Written by tundal45

November 9, 2009 at 10:40 pm

Foundation Rails 2 by Eldon Alameda

leave a comment »

I recently finished reading & working through the example code for Eldon Alameda’s Foundation Rails 2 book. As someone who has been struggling to learn Rails for the past few years, Eldon’s approach of teaching the fundamentals before diving into building an app was very helpful. This may not apply to everyone but I was always frustrated with the previous books & tutorials where the application building was done before explaining how the plumbing works.

Another good thing about the book is that Eldon talks about how a particular feature in rails has evolved over the years as well as explains the reasoning behind the changes. He also introduces related topics such as RESTful Architecture as an aside without going too far off topic. The fact that we play around with the console immediately is also helpful.

One of the thing that I really enjoyed is how Eldon would give a basic solution to a give problem and walk us through refactoring that piece and turning it into a more elegant code. This is great not only because it teaches us how to go about refactoring our code but also makes sure that the reader first understands the solution in a simple version that is easier to understand before introducing Ruby syntactic sugar & rails magic. I love the fact that Eldon introduces the idea of TDD in the book because it is definitely a good habit to develop and a requirement to be a successful Rails developer.

Eldon also does a really good job of introducing the readers to plugins as well. Rails is known for the extensibility and there are a lot of plugins out there that give us additional functionality. By showing us how to develop applications using plugins, he gives us an idea of what are the kinds of things to consider when using plugins as well as customizing it to fit your needs. All in all, it is a great introductory book for Rails and I highly recommend it to any beginners who are trying to learn Rails.

Written by tundal45

November 9, 2009 at 9:27 pm

Follow

Get every new post delivered to your Inbox.